0% found this document useful (0 votes)
5 views

SQL02-DML

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL02-DML

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Welcome to training of

Structure Query Language (SQL)


DML Commands
Objectives
• In this session you will learn:
– Describe each DML statement
– Insert rows into a table
– Update rows in a table
– Delete rows from a table

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

“…insert a new row


DEPT
into DEPT table…”
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS DEPT
30 SALES CHICAGO DEPTNO DNAME LOC
40 OPERATIONS BOSTON ------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEVELOPMENT DETROIT
4

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...]);

• Only one row is inserted at a time with this syntax.

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.

• Enclose character and date values within single


quotation marks.

6
Inserting Rows with Null Values
• Implicit method: Omit the column from the column list.

SQL> INSERT INTO dept (deptno, dname )


2 VALUES (60, 'MIS');
1 row created.

• Explicit method: Specify the NULL keyword.

SQL> INSERT INTO dept


2 VALUES (70, 'FINANCE', NULL);
1 row created.

7
Inserting Special Values
• The SYSDATE function records the current date and time.

SQL> INSERT INTO emp (empno, ename, job,


2 mgr, hiredate, sal, comm,
3 deptno)
4 VALUES (7196, 'GREEN', 'SALESMAN',
5 7782, SYSDATE, 2000, NULL,
6 10);
1 row created.

8
Inserting Values by Using
Substitution Variables
• Create an interactive script by using SQL*Plus
substitution parameters. & is a placeholder for the
variable value.

SQL> INSERT INTO dept (deptno, dname, loc)


2 VALUES (&department_id,
3 '&department_name', '&location');

Enter value for department_id: 80


Enter value for department_name: EDUCATION
Enter value for location: ATLANTA

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

7839 KING PRESIDENT 10


7698 BLAKE MANAGER 30
7782 CLARK MANAGER 20
10
7566 JONES MANAGER 20
... 10
The UPDATE Statement
• Modify existing rows with the UPDATE statement.

UPDATE
UPDATE table
table
SET
SET column
column == value
value [,
[, column
column == value,
value, ...]
...]
[WHERE
[WHERE condition];
condition];

• Update more than one row at a time, if required.

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.

• All rows in the table are modified if you omit the


WHERE clause.
SQL>
SQL> UPDATE
UPDATE employee
employee
22 SET
SET deptno
deptno == 20;
20;
14
14 rows
rows updated.
updated.

12
Updating with
Multiple-Column Sub-query
• Update employee 7698’s job and department to match
that of employee 7499.

SQL> UPDATE emp


2 SET (job, deptno) =
3 (SELECT job, deptno
4 FROM emp
5 WHERE empno = 7499)
6 WHERE empno = 7698;
1 row updated.

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

You might also like