Professional Documents
Culture Documents
SQL-9 DML
SQL-9 DML
Manipulating Data
Asif Sohail
University of the Punjab
Punjab University College of Information Technology (PUCIT)
© 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
© 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.
SQL> INSERT INTO dept (deptno, dname, loc)
2 VALUES (50, 'DEVELOPMENT', 'DETROIT');
1 row created.
© 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
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 9
Inserting Values by Using Substitution Variables
1 row created.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
Creating a Script
with Customized Prompts
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
Copying Rows
from Another Table
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
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 13
The UPDATE Statement
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
Updating Rows in a Table
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 15
Updating with
Multiple-Column Subquery
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 16
Updating Rows Based
on Another Table
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
Updating Rows:
Integrity Constraint Error
ist
x
o te
SQL> UPDATE emp s n
2 SET deptno = 55 oe
3 WHERE deptno = 10; 5 d
r 5
b e
um
t n
n
UPDATE emp rt me
p a
*
De
ERROR at line 1: •
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK)
violated - parent key not found
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 18
Inserting or Updating Rows Based
on DEFAULT value
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 19
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 20
The DELETE Statement
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 21
Deleting Rows from a Table
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 22
Deleting Rows Based
on Another Table
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 23
Deleting Rows:
Integrity Constraint Error
a row
SQL> DELETE FROM dept
e le te k ey
2 WHERE deptno = 10; o t d
m ary ey
c ann a pri ign k
•You ontains s a fore .
at c d a t ab le
DELETE FROM dept th s use ther
* a t i an o
th in
ERROR at line 1:
ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK)
violated - child record found
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 24
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 25
COMMIT, ROLLBACK and SAVEPOINT
• Statements for transaction management.
• A transaction begins with the first DML statement or by
using an explicit SET TRANSACTION statement and ends
when a COMMIT or ROLLBACK statement is issued.
• Use COMMIT and ROLLBACK SQL statements to
terminate a transaction explicitly.
• The syntax for COMMIT statement is:
– COMMIT [WORK][COMMENT text];
• The optional word WORK is used just to improve
readability.
• The COMMENT is usually employed by distributed
transactions.
– COMMIT COMMENT ‘Employees details have been changed’;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 26
COMMIT, ROLLBACK and SAVEPOINT
• SVAEPOINT gives more control on transaction
management.
• SVAEPOINT marks and saves the current point in a
transaction.
• This allows you to rollback the changes up to that point.
• There can be maximum 5 savepoints in a transaction.
• The syntax for SAVEPOINT statement is:
– SVAEPOINT savepoint-name;
– SVAEPOINT Emp_savepoint;
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 27
COMMIT, ROLLBACK and SAVEPOINT
• ROLLBACK statement is used to undo the changes made
by the transaction. However, it cant undo the changes that
have been made permanent by COMMIT statement.
• The syntax for ROLLBACK statement is:
– ROLLBACK [WORK][TO savepoint_name];
• Examples:
• ROLLBACK; // it will undo the changes made by the
current transaction (up to the last COMMIT)
• ROLLBACK TO rollback_point;
• // it will undo the changes up to the specific savepoint.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 28
COMMIT, ROLLBACK and SAVEPOINT (Example)
Time
Transaction t1
SAVEPOINT s1;
---------- (a)
---------- (b)
t2 SAVEPOINT s2;
---------- (c)
---------- (d)
t3 SAVEPOINT s3;
---------- (e)
----------(f)
t4 ROLLBACK TO s2;
---------- (g)
---------- (h)
t5 COMMIT; // work done since the start of
the transaction plus (g) and (h) will be
saved.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 29
Thank you for your attention.
Asif Sohail
Assistant Professor
University of the Punjab
Punjab University College of Information Technology (PUCIT)
Allama Iqbal (Old) Campus, Anarkali
Lahore, Pakistan
Tel: +92-(0)42-111-923-923 Ext. 154
E-mail: asif@pucit.edu.pk
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 30