




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 上海市奉賢區(qū)衛(wèi)生健康系統(tǒng)事業(yè)單位招聘考試真題2024
- 德江縣“特崗計(jì)劃”招聘考試真題2024
- 2025至2030新鮮蔬菜批發(fā)產(chǎn)業(yè)深度調(diào)研及前景趨勢與投資報(bào)告
- 2025至2030推土機(jī)行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報(bào)告
- 企業(yè)干部考試題庫及答案
- 2025至2030市場管理行業(yè)深度分析及有效策略與實(shí)施路徑評(píng)估報(bào)告
- 優(yōu)化住院環(huán)境與流程全程全面改善患者體驗(yàn)
- 有色金屬行業(yè)資源循環(huán)利用產(chǎn)業(yè)鏈2025年產(chǎn)業(yè)鏈產(chǎn)業(yè)鏈產(chǎn)業(yè)鏈產(chǎn)業(yè)鏈產(chǎn)業(yè)鏈整合報(bào)告
- 門面租房合同
- 2024年江蘇省蘇州吳中學(xué)區(qū)七上數(shù)學(xué)期末統(tǒng)考試題含解析
- 70歲以上老人考駕照,三力測試題庫答案
- 文件簽收回執(zhí)單
- DB4503T 0041-2022 桂林雜交鱘陸基生態(tài)養(yǎng)殖技術(shù)規(guī)程
- GB/T 33592-2017分布式電源并網(wǎng)運(yùn)行控制規(guī)范
- GB/T 28046.4-2011道路車輛電氣及電子設(shè)備的環(huán)境條件和試驗(yàn)第4部分:氣候負(fù)荷
- 綠化工程施工技術(shù)方案及措施(可編輯)
- 會(huì)計(jì)知識(shí)競賽題庫附答案2021
- 廠房鋼筋混凝土地坪板工程施工方案
- 項(xiàng)目延期申請(qǐng)表(樣本)
- 固井工藝技術(shù)培訓(xùn)教學(xué)課件(77p)
- 入團(tuán)志愿書(2016版本)(可編輯打印標(biāo)準(zhǔn)A4) (1)
評(píng)論
0/150
提交評(píng)論