SQL 8 (DML)
SQL 8 (DML)
Manipulating Data
DML
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
Objectives
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 2
Data Manipulation Language
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 3
Adding a New Row to a Table
50 DEVELOPMENT DETROIT
New row
“…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
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 4
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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 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.
DESCRIBE dept
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 6
Inserting Rows with Null Values
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 7
Inserting Special Values
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 8
Inserting Specific Date Values
– Add a new employee.
SQL> INSERT INTO emp
2 VALUES (2296,'AROMANO','SALESMAN',7782,
3 TO_DATE('FEB 3, 97', 'MON DD, YY'),
4 1300, NULL, 10);
1 row created.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
Changing Data in a Table
EMP
EMPNO ENAME JOB ... DEPTNO
“…update a row
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30 in EMP table…”
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
EMP
EMPNO ENAME JOB ... DEPTNO
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
The UPDATE Statement
UPDATE
UPDATE table
table
SET
SET column
column == value
value [,
[, column
column == value,
value, ...]
...]
[WHERE
[WHERE condition];
condition];
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
Updating Rows in a Table
– Specific row or rows are modified when you
specify the WHERE clause.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
Updating with
Multiple-Column Subquery
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
Updating Rows Based
on Another Table
SQL>
SQL> UPDATE
UPDATE employee
employee
22 SET
SET deptno
deptno == (SELECT
(SELECT deptno
deptno
33 FROM
FROM emp
emp
44 WHERE
WHERE empno
empno == 7788)
7788)
55 WHERE
WHERE job
job == (SELECT
(SELECT job
job
66 FROM
FROM emp
emp
77 WHERE
WHERE empno
empno == 7788);
7788);
22 rows
rows updated.
updated.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 15
Updating Rows:
Integrity Constraint Error
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 16
Inserting or Updating Rows Based
on DEFAULT value
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
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
...
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 18
The DELETE Statement
DELETE
DELETE [FROM]
[FROM] table
table
[WHERE
[WHERE condition];
condition];
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 19
Deleting Rows from a Table
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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 20
Deleting Rows Based
on Another Table
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 21
Deleting Rows:
Integrity Constraint Error
a row
SQL>
SQL> DELETE
DELETE FROM
FROM dept
dept
e lete k ey
22 WHERE deptno
deptno == 10;10; not d m ary ey
pri ign k
WHERE
can a
• You ntains a fore .
at co d a s
t ab le
DELETE
DELETE FROM
FROM dept
dept th s use ther
a t i an o
**
t h in
ERROR
ERROR at
at line
line 1:
1:
ORA-02292:
ORA-02292: integrity
integrity constraint
constraint (USR.EMP_DEPTNO_FK)
(USR.EMP_DEPTNO_FK)
violated
violated -- child
child record
record found
found
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 22
Truncating a Table
– The TRUNCATE TABLE statement:
• Removes all rows from a table
• Releases the storage space used by that table
• You can do the same with DELETE statement, which is a
DML statement and hence recoverable, but it is slow.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 23