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

Lab-Lecture-week-06(2)

The document provides an overview of Data Manipulation Language (DML) in SQL, detailing how to insert, update, and delete rows in a database table. It includes examples of DML statements such as INSERT, UPDATE, and DELETE, as well as methods for handling special values and integrity constraints. Additionally, it emphasizes the importance of using COMMIT and ROLLBACK to manage changes in the database.

Uploaded by

jessie.tang1022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab-Lecture-week-06(2)

The document provides an overview of Data Manipulation Language (DML) in SQL, detailing how to insert, update, and delete rows in a database table. It includes examples of DML statements such as INSERT, UPDATE, and DELETE, as well as methods for handling special values and integrity constraints. Additionally, it emphasizes the importance of using COMMIT and ROLLBACK to manage changes in the database.

Uploaded by

jessie.tang1022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Lab Week – 6

Data Manipulation Language


Manipulating Data
• In this session:
– Describe each DML statement
– Insert rows into a table
– Update rows in a table
– Delete rows from a table
Data Access Language
Oracle

Data Retrieval Data Definition Data Manipulation Data Control


Language Language Language

SELECT CREATE UPDATE GRANT


DROP INSERT REVOKE
ALTER DELETE

l DML - Commands used to maintain and query data in a DB


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
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
5 DETROIT
The INSERT Statement
• Add new rows to a table by using the INSERT statement.

INSERT INTO table [(column [, column...])]


VALUES (value [, value...]);

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


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.
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.
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.
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.
Copying Rows
from Another Table
• Write your INSERT statement with a subquery.
SQL> INSERT INTO managers(id, name, salary, hiredate)
2 SELECT empno, ename, sal, hiredate
3 FROM emp
4 WHERE job = 'MANAGER';
3 rows created.

• Do not use the VALUES clause.


• Match the number of columns in the INSERT
clause to those in the subquery.
Changing Data in a Table
EMPNO ENAME JOB ... DEPTNO
“…update a row
7839
EMP 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
... 12
The UPDATE Statement
• Modify existing rows with the UPDATE statement.

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

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


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> UPDATE employee
2 SET deptno = 20;
14 rows updated.
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.
Updating Rows Based
on Another Table
• Use subqueries in UPDATE statements to update rows in
a table based on values from another table.
SQL> UPDATE employee
2 SET deptno = (SELECT deptno
3 FROM emp
4 WHERE empno = 7788)
5 WHERE job = (SELECT job
6 FROM emp
7 WHERE empno = 7788);
2 rows updated.
Updating Rows:
Integrity Constraint Error
SQL> UPDATE emp
o t
2 SET deptno = 55
s n
3 WHERE deptno = 10;
o e
5 d
r 5
b e
u m
UPDATE emp
t n
*
e n
ERROR at line 1:
r tm
p a
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK)
violated - parent key enot found
•D ist
ex
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
... 18
The DELETE Statement
• You can remove existing rows from a table by using the
DELETE statement.

DELETE [FROM] table


[WHERE condition];
Deleting Rows from a Table
• Specific rows are deleted when you specify the WHERE
clause.

SQL> DELETE FROM department


2 WHERE dname = 'DEVELOPMENT';
1 row deleted.

• All rows in the table are deleted if you omit the WHERE
clause.

SQL> DELETE FROM department;


4 rows deleted.
Deleting Rows Based
on Another Table
• Use subqueries in DELETE statements to remove rows
from a table based on values from another table.

SQL> DELETE FROM employee


2 WHERE deptno =
3 (SELECT deptno
4 FROM dept
5 WHERE dname ='SALES');
6 rows deleted.
Deleting Rows:
Integrity Constraint Error

SQL> DELETE FROM dept


2 WHERE deptno = 10; r o w
te a e y
d e l e r y k
n o t r im a n
c a n a p r e i g
DELETE FROM dept
o u i n s a fo
* •Y o n ta a s
a t c s e d
ERROR at line 1:
•thconstraint
i s u (USR.EMP_DEPTNO_FK)
e y e .
ORA-02292: integrity a t k b l
•thfound
violated - child record e r ta
n o th
•i n a
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
Lab Activities
• Complete the exercise.

You might also like