SQL02-DML
SQL02-DML
2
Data Manipulation Language
• A DML statement is executed when you:
– Add new rows to a table
– Modify existing rows in a table
– Remove existing rows from a table
3
Adding a New Row to a Table
4
The INSERT Statement
• Add new rows to a table by using the INSERT statement.
INSERT
INSERT INTO
INTO table
table [(column
[(column [,
[, column...])]
column...])]
VALUES
VALUES (value
(value [,
[, value...]);
value...]);
5
Inserting New Rows
• 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.
SQL> INSERT INTO dept (deptno, dname, loc)
2 VALUES (50, 'DEVELOPMENT', 'DETROIT');
1 row created.
6
Inserting Rows with Null Values
• Implicit method: Omit the column from the column list.
7
Inserting Special Values
• The SYSDATE function records the current date and time.
8
Inserting Values by Using
Substitution Variables
• Create an interactive script by using SQL*Plus
substitution parameters. & is a placeholder for the
variable value.
1 row created.
9
Changing Data in a Table
EMPNO ENAME JOB ... DEPTNO
“…update a row
EMP
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30 in EMP table…”
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
EMP
EMPNO ENAME JOB ... DEPTNO
UPDATE
UPDATE table
table
SET
SET column
column == value
value [,
[, column
column == value,
value, ...]
...]
[WHERE
[WHERE condition];
condition];
11
Updating Rows in a Table
• Specific row or rows to be modified in the WHERE
clause.
SQL> UPDATE emp
2 SET deptno = 20
3 WHERE empno = 7782;
1 row updated.
12
Updating with
Multiple-Column Sub-query
• Update employee 7698’s job and department to match
that of employee 7499.
13
Removing a Row from a Table
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS “…delete a row
30 SALES CHICAGO
40 OPERATIONS BOSTON from DEPT table…”
50 DEVELOPMENT DETROIT
60 MIS DEPT
...
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 MIS
... 14
The DELETE Statement
• You can remove existing rows from a table by using the
DELETE statement.
DELETE
DELETE [FROM]
[FROM] table
table
[WHERE
[WHERE condition];
condition];
15
Deleting Rows from a Table
• Specific rows are deleted when you specify the WHERE
clause.
SQL>
SQL> DELETE
DELETE FROM
FROM department
department
22 WHERE
WHERE dname
dname == 'DEVELOPMENT';
'DEVELOPMENT';
11 row
row deleted.
deleted.
• All rows in the table are deleted if you omit the WHERE
clause.
SQL>
SQL> DELETE
DELETE FROM
FROM department;
department;
44 rows
rows deleted.
deleted.
16
COMMIT and ROLLBACK
• Use COMMIT to permanently save the changes made to
the database
• Use ROLLBACK to undo the previous operation.
• Note:
– If you close SQL*Plus using the x in top right hand corner-your
work will be lost unless you have specifically committed your DML
– If you type ‘Exit’ to close SQL*Plus, your work will be saved
17
Summary
We covered:
– Describe each DML statement
– Insert rows into a table
– Update rows in a table
– Delete rows from a table
18