Lab 9
Lab 9
Activity Outcomes:
After completing this lesson, you should be able to do the following:
Instructor Note:
Please make sure that you have completed the previous labs.
94
1) Useful Concepts
Data manipulation language (DML) is a core part of SQL. When you want to add, update, or delete data
in the database, you execute a DML statement. A collection of DML statements that form a logical unit of
work is called a transaction.
Consider a banking database. When a bank customer transfers money from a savings account to a
checking account, the transaction might consist of three separate operations: decreasing the savings
account, increasing the checking account, and recording the transaction in the transaction journal. The
Oracle server must guarantee that all the three SQL statements are performed to maintain the accounts in
proper balance. When something prevents one of the statements in the transaction from executing, the
other statements of the transaction must be undone.
Note: Most of the DML statements in this lesson assume that no constraints on the table are violated.
Constraints are discussed later in the next lab. In SQL Developer, click the Run Script icon or press [F5]
to run the DML statements. The feedback messages will be shown on the Script Output tabbed page.
INSERT Statement
We can add new rows to a table by issuing the INSERT statement. In the above syntax:
• table is the name of the table
• column is the name of the column in the table to populate
• value is the corresponding value for the column
Note: This statement with the VALUES clause adds only one row at a time to a table.
The INSERT statement can also be used to add rows to a table where the values are derived from existing
tables. To do that, in place of the VALUES clause, you use a subquery as in the following syntax:
The number of columns and their data types in the column list of the INSERT clause must match the
number of values and their data types in the subquery. Zero or more rows are added depending on the
number of rows returned by the subquery.
While insertion, the Oracle server automatically enforces all data types, data ranges, and data integrity
constraints. Any column that is not listed explicitly obtains a null value in the new row unless we have
default values for the missing columns that are used. Common errors that can occur during user input are
checked in the following order:
• Mandatory value missing for a NOT NULL column
• Duplicate value violating any unique or primary key constraint
• Any value violating a CHECK constraint
• Referential integrity maintained for foreign key constraint
• Data type mismatches or values too wide to fit in column
95
UPDATE Statement
We can modify the existing values in a table by using the UPDATE statement.
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
Note: In general, use the primary key column in the WHERE clause to identify a single row for update.
Using other columns can unexpectedly cause several rows to be updated. For example, identifying a
single row in the EMPLOYEES table by name is dangerous, because more than one employee may have
the same name.
DELETE Statement
We can remove existing rows from a table by using the DELETEstatement. In the above syntax:
• table is the name of the table
• condition identifies the rows to be deleted, and is composed of column names, expressions,
constants, subqueries, and comparison operators
Note: If no rows are deleted, the message “0 rows deleted” is returned (on the Script Output tab in SQL
Developer).
96
Activity 8 5 mins Low CLO-5
Activity 9 5 mins Low CLO-5
Activity 10 5 mins Low CLO-5
Activity 11 5 mins Low CLO-5
Activity 12 5 mins Low CLO-5
Output
Method Description
Implicit Omit the column from the column list.
Explicit Specify the NULL keyword in the VALUES list;
specify the empty string ('') in the VALUES list for character strings
and dates.
In this lab activity, we are going to insert rows in department table using both the methods.
97
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
Output
Output
In this lab activity, we are going to record information for employee Raphealy in the EMPLOYEES
table. It sets the HIRE_DATE column to be February 3, 2003.
98
Output
In this lab activity, we are going to create a script with substitution variables to record information for
a department in the DEPARTMENTS table.
Output
99
Output
Here’s another statement that creates a copy of the rows of employees table by using SELECT * in the
subquery:
UPDATE employees
SET department_id = 50
WHERE employee_id = 113;
Output
Here’s another UPDATE statement to update an employee who was a SA_REP and has now changed his
job to an IT_PROG. Consequently, his JOB_ID needs to be updated and the commission field needs to be
set to NULL.
UPDATE employees
SET job_id = 'IT_PROG', commission_pct = NULL
WHERE employee_id = 114;
UPDATE employees
SET (job_id,salary) = (SELECT job_id,salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 103;
100
Output
UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE employee_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
Output
Output
However, if you omit the WHERE clause, all rows in the table are deleted. In the following statement, all
rows from the COPY_EMP table would get deleted, because no WHERE clause was specified.
101
DELETE FROM employees
WHERE department_id IN
(SELECT department_id
FROM departments
WHERE department_name LIKE '%Public%');
Output
In the above statement, the subquery searches the DEPARTMENTS table to find the department number
based on the department name containing the string Public. The subquery then feeds the department
number to the main query, which deletes rows of data from the EMPLOYEES table based on this
department number.
You can use the TRUNCATE statement to quickly remove all rows from a table or cluster. Removing
rows with the TRUNCATE statement is faster than removing them with the DELETE statement for the
following reasons:
• The TRUNCATE statement is a data definition language (DDL) statement and generates no
rollback information. Rollback information is covered later in this lab.
• Truncating a table does not fire the delete triggers of the table.
If the table is the parent of a referential integrity constraint, you cannot truncate the table. You need to
disable the constraint before issuing the TRUNCATE statement. Disabling constraints is covered in the
next lab.
Lab Task 1
Write a SQL statement that increases the commission percentage for every employee in department 80 by
5%.
Lab Task 2
Write a SQL statement that inserts you as an employee.
102
Lab Task 3
Write a SQL statement that inserts Network Operations department located in Africa.
Lab Task 4
Write a SQL statement that updates the Sales department’s location to London
Lab Task 5
Write a SQL statement that deletes all the employees who works for Steven Kings
Lab Task 6
Write a SQL statement that deletes you as an employee.
103