Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

CIT365-Lab-Practice-Tutorial-04 - SQL DML - Manipulating & Inserting Data

Uploaded by

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

CIT365-Lab-Practice-Tutorial-04 - SQL DML - Manipulating & Inserting Data

Uploaded by

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

4

SQL-DML
Manipulating
&
Inserting Data

Copyright © Oracle Corporation, 1999. All rights reserved.


Objectives

After completing this lesson, you should


be able to do the following:
• Describe each DML statement
• Insert rows into a table
• Update rows in a table
• Delete rows from a table
• Control transactions

9-2 Copyright © Oracle Corporation, 1999. All rights reserved.


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
• A transaction consists of a collection of
DML statements that form a logical unit
of work.

9-3 Copyright © Oracle Corporation, 1999. All rights reserved.


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 DEPT
YORK DEPTNO DNAME LOC
20 RESEARCH DALLAS ------ ----------
30 SALES CHICAGO --------
40 OPERATIONS BOSTON 10 ACCOUNTING NEW
YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
50 DEVELOPMENT
40 OPERATIONS BOSTON
DETROIT
9-4 Copyright © Oracle Corporation, 1999. All rights reserved.
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.

9-5 Copyright © Oracle Corporation, 1999. All rights reserved.


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.
9-6 Copyright © Oracle Corporation, 1999. All rights reserved.
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.

9-7 Copyright © Oracle Corporation, 1999. All rights reserved.


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.

9-8 Copyright © Oracle Corporation, 1999. All rights reserved.


Inserting Specific Date Values

• Add a new employee.


SQL> INSERT INTO emp
2 VALUES (2296,'AROMANO','SALESMAN',7782,
3 TO_DATE('FEB 3, 1997', 'MON DD, YYYY'),
4 1300, NULL, 10);
1 row created.

• Verify your addition.


EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- -------- ---- --------- ---- ---- ------
2296 AROMANO SALESMAN 7782 03-FEB-97 1300 10

9-9 Copyright © Oracle Corporation, 1999. All rights reserved.


Inserting Values by Using
Substitution Variables X
Create an interactive script by using
SQL*Plus substitution parameters.
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-10 Copyright © Oracle Corporation, 1999. All rights reserved.


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.

9-11 Copyright © Oracle Corporation, 1999. All rights reserved.


Updating Rows in a Table
• Specific row or rows are modified when
you specify 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 EMP
2 SET deptno = 20;
14 rows updated.

9-12 Copyright © Oracle Corporation, 1999. All rights reserved.


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

7839 KING PRESIDENT 10


7698 BLAKE MANAGER 30
7782 CLARK MANAGER 20
10
7566 JONES MANAGER 20
...

9-13 Copyright © Oracle Corporation, 1999. All rights reserved.


Updating with
Multiple-Column Subquery
(Next class)
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.

9-14 Copyright © Oracle Corporation, 1999. All rights reserved.


Updating Rows:
Integrity Constraint Error
i s t
ex
o t
SQL> UPDATE emp s n
o e
2 SET deptno = 55
5 d
3 WHERE deptno = 10;
r 5
b e
um
tn
en
UPDATE emp
a rtm
* e p
ERROR at line 1: D
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK)
violated - parent key not found

9-15 Copyright © Oracle Corporation, 1999. All rights reserved.


16
Referential Integrity Rule
Referential integrity rule - a database constraint that
ensures that relationships between tables remain valid
and consistent
When one table is linked to another table through the
PK/FK, referential integrity dictates that you may not add a
record to the table that contains the FK unless there is a
corresponding PK in the linked table
Also, you cannot delete a record in the PK table if that PK
is a FK in another table.
Also, cannot change PK if change invalidates the link to
data in the FK table

9-16 Copyright © Oracle Corporation, 1999. All rights reserved.


Removing a Row from a Table
DEPT
DEPTNO DNAME LOC
------ ----------
--------
10 ACCOUNTING NEW “…delete a row
YORK
20 RESEARCH DALLAS from DEPT table…”
30 SALES CHICAGO
40 OPERATIONS BOSTON DEPT
50 DEVELOPMENT
DETROIT DEPTNO DNAME LOC
60 MIS ------ ----------
... --------
10 ACCOUNTING NEW
YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 MIS
...
9-17 Copyright © Oracle Corporation, 1999. All rights reserved.
The DELETE Statement

You can remove existing rows from a table


by using the DELETE statement.
DELETE [FROM] table
[WHERE condition];

9-18 Copyright © Oracle Corporation, 1999. All rights reserved.


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

SQL> DELETE FROM DEPT


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

• All rows in the table are deleted if you


omit the WHERE clause.

SQL> DELETE FROM DEPT;


