版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、8Copyright Oracle Corporation, 2001. All rights reserved.Manipulating Data8-2ObjectivesAfter completing this lesson, you should be able to do the following:Describe each DML statementInsert rows into a tableUpdate rows in a tableDelete rows from a tableMerge rows in a tableControl transactions8-3Dat
2、a Manipulation LanguageA DML statement is executed when you:Add new rows to a tableModify existing rows in a tableRemove existing rows from a tableA transaction consists of a collection of DML statements that form a logical unit of work.8-4Adding a New Row to a TableDEPARTMENTS New rowinsert a new r
3、ow into the DEPARMENTS table8-5The INSERT Statement SyntaxAdd new rows to a table by using the INSERT statement.Only one row is inserted at a time with this syntax.INSERT INTOtable (column , column.)VALUES(value , value.);8-6Inserting New RowsInsert a new row containing values for each column.List v
4、alues in the default order of the columns in the table. Optionally, list the columns in the INSERT clause.Enclose character and date values within single quotation marks.INSERT INTO departments(department_id, department_name, manager_id, location_id)VALUES (70, Public Relations, 100, 1700);1 row cre
5、ated.8-7INSERT INTOdepartmentsVALUES(100, Finance, NULL, NULL);INSERT INTOdepartments (department_id, department_name )VALUES(30, Purchasing);Inserting Rows With Null ValuesImplicit method: Omit the column from the column list.Explicit method: Specify the NULL keyword in the VALUES clause.8-8INSERT
6、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.4567, SYSDATE, AC_ACCOUNT, 6900, NULL, 205, 100);Inserting Special ValuesThe SYSDATE function records the current dat
7、e and time.8-9INSERT INTO employeesVALUES (114, Den, Raphealy, DRAPHEAL, 515.127.4561, TO_DATE(FEB 3, 1999, MON DD, YYYY), AC_ACCOUNT, 11000, NULL, 100, 30);Inserting Specific Date ValuesAdd a new employee.Verify your addition.8-10Write your INSERT statement with a subquery.Do not use the VALUES cla
8、use.Match the number and data type of columns in the INSERT clause to those in the 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%;Copying Rows From Another Table8-11Changing Data in a Tabl
9、eEMPLOYEESUpdate rows in the EMPLOYEES table.8-12The UPDATE Statement SyntaxModify existing rows with the UPDATE statement.Update more than one row at a time, if required.UPDATEtableSETcolumn = value , column = value, .WHERE condition;8-13UPDATE employeesSET department_id = 70WHERE employee_id = 113
10、;Specific row or rows are modified if you specify the WHERE clause.All rows in the table are modified if you omit the WHERE clause.Updating Rows in a TableUPDATE copy_empSET department_id = 110;8-14UPDATE employeesSET job_id = (SELECT job_id FROM employees WHERE employee_id = 205), salary = (SELECT
11、salary FROM employees WHERE employee_id = 205) WHERE employee_id = 114;Updating Two Columns With a SubqueryUpdate employee 114s job and salary to match that of employee 205.8-15UPDATE copy_empSET department_id = (SELECT department_id FROM employees WHERE employee_id = 100)WHERE job_id = (SELECT job_
12、id FROM employees WHERE employee_id = 200);Updating Rows Based on Another TableUse subqueries in UPDATE statements to update rows in a table based on values from another table.8-16Delete a row from the DEPARTMENTS table.Removing Rows From a Table DEPARTMENTS 8-17The DELETE StatementYou can remove ex
13、isting rows from a table by using the DELETE statement.DELETE FROM tableWHERE condition;8-18Specific rows are deleted if you specify the WHERE clause.All rows in the table are deleted if you omit the WHERE clause.Deleting Rows From a Table DELETE FROM departments WHERE department_name = Finance;DELE
14、TE FROM copy_emp;8-19DELETE FROM employeesWHERE department_id = (SELECT department_id FROM departments WHERE department_name LIKE %Public%);Deleting Rows Based on Another TableUse subqueries in DELETE statements to remove rows from a table based on values from another table.8-20Overview of the Expli
15、cit Default FeatureWith 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
16、 data.Explicit defaults can be used in INSERT and UPDATE statements.8-21Using Explicit Default ValuesINSERT INTO departments (department_id, department_name, manager_id) VALUES (300, Engineering, DEFAULT);UPDATE departments SET manager_id = DEFAULT WHERE department_id = 10;DEFAULT with INSERT:DEFAUL
17、T with UPDATE:8-22The MERGE StatementProvides the ability to conditionally update or insert data into a database tablePerforms an UPDATE if the row exists, and an INSERT if it is a new row:Avoids separate updatesIncreases performance and ease of useIs useful in data warehousing applications8-23The M
18、ERGE Statement SyntaxYou can conditionally insert or update rows in a table by using the MERGE statement.MERGE INTO table_name table_alias USING (table|view|sub_query) alias ON (join condition) WHEN MATCHED THEN UPDATE SET col1 = col_val1, col2 = col2_val WHEN NOT MATCHED THEN INSERT (column_list) V
19、ALUES (column_values);8-24MERGE INTO copy_emp c USING employees e ON (c.employee_id = e.employee_id)WHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, . c.department_id = e.department_idWHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name, e.last_name, e.
20、email, e.phone_number, e.hire_date, e.job_id, e.salary, mission_pct, e.manager_id, e.department_id);Merging RowsInsert or update rows in the COPY_EMP table to match the EMPLOYEES table.8-25Merging RowsMERGE INTO copy_emp c USING employees e ON (c.employee_id = e.employee_id)WHEN MATCHED THEN UPDATE
21、SET .WHEN NOT MATCHED THEN INSERT VALUES.;SELECT * FROM COPY_EMP;no rows selectedSELECT * FROM COPY_EMP;20 rows selected.8-26Database TransactionsA database transaction consists of one of the following:DML statements which constitute one consistent change to the dataOne DDL statementOne DCL statemen
22、t8-27Database TransactionsBegin when the first DML SQL statement is executedEnd with one of the following events:A COMMIT or ROLLBACK statement is issuedA DDL or DCL statement executes (automatic commit)The session endThe system crashes8-28Advantages of COMMIT and ROLLBACK StatementsWith COMMIT and
23、ROLLBACK statements, you can: Ensure data consistencyPreview data changes before making changes permanentGroup logically related operations8-29Controlling TransactionsSAVEPOINT BSAVEPOINT ADELETEINSERTUPDATEINSERTCOMMITTimeTransactionROLLBACK to SAVEPOINT BROLLBACK to SAVEPOINT AROLLBACK8-30UPDATE.S
24、AVEPOINT update_done;INSERT.ROLLBACK TO update_done;Rolling Back Changes to a MarkerCreate a marker in a current transaction by using the SAVEPOINT statement.Roll back to that marker by using the ROLLBACK TO SAVEPOINT statement.8-31An automatic commit occurs under the following circumstances:DDL sta
25、tement is issuedDCL statement is issuedNormal exit from iSQL*Plus, without explicitly issuing COMMIT or ROLLBACK statementsAn automatic rollback occurs under an abnormal termination of iSQL*Plus or a system failure.Implicit Transaction Processing8-32State of the Data Before COMMIT or ROLLBACKThe pre
26、vious 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
27、.8-33State of the Data After COMMITData 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.8-34COMMIT;Ma
28、ke the changes.Commit the changes.DELETE FROM employeesINSERT INTO departments VALUES (290, Corporate Tax, NULL, 1700);Committing Data8-35State of the Data After ROLLBACKDiscard all pending changes by using the ROLLBACK statement:Data changes are undone.Previous state of the data is restored.Locks o
29、n the affected rows are released.DELETE FROM copy_emp;ROLLBACK;8-36Statement-Level RollbackIf 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 exp
30、licitly by executing a COMMIT or ROLLBACK statement.8-37Read ConsistencyRead 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.Wr
31、iters do not wait for readers.8-38SELECT *FROM userA.employees;Implementation of Read ConsistencyUPDATE employeesSET salary = 7000WHERE last_name = Goyal;DatablocksRollbacksegmentschangedand unchanged databefore change“old” dataUser AUser BReadconsistentimage8-39SELECT salary FROM employeesWHERE last_name=King;24000UPDATE employeesSET salary=salary+10000WHERE last_name=King;24000COMMIT;34000SELECT salary FROM employeesWHERE
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 开展节能宣传活动总结
- 淘宝客服试用期转正工作总结
- 活着的读书心得
- 23.1.1 锐角的三角函数 同步练习
- 23.2 相似图形 同步练习
- 江苏省扬州市扬大附中2024-2025学年高一上学期联考英语试卷(含解析)
- 河北省秦皇岛市卢龙县2024-2025学年八年级上学期期中地理试题
- 天津地区高考语文五年高考真题汇编-名篇名句默写
- 语文教学论教案 第二章 语文教材
- 个人车位买卖协议书范本
- 南昌大学道路交通安全管理暂行办法
- 一体化泵站施工组织设计方案
- 电力--标煤---碳排放之间的换算
- 食堂食品定点采购询价记录表
- 人教六上数学外圆内方外方内圆及课后练习(课堂PPT)
- 管材的计算方法
- 电动叉车控制系统详解(带电路图)
- 软件项目管理实验报告(共17页)
- CNC84操作手册
- 班级文化建设研究课题研究报告
- 学而思寒假七年级尖子班讲义第5讲二元一次方程组进阶
评论
0/150
提交评论