




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國氣動密封圈行業(yè)投資前景及策略咨詢報告
- 2025至2030年中國歐式工業(yè)門可調(diào)鉸鏈?zhǔn)袌霈F(xiàn)狀分析及前景預(yù)測報告
- 2025至2030年中國櫻桃水果汁行業(yè)發(fā)展研究報告
- 2025至2030年中國桐木包裝盒市場分析及競爭策略研究報告
- 2025至2030年中國柳編箱行業(yè)投資前景及策略咨詢報告
- 2025至2030年中國果品箱行業(yè)發(fā)展研究報告
- 2025至2030年中國條幾市場分析及競爭策略研究報告
- 2025至2030年中國機(jī)板紙數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國普通整流二極管行業(yè)投資前景及策略咨詢報告
- 2025至2030年中國時裝褲行業(yè)發(fā)展研究報告
- 【“三曹”詩歌風(fēng)格差異研究7800字(論文)】
- 任務(wù)三 輔助出行的出行者信息系統(tǒng)
- 綠色中國智慧樹知到課后章節(jié)答案2023年下華東理工大學(xué)
- 第16課-兩次鴉片戰(zhàn)爭說課稿(教學(xué)課件)高中歷史人教統(tǒng)編版中外歷史綱要上冊
- ERCP術(shù)前知情同意書
- 建筑工程計(jì)量與計(jì)價高職PPT完整全套教學(xué)課件
- 看板管理-精益生產(chǎn)
- 菜鳥WMS(大寶)操作手冊 (修復(fù)的)
- 葫蘆島蘭家溝礦業(yè)有限公司(鉬礦)礦山地質(zhì)環(huán)境保護(hù)與土地復(fù)墾方案
- nc600產(chǎn)品說明書串口服務(wù)器使用
- 2022年07月甘肅張掖市引進(jìn)高層次急需人才23人筆試題庫含答案解析
評論
0/150
提交評論