




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 濮陽科技職業(yè)學(xué)院《大數(shù)據(jù)統(tǒng)計模型實驗》2023-2024學(xué)年第二學(xué)期期末試卷
- 喀什大學(xué)《數(shù)字影像工程》2023-2024學(xué)年第二學(xué)期期末試卷
- 安徽工業(yè)經(jīng)濟職業(yè)技術(shù)學(xué)院《流行音樂經(jīng)典作品分析(2)》2023-2024學(xué)年第二學(xué)期期末試卷
- 公章的管理制度
- 公司章程中內(nèi)控的內(nèi)容
- 公共交通線路調(diào)整管理制度
- 工程施工隊每周進度計劃表格
- 頁巖磚砌體施工方案
- 【2025年二手房行業(yè)資訊:深圳周錄1812套再創(chuàng)新高】
- 江西省上饒市2024-2025學(xué)年高二上學(xué)期1月期末英語試題【含答案】
- 防火門安全生產(chǎn)管理制度
- 電工教學(xué)大綱與教學(xué)計劃
- 頸源性頭痛演示課件
- 超市消防應(yīng)急疏散預(yù)案
- 數(shù)字媒體技術(shù)基礎(chǔ)實訓(xùn)指導(dǎo)
- 醫(yī)院基建科招聘筆試題目
- 消防安全管理制度完整版完整版
- 青少年毒品預(yù)防教育-小學(xué)版
- 《Unit2Myfavoriteseason》教學(xué)設(shè)計課件
- 學(xué)前教育-大班幼兒使用電子產(chǎn)品現(xiàn)狀對其日常影響的研究調(diào)查
- 解除終止勞動合同備案登記表
評論
0/150
提交評論