




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





































40 minutes Lecture 30 minutes Practice 70 minutes Total
Lesson Aim
In this lesson, you will learn how to insert rows into a table, update existing rows in a table, and delete existing rows from a table. You will also learn how to control transactions with the COMMIT, SAVEPOINT, and ROLLBACK statements.
8-
Objectives
After completing this lesson, you shouldAfter completing this lesson, you should
be able to do the following:be able to do the following:
Adding a New Row to a Table
The slide graphic adds a new department to the DEPT table.
8-
Adding a New Row to a Table
DEPTDEPT
New row New row
DEPT DEPT
“…insert a new row “…insert a new row into DEPT table…”into DEPT table…”
Adding a New Row to a Table (continued)
You can add new rows to a table by issuing the INSERT statement. In the 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.
8-
The INSERT Statement
INSERT statement.
this syntax.
8-
Inserting Rows with Null Values
the column list.
keyword.
Methods for Inserting Null Values
Be sure that the targeted column allows null values by verifying the Null? status from the SQL*Plus DESCRIBE command. The Oracle Server automatically enforces all datatypes, data ranges, and data integrity constraints. Any column that is not listed explicitly obtains a null value in the new row.
Instructor Note
Common errors that can occur during user input:
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 only.
8-
Inserting Special Values
The SYSDATE function records theThe SYSDATE function records the
current date and time.current date and time.
Inserting Special Values by Using SQL Functions
You can use pseudocolumns to enter special values in your table. The slide example records information for employee Green in the EMP table. It supplies the current date and time in the HIREDATE column. It uses the SYSDATE function for current date and time. You can also use the USER function when inserting rows in a table. The USER function records the current username.
Confirming Additions to the Table
SQL> SELECT empno, ename, job, hiredate, comm 2 FROM emp 3 WHERE empno = 7196;
8-
Inserting Values by Using
Substitution Variables
Create an interactive script by usingCreate an interactive script by using
SQLPlus substitution parameters.SQLPlus substitution parameters.**
Inserting Values by Using Substitution Variables
You can produce an INSERT statement that allows the user to add values interactively by using SQL*Plus substitution variables. The slide example records information for a department in the DEPT table. It prompts the user for the department number, department name, and location. For date and character values, the ampersand and the variable name are enclosed in single quotation marks.
Instructor Note
Be sure to mention the following points about the example:
8-
Creating a Script
with Customized Prompts
Creating a Script to Manipulate Data
You can save your command with substitution variables to a file and execute the file. Each time you execute the command, it will prompt you for new values. Customize the prompts by using the SQL*Plus ACCEPT command. The example on the slide records information for a department in the DEPT table. It prompts the user for the department number, department name, and location by using customized prompt messages.
Please enter the department number: 90 Please enter the department name: PAYROLL Please enter the location: HOUSTON
1 row created. Do not prefix the SQLPlus substitution parameter with the ampersand (&) when referencing it in the ACCEPT command. Use a dash (-) to continue a SQLPlus command on the next line.
Instructor Note
Be sure to mention the following points about the script:
Changing Data in a Table
The slide graphic changes the department number for Clark from 10 to 20.
8-
Changing Data in a Table
EMPEMP
“…update a row“…update a row in EMP table…”in EMP table…”
EMPEMP
Updating Rows
You can modify existing rows by using the UPDATE 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 or subquery for the column condition identifies the rows to be updated and is composed of column names expressions, constants, subqueries, and comparison operators Confirm the update operation by querying the table to display the updated rows. For more information, see Oracle Server SQL Reference , Release 8, “UPDATE.” Note: In general, use the primary key to identify a single row. Using other columns may unexpectedly cause several rows to be updated. For example, identifying a single row in the EMP table by name is dangerous because more than one employee may have the same name.
Instructor Note
Demo: l9sel.sql, l9upd.sql Purpose: To illustrate displaying the initial state of data, performing updates, and viewing results.
8-
The UPDATE Statement
statement.
required.
8-
Updating with
Multiple-Column Subquery
Update employee 7698’s job and department Update employee 7698’s job and department
to match that of employee 7499.to match that of employee 7499.
Updating Rows with a Multiple-Column Subquery
Multiple-column subqueries can be implemented in the SET clause of an UPDATE statement. Syntax
Instructor Note
It may be worth showing participants that the results would be the same for the example on the slide if two different subqueries were used in the SET clause as illustrated below:
SQL> UPDATE emp 2 SET job = (SELECT job FROM emp 3 WHERE empno = 7499), 4 deptno = (SELECT deptno FROM dept 5 WHERE dname = 'SALES') 6 WHERE empno = 7698;
8-
Updating Rows Based
on Another Table
Use subqueriesUsesubqueries in UPDATE statements toin UPDATE statements to
update rows in a table based on valuesupdate rows in a table based on values
from another table.from another table.
Updating Rows Based on Another Table
You can use subqueries in UPDATE statements to update rows in a table. The example on the slide updates the EMPLOYEE table based on the values from the EMP table. It changes the department number of all employees with employee 7788’s job title to employee 7788’s current department number.
Removing a Row from a Table
The slide graphic removes the DEVELOPMENT department from the DEPT table (assuming that there are no constraints defined on the DEPT table).
Instructor Note
After all the rows have been eliminated with the DELETE statement, only the data structure of the table remains. A more efficient method of emptying a table is with the TRUNCATE statement. 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:
8-
“…“…delete a rowdelete a row
from DEPT table…”from DEPT table…”
Removing a Row from a Table
DEPTDEPT
DEPTDEPT
Deleting Rows
You can remove existing rows by using the DELETE statement. In the syntax: table is the table name condition identifies the rows to be deleted and is composed of column names, expressions, constants, subqueries, and comparison operators For more information, see Oracle Server SQL Reference , Release 8, “DELETE.”
Instructor Note
The DELETE statement does not ask for confirmation. However, the delete operation is not made permanent until the data transaction is committed. Therefore, you can undo the operation with the ROLLBACK statement if you make a mistake.
8-
The DELETE Statement
You can remove existing rows from aYou can remove existing rows from a
table by using the DELETE statement.table by using the DELETE statement.