Академический Документы
Профессиональный Документы
Культура Документы
Manipulating Data
8-2
Data Manipulation Language
8-3
Adding a New Row to a Table
New
DEPARTMENTS row
8-4
The INSERT Statement Syntax
INSERT
INSERT INTO
INTO table
table [(column
[(column [,
[, column...])]
column...])]
VALUES
VALUES (value
(value [,
[, value...]);
value...]);
8-5
Inserting New Rows
8-6
Inserting Rows With Null Values
8-7
Inserting Special Values
8-8
Inserting Specific Date Values
8-9
Copying Rows
From Another Table
• Write your INSERT statement with a subquery.
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';
4 rows created.
8-10
Changing Data in a Table
EMPLOYEES
8-11
The UPDATE Statement Syntax
8-12
Updating Rows in a Table
8-13
Updating Two Columns With a
Subquery
Update employee 114’s job and salary to match that of
employee 205.
UPDATE employees
SET job_id = (SELECT job_id
FROM employees
WHERE employee_id = 205),
salary = (SELECT salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 114;
1 row updated.
8-14
Updating Rows Based
on Another Table
Use subqueries in UPDATE statements to update
rows in a table based on values from another table.
UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
1 row updated.
8-15
Removing Rows From a Table
DEPARTMENTS
8-16
The DELETE Statement
DELETE
DELETE [FROM]
[FROM] table
table
[WHERE
[WHERE condition];
condition];
8-17
Deleting Rows From a Table
8-18
Deleting Rows Based
on Another Table
Use subqueries in DELETE statements to remove
rows from a table based on values from another table.
8-19
Overview of the Explicit Default
Feature
• With the explicit default feature, you can use the
DEFAULT keyword as a column value where the
column default is desired.
• The addition of this feature is for compliance with
the SQL: 1999 Standard.
• This allows the user to control where and when the
default value should be applied to data.
• Explicit defaults can be used in INSERT and
UPDATE statements.
8-20
Using Explicit Default Values
8-21
The MERGE Statement
8-22
The MERGE Statement Syntax
MERGE
MERGE INTO
INTO table_name
table_name table_alias
table_alias
USING
USING (table|view|sub_query)
(table|view|sub_query) alias
alias
ON
ON (join
(join condition)
condition)
WHEN
WHEN MATCHED
MATCHED THEN
THEN
UPDATE
UPDATE SET
SET
col1
col1 == col_val1,
col_val1,
col2
col2 == col2_val
col2_val
WHEN
WHEN NOT
NOT MATCHED
MATCHED THEN
THEN
INSERT
INSERT (column_list)
(column_list)
VALUES
VALUES (column_values);
(column_values);
8-23
Merging Rows
8-24
Merging Rows
SELECT
SELECT **
FROM
FROM COPY_EMP;
COPY_EMP;
no
no rows
rows selected
selected
MERGE
MERGE INTO
INTO copy_emp
copy_emp cc
USING
USING employees
employees ee
ON
ON (c.employee_id
(c.employee_id == e.employee_id)
e.employee_id)
WHEN MATCHED THEN
WHEN MATCHED THEN
UPDATE
UPDATE SET
SET
...
...
WHEN
WHEN NOT MATCHED
NOT MATCHED THEN
THEN
INSERT VALUES...;
INSERT VALUES...;
SELECT
SELECT **
FROM
FROM COPY_EMP;
COPY_EMP;
20
20 rows
rows selected.
selected.
8-25
Database Transactions
8-26
Database Transactions
8-27
Advantages of COMMIT
and ROLLBACK Statements
With COMMIT and ROLLBACK statements, you can:
• Ensure data consistency
• Preview data changes before making changes
permanent
• Group logically related operations
8-28
Controlling Transactions
Time
COMMIT
Transaction
DELETE
SAVEPOINT A
INSERT
UPDATE
SAVEPOINT B
8-29
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.
UPDATE...
SAVEPOINT update_done;
Savepoint created.
INSERT...
ROLLBACK TO update_done;
Rollback complete.
8-30
Implicit Transaction Processing
8-31
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 by the current user.
• The affected rows are locked; other users cannot change
the data within the affected rows.
8-32
State of the Data After COMMIT
8-33
Committing Data
INSERT
INSERT INTO
INTO departments
departments
VALUES
VALUES (290,
(290, 'Corporate
'Corporate Tax',
Tax', NULL,
NULL, 1700);
1700);
11 row
row inserted.
inserted.
8-34
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.
DELETE FROM copy_emp;
22 rows deleted.
ROLLBACK;
Rollback complete.
8-35
Statement-Level Rollback
8-36
Read Consistency
8-37
Implementation of Read
Consistency
User A
UPDATE employees Data
SET salary = 7000 blocks
WHERE last_name = 'Goyal';
Rollback
segments
changed
SELECT * and
FROM userA.employees; Read unchanged
data
consistent
image before
change
“old” data
User B
8-38
Locking
8-40
Implicit Locking
8-41
Summary
In this lesson, you should have learned how to use DML
statements and control transactions.
Statement Description
8-42