4 rows deleted.

9-19 Copyright © Oracle Corporation, 1999. All rights reserved.


Deleting Rows:
Integrity Constraint Error

r o w
SQL> DELETE FROM dept t e a e y
e l e ry ey k
2 WHERE deptno = 10; t d m a
a n no a pri ign k
u c i n s o re
Y o n t a a f .
co a s a b l e
t
DELETE FROM dept tha is used ther t
* a t a n o
ERROR at line 1:
th in
ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK)
violated - child record found

9-20 Copyright © Oracle Corporation, 1999. All rights reserved.


Advantages of COMMIT
and ROLLBACK Statements
• Ensure data consistency
• Preview data changes before making
changes permanent
• Group logically related operations

9-21 Copyright © Oracle Corporation, 1999. All rights reserved.


Controlling Transactions

9-22 Copyright © Oracle Corporation, 1999. All rights reserved.


State of the Data Before
COMMIT or ROLLBACK
• The previous state of the data can be recovered.
• The current user can review the results of the
DML operations by using the SELECT
statement.
• Other users cannot view the results of the DML
statements made by the current user.
• The affected rows are locked; other users
cannot change the data within the affected
rows.

9-23 Copyright © Oracle Corporation, 1999. All rights reserved.


State of the Data After COMMIT
• Data changes are made permanent in the
database.
• The previous state of the data is
permanently lost.
• All users can view the results.
• Locks on the affected rows are released;
those rows are available for other users to
manipulate.
• All savepoints are erased.

9-24 Copyright © Oracle Corporation, 1999. All rights reserved.


Committing Data

• Make the changes.


SQL> UPDATE emp
2 SET deptno = 10
3 WHERE empno = 7782;
1 row updated.

• Commit the changes.


SQL> COMMIT;
Commit complete.

9-25 Copyright © Oracle Corporation, 1999. All rights reserved.


State of the Data After ROLLBACK
Discard all pending changes by using the
ROLLBACK statement.
• Data changes are undone.
• Previous state of the data is restored.
• Locks on the affected rows are released.

SQL> DELETE FROM employee;


14 rows deleted.
SQL> ROLLBACK;
Rollback complete.

9-26 Copyright © Oracle Corporation, 1999. All rights reserved.


Rolling Back Changes
to a Marker
• Create a marker in a current transaction
by using the SAVEPOINT statement.
• Roll back to that marker by using the
ROLLBACK TO SAVEPOINT statement.

SQL> UPDATE...
SQL> SAVEPOINT update_done;
Savepoint created.
SQL> INSERT...
SQL> ROLLBACK TO update_done;
Rollback complete.

9-27 Copyright © Oracle Corporation, 1999. All rights reserved.


Statement-Level Rollback
• If a single DML statement fails during
execution, only that statement is rolled
back.
• The Oracle Server implements an
implicit savepoint.
• All other changes are retained.
• The user should terminate transactions
explicitly by executing a COMMIT or
ROLLBACK statement.

9-28 Copyright © Oracle Corporation, 1999. All rights reserved.


Read Consistency
• Read consistency guarantees a
consistent view of the data at all times.
• Changes made by one user do not
conflict with changes made by another
user.
• Read consistency ensures that on the
same data:
– Readers do not wait for writers
– Writers do not wait for readers

9-29 Copyright © Oracle Corporation, 1999. All rights reserved.


Implementation of Read
Consistency
UPDATE emp Data
SET sal = 2000 blocks
WHERE ename =
'SCOTT';
Rollback
segments
User A
changed
SELECT * and
FROM emp; Read unchanged
consistent data
image before
change
“old” data
User B

9-30 Copyright © Oracle Corporation, 1999. All rights reserved.


Locking
Oracle locks:
• Prevent destructive interaction between
concurrent transactions
• Require no user action
• Automatically use the lowest level of
restrictiveness
• Are held for the duration of the
transaction
• Have two basic modes:
– Exclusive
– Share
9-31 Copyright © Oracle Corporation, 1999. All rights reserved.
Summary

Statement Description
INSERT Adds a new row to the table
UPDATE Modifies existing rows in the table
DELETE Removes existing rows from the table
COMMIT Makes all pending changes permanent
SAVEPOINT Allows a rollback to the savepoint marker
ROLLBACK Discards all pending data changes

9-32 Copyright © Oracle Corporation, 1999. All rights reserved.


Practice

• Create the tables for Student-Advisor


data model (Show ER in class)
• Insert 3 rows in each table
• Updating one row in student table
• Write any 3 queries and answer them
• Upload your work on Bb\Practice 6

9-33 Copyright © Oracle Corporation, 1999. All rights reserved.

You might also like