Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Manipulation Language: Module of Instruction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

MODULE OF INSTRUCTION

Data Manipulation Language


Welcome to the fourth module of this course Database
Management System 1! For this lesson, we will discuss the
Data Manipulation Language or also known as DML.

Data Manipulation Language is used to insert update and


delete value in the table.

DML Data manipulation language (DML) is a core part of SQL.


When you want to add, update, or delete data in the database,
you execute a DML statement. A collection of DML
statements that form a logical unit of work is called a
transaction.

After completing this lesson, the student should be able


to:
 Insert new values in the table using the INSERT
statement.
 Modify existing values in the table using the
UPDATE statement
 Delete existing row using DELETE statement.

Data Manipulation Language


A DML statement is executed when you:
 Add new rows to a table using INSERT statement

Database Management System 1 1


Week 3 Data Manipulation Language

 Modify existing rows in a table suing UPDATE


statement
 Remove existing rows from a table using DELETE
statement

A transaction consists of a collection of DML statements


that form a logical unit of work.
 Consider an enrollment database. When a student
already took the subject, the status of this subject
should be updated from enrolled to pass, as well a the
year level of the student must also change.

INSERT statement
 Insert a new row containing values for each column.
 List values in the default order of the columns in the
table.
 Optionally, list the columns in the INSERT clause.
 Enclose character and date values within single
quotation marks.
 Add new rows to a table by using the INSERT
statement:
 Syntax:
INSERT INTO tbl_name values (column_list);
 In the syntax:
o TBL_NAME - Is the name of the table
o COLUMN_LIST - Is the name of the column
in the table to populate
o VALUES - Is the corresponding value for the
column

_____________________________________________________________________________________
2
MODULE OF INSTRUCTION

 Old Table: with three values

 Suppose that the STUDENTS table is composed of 3


rows and a user wanted to add a new student record:
 Example:
INSERT INTO STUDENTS VALUES
(500,'REYES','ANNA','BSIS','1');
 To confirm that ANNA was successfully added used
the SELECT statement:

 As shown in the image above notice that Anna is now


one of the students recorded in STUDENTS table.

Inserting Rows with Null Values


 Implicit method: Omit the column from the
column list.
 Example:
INSERT INTO STUDENTS
(USN_ID,LASTNAME)
VALUES (502,'CUBAO');

Database Management System 1 3


Week 3 Data Manipulation Language

 Output:

 As you notice, in the implicit method specific


columns are specified during the insertion before the
values are added in the VALUES clause using this
method will automatically set the column to NULL to
those columns which are not called in the INSERT
INTO statement.

 Explicit method: Specify the NULL keyword in the


VALUES clause.
 EXAMPLE:
INSERT INTO STUDENTS VALUES
(505,'ABIOG','SHIELA',NULL,NULL);
 Output:

 As shown in the example, using the implicit method


this method is used to insert all values according to its
order in the column lists, however, notice that to

_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
those values with no inputs this is set to NULL in the
INSERT INTO statement. By using this method
automatically column with values set to NULL will
automatically have NULL values.

Changing Data in a Table


 Modify existing values in a table with the UPDATE
statement:
 Syntax:
UPDATE TBL_NAME
SET COLUMN = NEW VALUES
[WHERE CONDITION];
 In the syntax:
Tbl_name - Is the name of the table
set column - Is the name of the column in the table to
populate
new_value - Is the corresponding value or subquery
for the column
condition - Identifies the rows to be updated by using
comparison operator, logical condition
and or adding subqueries.

 As shown in the table supposed that we wanted to


update the firstname to ‘MARVIN’ of students with
USN_ID equal to 502. To do that we may need to use

Database Management System 1 5


Week 3 Data Manipulation Language

the UPDATE statement.


 Example:
UPDATE STUDENTS
SET FIRSTNAME = 'MARVIN'
WHERE USN_ID=502;
 Output:

 As you notice in the output the firstname of student


with USN_ID equal to 502 is not updated to
MARVIN.
 Where condition identifies into which specific
column the user wanted to make the changes given in
the SET clause.

Updating two columns in one UPDATE statement.


 To update to or more columns in one UPDATE
statement put a comma (,) after each column called in
the SET clause.
 Example:
UPDATE STUDENTS
SET YR_LVL='IRREG', LASTNAME = 'ABIOG'
WHERE USN_ID = 505;

_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
 Output: Old table

 Output: New table

 Compare the Old and new table after the Update


statement is issued as you notice the LASTNAME
and YR_LVL of the student of USN_ID equal to 505
were updated by using 1 UPDATE statement.

DELETE Statement
 You can remove existing rows from a table by using
the DELETE statement:
 Syntax:
DELETE FROM TBL_NAME
WHERE CONDITION;
 In the syntax:
Tbl_name - Is the name of the table
Where condition - Identifies the rows to be deleted,
and is composed of column names, expressions,
constants, subqueries, and comparison operators.

Database Management System 1 7


Week 3 Data Manipulation Language

 If WHERE condition is added on the statement,


specific rows will be deleted
 Example:
DELETE FROM STUDENTS
WHERE YR_LVL='1';
 Output: Old table

 Output: New table

 As by comparing old and new output notice that 2


rows with YR_LCL equal to 1 are deleted as shown
in the new output.
 All rows in the table are deleted if you omit the
WHERE clause:
 Example:
DELETE FROM STUDENTS;
 Output:

_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
 As in the output all rows are deleted if WHERE
clause is omitted.

Lesson Summary:
In this lesson, you should have learned how to use the
following statements:

 Insert into to add new rows in a table

 Update to modify existing values is a table

 Delete to remove specific rows in a table

Terms to Remember!
 DELETE – a statement use to remove existing rows
in a table.
 DML – stand for Data Manipulation Language.
 Explicit method - an Insert statement that Specifies
the NULL keyword in the VALUES clause.
 Implicit method - an Insert statement that Omits the
column from the column list.
 INSERT - a statement used to add new rows in a
table.
 Transaction - consists of a collection of DML
statements that form a logical unit of work.
 UPDATE - a statement used to update existing rows
in a table.

References Textbook:
 Oracle Press (2010). Applied Oracle Security

References:
 Pratt, Philip J. (2010). Database management systems

Database Management System 1 9


Week 3 Data Manipulation Language

 Rob, Peter & Coronel, Carlo (2009). Database


Management Systems
 Schwalbe, Kathy (2011). Management of Information
Technology Projects
 Wheeler, Evan (2011). Security Risk Management :
Building an Information Security Risk Management
Program from the Ground Up

Supplementary Reading and Video Link


Supplementary Reading
https://docs.microsoft.com/en-us/sql/t-sql/queries/queries
https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-
manipulation.html
https://docs.oracle.com/database/121/TDDDG/tdddg_dml.htm#T
DDDG99941

Supplementary Video
https://www.youtube.com/watch?v=S54W4oSqp7s
https://www.youtube.com/watch?v=vnFu6sBfcSs
https://www.youtube.com/watch?v=0EmMIUvjPbA

Suggested Reading
 SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
 Database management system. In Encyclopedia
Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/152201/
database-management-system-DBMS.
 SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/
SQL
 Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_admini

_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
stration.aspx
 SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
 Tutorialspoint.com
 oracle.com
 apex.oracle.com

Database Management System 1 11

You might also like