Objectives
After completing this lesson,you should be able to do the following:
- Describe each data manipulation language(DML) statement.
- Insert rows into a table.
- Update rows in a table.
- Delete rows from a table.
- Control transactions.
Lesson Agenda
- Adding new rows in a table
- -INSERT statement
- Changing data in a table
- -UPDATE statement
- Removing rows from a table:
- -DELETE statement
- -TRUNCATE statement
- Database transactions control using COMMIT,ROLLBACK,and SAVEPOINT
- Read consistency
- FOR UPDATE clause in a SELECT statement
Data Manipulatioin 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.
Adding a New Row to a table
very simple to do , you know
INSERT Statement Syntax
- Add new rows to a table by using the INSERT statement
INSERT INTO table [(column,[,column...])] VALUES (value [,value...]);
- With this syntax,only one row is inserted at a time.
INSERT 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.
INSERT INTO departments(department_id,department_name,manager_id,location_id) VALUES(70,'Public Relations',100,1700);
- Enclose character and date values within single quotation marks.
Build a Lab Environment

SQL> show user USER is "HR" SQL> CREATE TABLE DEPT AS SELECT * FROM departments; Table created. SQL> SELECT * FROM DEPT; DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 10 Administration 200 1700 20 Marketing 201 1800 30 Purchasing 114 1700 40 Human Resources 203 2400 50 Shipping 121 1500 60 IT 103 1400 70 Public Relations 204 2700 80 Sales 145 2500 90 Executive 100 1700 100 Finance 108 1700 110 Accounting 205 1700 DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 120 Treasury 1700 130 Corporate Tax 1700 140 Control And Credit 1700 150 Shareholder Services 1700 160 Benefits 1700 170 Manufacturing 1700 180 Construction 1700 190 Contracting 1700 200 Operations 1700 210 IT Support 1700 220 NOC 1700 DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 230 IT Helpdesk 1700 240 Government Sales 1700 250 Retail Sales 1700 260 Recruiting 1700 270 Payroll 1700 27 rows selected.

SQL> INSERT INTO DEPT(department_id,department_name,manager_id,location_id) 2 VALUES(280,'Boobooke',108,1700); 1 row created. SQL>
Inserting Rows with Null Values
- Implict method:Omit the column from the column list.
INSERT INTO departments (department_id,department_name) VALUES (30,'Purchasing');
- Explict method:Specify the NULL keyword in the AVALUES clause.

INSERT INTO departments VALUES (100,'Finance',NULL,NULL);
Inserting Special Values
The SYSDATE function records the current date and time
INSERT INTO employees (employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,commission_pct,manager_id,department_id) VALUES(113,'Louis','Popp','LPOPP','515.124.3456',SYSDATE,'AC_ACCOUNT',6900,NULL,205,110);
Inserting Specific Date and Time Values
- Add a new employee.
INSERT INTO employees VALUES(114,'Den','Rephealy','DRAPHEAL','515,127,4567',TO_DATE('FEB 3,1999','MON DD,YYYY'),'SA_REP',11000,0.2,100,60);
- Verify your addition.
SELECT * FROM employees WHERE employee_id = 114;
Creating a Script
- Use &substitution in a SQL statement prompt for values.
- & is a placeholder for the variable value.
INSERT INTO departments(department_id,department_name,location_id) VALUES(&department_id,'&department_name',&lcoation);--尤其注意里面的单引号.

SQL> INSERT INTO dept(department_id,department_name,location_id) VALUES(&department_id,'&department_name',&lcoation); Enter value for department_id: 298 Enter value for department_name: arcerzhang Enter value for lcoation: 1700 old 1: INSERT INTO dept(department_id,department_name,location_id) VALUES(&department_id,'&department_name',&lcoation) new 1: INSERT INTO dept(department_id,department_name,location_id) VALUES(298,'arcerzhang',1700) 1 row created.
Copying Rows from Another Table
- Write your INSERT statement with a subquery:
- INSERT INTO sales_reps()
- Do not use the VALUES clause.
- Match the number of colukmns in the INSERT clause to those in the subquery
- Inserts all the rows returned by the subquery in the table,sales_reps.

SQL> INSERT INTO DEPT SELECT * FROM departments; 27 rows created. SQL> SELECT * FROM DEPT; DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 10 Administration 200 1700 20 Marketing 201 1800 30 Purchasing 114 1700 40 Human Resources 203 2400 50 Shipping 121 1500 60 IT 103 1400 70 Public Relations 204 2700 80 Sales 145 2500 90 Executive 100 1700 100 Finance 108 1700 110 Accounting 205 1700 DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 120 Treasury 1700 130 Corporate Tax 1700 140 Control And Credit 1700 150 Shareholder Services 1700 160 Benefits 1700 170 Manufacturing 1700 180 Construction 1700 190 Contracting 1700 200 Operations 1700 210 IT Support 1700 220 NOC 1700 DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 230 IT Helpdesk 1700 240 Government Sales 1700 250 Retail Sales 1700 260 Recruiting 1700 270 Payroll 1700 27 rows selected.