版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Manipulating DataObjectivesAfter completing this lesson, you should be able to do the following:Describe each data manipulation language (DML) statementInsert rows into a tableUpdate rows in a tableDelete rows from a tableControl transactionsData Manipulation LanguageA DML statement is executed when
2、 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.Adding a New Row to a TableDEPARTMENTS New rowInsert new rowinto theDEPARTMENTS tableINSERT Statement SyntaxAdd new ro
3、ws to a table by using the INSERT statement:With this syntax, only one row is inserted at a time.INSERT INTOtable (column , column.)VALUES(value , value.);Inserting New RowsInsert a new row containing values for each column.List values in the default order of the columns in the table. Optionally, li
4、st the columns in the INSERT clause.Enclose character and date values in single quotation marks.INSERT INTO departments(department_id, department_name, manager_id, location_id)VALUES (70, Public Relations, 100, 1700);1 row created.INSERT INTOdepartmentsVALUES(100, Finance, NULL, NULL);1 row created.
5、INSERT INTOdepartments (department_id, department_name )VALUES(30, Purchasing);1 row created.Inserting Rows with Null ValuesImplicit method: Omit the column from the column list.Explicit method: Specify the NULL keyword in the VALUES clause.INSERT INTO employees (employee_id, first_name, last_name,
6、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);1 row created.Inserting Special ValuesThe SYSDATE function records the current date and time.Add a new employee.Verify y
7、our addition.Inserting Specific Date ValuesINSERT INTO employeesVALUES (114, Den, Raphealy, DRAPHEAL, 515.127.4561, TO_DATE(FEB 3, 1999, MON DD, YYYY), AC_ACCOUNT, 11000, NULL, 100, 30);1 row created.INSERT INTO departments (department_id, department_name, location_id)VALUES (&department_id, &depart
8、ment_name,&location);Creating a Script Use & substitution in a SQL statement to prompt for values.& is a placeholder for the variable value.1 row created.Copying Rows from Another TableWrite your INSERT statement with a subquery:Do not use the VALUES clause.Match the number of columns in the INSERT
9、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%;4 rows created.Changing Data in a TableEMPLOYEESUpdate rows in the EMPLOYEES table:Modify existing rows with the UPDAT
10、E statement:Update more than one row at a time (if required).UPDATEtableSETcolumn = value , column = value, .WHERE condition;UPDATE Statement SyntaxSpecific 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 Tab
11、leUPDATE employeesSET department_id = 70WHERE employee_id = 113;1 row updated.UPDATE copy_empSET department_id = 110;22 rows updated.UPDATE employeesSET job_id = (SELECT job_id FROM employees WHERE employee_id = 205), salary = (SELECT salary FROM employees WHERE employee_id = 205) WHERE employee_id
12、= 114;1 row updated.Updating Two Columns with a SubqueryUpdate employee 114s job and salary to match that of employee 205.UPDATE copy_empSET 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 update
13、d.Updating Rows Based on Another TableUse subqueries in UPDATE statements to update rows in a table based on values from another table:Delete a row from the DEPARTMENTS table:Removing a Row from a Table DEPARTMENTS DELETE StatementYou can remove existing rows from a table by using the DELETE stateme
14、nt:DELETE FROM tableWHERE condition;Deleting Rows from a TableSpecific rows are deleted if you specify the WHERE clause:All rows in the table are deleted if you omit the WHERE clause: DELETE FROM departments WHERE department_name = Finance;1 row deleted.DELETE FROM copy_emp;22 rows deleted.Deleting
15、Rows Based on Another TableUse subqueries in DELETE statements to remove rows from a table based on values from another table:DELETE FROM employeesWHERE department_id = (SELECT department_id FROM departments WHERE department_name LIKE %Public%);1 row deleted.TRUNCATE StatementRemoves all rows from a
16、 table, leaving the table empty and the table structure intactIs a data definition language (DDL) statement rather than a DML statement; cannot easily be undoneSyntax:Example:TRUNCATE TABLE table_name;TRUNCATE TABLE copy_emp;INSERT INTO (SELECT employee_id, last_name, email, hire_date, job_id, salar
17、y, department_id FROM employees WHERE department_id = 50) VALUES (99999, Taylor, DTAYLOR, TO_DATE(07-JUN-99, DD-MON-RR), ST_CLERK, 5000, 50);1 row created.Using a Subquery in an INSERT StatementUsing a Subquery in an INSERT StatementVerify the results:SELECT employee_id, last_name, email, hire_date,
18、 job_id, salary, department_idFROM employeesWHERE department_id = 50;Database TransactionsA database transaction consists of one of the following:DML statements that constitute one consistent change to the dataOne DDL statementOne data control language (DCL) statementDatabase TransactionsBegin when
19、the first DML SQL statement is executed End with one of the following events:A COMMIT or ROLLBACK statement is issued.A DDL or DCL statement executes (automatic commit).The user exits iSQL*Plus.The system crashes.Advantages of COMMIT and ROLLBACK StatementsWith COMMIT and ROLLBACK statements, you ca
20、n: Ensure data consistencyPreview data changes before making changes permanentGroup logically related operationsControlling TransactionsSAVEPOINT BSAVEPOINT ADELETEINSERTUPDATEINSERTCOMMITTimeTransactionROLLBACK to SAVEPOINT BROLLBACK to SAVEPOINT AROLLBACKUPDATE.SAVEPOINT update_done;Savepoint crea
21、ted.INSERT.ROLLBACK TO update_done;Rollback complete.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.Implicit Transaction ProcessingAn automatic commit occurs under the fol
22、lowing circumstances:DDL statement 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.State of the Data Before COMMIT or ROLLBACKThe previous s
23、tate 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 in the affected rows.State of t
24、he 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.COMMIT;Commit complete.Commi
25、tting DataMake the changes:Commit the changes:DELETE FROM employeesWHERE employee_id = 99999;1 row deleted.INSERT INTO departments VALUES (290, Corporate Tax, NULL, 1700);1 row created.DELETE FROM copy_emp;22 rows deleted.ROLLBACK ;Rollback complete.State of the Data After ROLLBACKDiscard all pendin
26、g changes by using the ROLLBACK statement:Data changes are undone.Previous state of the data is restored.Locks on the affected rows are released.State of the Data After ROLLBACKDELETE FROM test;25,000 rows deleted.ROLLBACK;Rollback complete.DELETE FROM test WHERE id = 100;1 row deleted.SELECT * FROM
27、 test WHERE id = 100;No rows selected.COMMIT;Commit complete.Statement-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 explicitly by executing a COMMIT or ROLLBACK statement.Read ConsistencyRead consistency guarantees a consistent view of the data at all times.Ch
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026安徽合肥一六八新店花园学校小学部教师招聘笔试备考试题及答案解析
- 2026贵州安顺市黄果树旅游区龙宫镇人民政府招聘公益性岗位人员1人笔试备考试题及答案解析
- 2026海南三亚市吉阳区机关事业单位编外聘用人员、村(社区)工作人员储备库(考核)招聘200人(第1号)笔试备考题库及答案解析
- 2026广东深圳市宝安区西乡桃源居幼儿园(集团)御龙居幼儿园招聘公办幼儿园工作人员1人笔试备考题库及答案解析
- 2026四川天府银行攀枝花分行春季社会招聘笔试备考题库及答案解析
- 2026福建福州铜盘中学招聘代课教师的1人笔试备考试题及答案解析
- 2026中国科学院近代物理研究所劳务派遣人员招聘2人(甘肃)笔试备考试题及答案解析
- 2025年环保集团笔试题库及答案
- 2025年铁路局校园招聘笔试及答案
- 2025年民航华北空管局笔试题及答案
- 2026年上海市宝山区初三上学期一模化学试卷和答案及评分标准
- 内蒙古赤峰市松山区2025-2026学年高一上学期期末数学试题(含答案)
- 2026年官方标准版离婚协议书
- 未来五年造纸及纸制品企业数字化转型与智慧升级战略分析研究报告
- 2025年贵州省高考化学试卷真题(含答案及解析)
- 紧固件 弹簧垫圈 标准型(2025版)
- 2025年数字印刷技术应用项目可行性研究报告
- 2024年第41届全国中学生竞赛预赛物理试题(解析版)
- 民间借贷合同规范示范文本
- 2025年高考真题-生物(浙江卷) 含答案
- DB34∕T 1718-2021 冶金起重机安全检验规程
评论
0/150
提交评论