Download presentation
Presentation is loading. Please wait.
1
九.操作数据-DML语句 Schedule: Timing Topic 40 minutes Lecture
30 minutes Practice 70 minutes Total
2
目标 结束本节课后,应当达到如下目标: 描述每个DML语句 向一个表中插入数据 更新一个表中数据行 从一个表中删除行 控制事务
Lesson Aim In this lesson, you will learn how to insert rows into a table, update existing rows in a table, and delete existing rows from a table. You will also learn how to control transactions with the COMMIT, SAVEPOINT, and ROLLBACK statements.
3
数据操作语言 DML语句在下列情况下发生: 一个事务由一组构成一个逻辑操作的DML语句组成. 往一个表中增加新行 更改一个表中现有的行
从一个表中删除掉现有的行 一个事务由一组构成一个逻辑操作的DML语句组成. Data Manipulation Language Data manipulation language (DML) is a core part of SQL. When you want to add, update, or delete data in the database, you execute a DML statement. A collection of DML statements that form a logical unit of work is called a transaction. Consider a banking database. When a bank customer transfers money from a savings account to a checking account, the transaction might consist of three separate operations: decrease the savings account, increase the checking account, and record the transaction in the transaction journal. The Oracle Server must guarantee that all three SQL statements are performed to maintain the accounts in proper balance. When something prevents one of the statements in the transaction from executing, the other statements of the transaction must be undone. Instructor Note DML statements can be issued directly in SQL*Plus, performed automatically by tools such as Oracle Developer or programmed with tools such as the 3GL precompilers. Every table has INSERT, UPDATE, and DELETE privileges associated with it. These privileges are automatically granted to the creator of the table, but in general they must be explicitly granted to other users. Starting with Oracle 7.2, you can place a subquery in the place of the table name in an UPDATE statement, essentially the same way a view is used.
4
向一个表中增加新行 “…向 DEPT 表中 增加新行…” 新行 DEPT DEPT 50 DEVELOPMENT DETROIT
DEPTNO DNAME LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON DEPT DEPTNO DNAME LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON Adding a New Row to a Table The slide graphic adds a new department to the DEPT table. 50 DEVELOPMENT DETROIT
5
INSERT语句 使用 INSERT 语句向表中增加新行. 使用这种方法只能一次插入一行数据.
INSERT INTO table [(column [, column...])] VALUES (value [, value...]); Adding a New Row to a Table (continued) You can add new rows to a table by issuing the INSERT statement. In the syntax: table is the name of the table column is the name of the column in the table to populate value is the corresponding value for the column Note: This statement with the VALUES clause adds only one row at a time to a table.
6
插入新行 插入包含每一个列值的新行. 按缺省顺序列出表中所有的列值. 列出 INSERT 子句中所有的列. 日期值和字符值要用单引号括起来.
SQL> INSERT INTO dept (deptno, dname, loc) 2 VALUES (50, 'DEVELOPMENT', 'DETROIT'); 1 row created. Adding a New Row to a Table (continued) Because you can insert a new row that contains values for each column, the column list is not required in the INSERT clause. However, if you do not use the column list, the values must be listed according to the default order of the columns in the table. SQL> DESCRIBE dept Name Null? Type DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13) For clarity, use the column list in the INSERT clause. Enclose character and date values within single quotation marks; do not enclose numeric values within single quotation marks. Instructor Note Number values should not be enclosed in single quotes, because implicit conversion may take place for numeric values assigned to NUMBER datatype columns if single quotes are included.
7
插入带有空值的行 省略的方法: 从列的链表忽略有空值的列. 明确的方法: 指定 NULL 关键字.
SQL> INSERT INTO dept (deptno, dname ) 2 VALUES (60, 'MIS'); 1 row created. 明确的方法: 指定 NULL 关键字. Methods for Inserting Null Values Be sure that the targeted column allows null values by verifying the Null? status from the SQL*Plus DESCRIBE command. The Oracle Server automatically enforces all datatypes, data ranges, and data integrity constraints. Any column that is not listed explicitly obtains a null value in the new row. Instructor Note Common errors that can occur during user input: Mandatory value missing for a NOT NULL column Duplicate value violates uniqueness constraint Foreign key constraint violated CHECK constraint violated Datatype mismatch Value too wide to fit in column SQL> INSERT INTO dept 2 VALUES (70, 'FINANCE', NULL); 1 row created.
8
插入特殊的值 SYSDATE函数取出当前的日期的时间. EMPNO ENAME JOB HIREDATE COMM
SQL> INSERT INTO emp (empno, ename, job, 2 mgr, hiredate, sal, comm, 3 deptno) 4 VALUES (7196, 'GREEN', 'SALESMAN', , SYSDATE, 2000, NULL, 6 10); 1 row created. Inserting Special Values by Using SQL Functions You can use pseudocolumns to enter special values in your table. The slide example records information for employee Green in the EMP table. It supplies the current date and time in the HIREDATE column. It uses the SYSDATE function for current date and time. You can also use the USER function when inserting rows in a table. The USER function records the current username. Confirming Additions to the Table SQL> SELECT empno, ename, job, hiredate, comm 2 FROM emp 3 WHERE empno = 7196; EMPNO ENAME JOB HIREDATE COMM 7196 GREEN SALESMAN 01-DEC-97
9
插入特殊的日期值 增加一个新的员工. 检验结果. SQL> INSERT INTO emp
2 VALUES (2296,'AROMANO','SALESMAN',7782, TO_DATE('FEB 3, 1997', 'MON DD, YYYY'), , NULL, 10); 1 row created. 检验结果. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 2296 AROMANO SALESMAN FEB Inserting Specific Date and Time Values The format DD-MON-YY is usually used to insert a date value. With this format, recall that the century defaults to the current century. Because the date also contains time information, the default time is midnight (00:00:00). If a date must be entered in a format other than the default format—for example, another century and/or a specific time—you must use the TO_DATE function. The example on the slide records information for employee Aromano in the EMP table. It sets the HIREDATE column to be February 3, 1997. If the RR format is set, the century may not be the current one.
10
从另一个表中拷贝行 创建带有子查询的 INSERT语句。 不要使用 VALUES 子句. 子查询中的列要与INSERT子句中的列相匹配.
SQL> INSERT INTO managers(id, name, salary, hiredate) SELECT empno, ename, sal, hiredate FROM emp WHERE job = 'MANAGER'; 3 rows created. Copying Rows from Another Table You can use the INSERT statement to add rows to a table where the values are derived from existing tables. In place of the VALUES clause, you use a subquery. Syntax INSERT INTO table [ column (, column) ] subquery; where: table is the table name column is the name of the column in the table to populate subquery is the subquery that returns rows into the table For more information, see Oracle Server SQL Reference, Release 8, “SELECT,” Subqueries section. The number of columns and their datatypes in the column list of the INSERT clause must match the number of values and their datatypes in the subquery. Instructor Note Please run the script lab9_12.sql to create the managers table. Do not get into too many details on copying rows from another table.
11
改变一个表中的数据 “…更改EMP 表中的一行数据…” EMP EMP EMPNO ENAME JOB ... DEPTNO
7839 KING PRESIDENT 7698 BLAKE MANAGER 7782 CLARK MANAGER 7566 JONES MANAGER ... “…更改EMP 表中的一行数据…” 20 EMP EMPNO ENAME JOB DEPTNO 7839 KING PRESIDENT 7698 BLAKE MANAGER 7782 CLARK MANAGER 7566 JONES MANAGER ... Changing Data in a Table The slide graphic changes the department number for Clark from 10 to 20.
12
UPDATE 语句 使用 UPDATE 语句更改现有的行. 如果需要,可以一次更改多行. UPDATE table
SET column = value [, column = value, ...] [WHERE condition]; Updating Rows You can modify existing rows by using the UPDATE statement. In the above syntax: table is the name of the table column is the name of the column in the table to populate value is the corresponding value or subquery for the column condition identifies the rows to be updated and is composed of column names expressions, constants, subqueries, and comparison operators Confirm the update operation by querying the table to display the updated rows. For more information, see Oracle Server SQL Reference, Release 8, “UPDATE.” Note: In general, use the primary key to identify a single row. Using other columns may unexpectedly cause several rows to be updated. For example, identifying a single row in the EMP table by name is dangerous because more than one employee may have the same name. Instructor Note Demo: l9sel.sql, l9upd.sql Purpose: To illustrate displaying the initial state of data, performing updates, and viewing results.
13
更改表中的行 使用 WHERE 子句来指定要修改的行. 如果忽略WHERE子句,那么句子中所有的值都将被更改.
SQL> UPDATE emp 2 SET deptno = 20 3 WHERE empno = 7782; 1 row updated. Updating Rows (continued) The UPDATE statement modifies specific rows, if the WHERE clause is specified. The slide example transfers employee 7782 (Clark) to department 20. If you omit the WHERE clause, all the rows in the table are modified. Note: The EMPLOYEE table has the same data as the EMP table. SQL> UPDATE employee 2 SET deptno = 20; 14 rows updated. SQL> SELECT ename, deptno 2 FROM employee; ENAME DEPTNO KING BLAKE CLARK JONES MARTIN ALLEN TURNER ... 14 rows selected.
14
用多列子查询进行修改 更改第 7698号雇员的工作和部门,以与第 7499号雇员进行匹配. SQL> UPDATE emp
2 SET (job, deptno) = (SELECT job, deptno FROM emp WHERE empno = 7499) 6 WHERE empno = 7698; 1 row updated. Updating Rows with a Multiple-Column Subquery Multiple-column subqueries can be implemented in the SET clause of an UPDATE statement. Syntax Instructor Note It may be worth showing participants that the results would be the same for the example on the slide if two different subqueries were used in the SET clause as illustrated below: SQL> UPDATE emp 2 SET job = (SELECT job FROM emp WHERE empno = 7499), 4 deptno = (SELECT deptno FROM dept WHERE dname = 'SALES') 6 WHERE empno = 7698; UPDATE table SET (column, column, ...) = (SELECT column, column, ... FROM table WHERE condition) WHERE condition;
15
对基于另一个表的行进行更改 在 UPDATE语句中,使用子查询来更进行更改,更改的数据与其它表中的数据有关.
SQL> UPDATE employee 2 SET deptno = (SELECT deptno 3 FROM emp 4 WHERE empno = 7788) 5 WHERE job = (SELECT job 6 FROM emp 7 WHERE empno = 7788); 2 rows updated. Updating Rows Based on Another Table You can use subqueries in UPDATE statements to update rows in a table. The example on the slide updates the EMPLOYEE table based on the values from the EMP table. It changes the department number of all employees with employee 7788’s job title to employee 7788’s current department number.
16
从一个表中移去一行 “…从一个表中删去一行…” DEPT DEPT DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 50 DEVELOPMENT DETROIT 60 MIS ... “…从一个表中删去一行…” DEPT DEPTNO DNAME LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 60 MIS ... Removing a Row from a Table The slide graphic removes the DEVELOPMENT department from the DEPT table (assuming that there are no constraints defined on the DEPT table). Instructor Note After all the rows have been eliminated with the DELETE statement, only the data structure of the table remains. A more efficient method of emptying a table is with the TRUNCATE statement. You can use the TRUNCATE statement to quickly remove all rows from a table or cluster. Removing rows with the TRUNCATE statement is faster than removing them with the DELETE statement for the following reasons: The TRUNCATE statement is a data definition language (DDL) statement and generates no rollback information. It will be covered in a subsequent lesson. Truncating a table does not fire the DELETE triggers of the table. If the table is the parent of a referential integrity constraint, you cannot truncate the table. Disable the constraint before issuing the TRUNCATE statement.
17
DELETE语句 可以使用 DELETE 语句从表中删去现存的行. DELETE [FROM] table
[WHERE condition]; Deleting Rows You can remove existing rows by using the DELETE statement. In the syntax: table is the table name condition identifies the rows to be deleted and is composed of column names, expressions, constants, subqueries, and comparison operators For more information, see Oracle Server SQL Reference, Release 8, “DELETE.” Instructor Note The DELETE statement does not ask for confirmation. However, the delete operation is not made permanent until the data transaction is committed. Therefore, you can undo the operation with the ROLLBACK statement if you make a mistake.
18
从一个表中删去行 使用 WHERE 子句以指定哪些行应当被删去. 如果忽略WHERE 子句,那么表中所有的数据.
SQL> DELETE FROM department 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. Deleting Rows (continued) You can delete specific rows by specifying the WHERE clause in the DELETE statement. The slide example deletes the DEVELOPMENT department from the DEPARTMENT table. You can confirm the delete operation by displaying the deleted rows using the SELECT statement. Example Remove all employees who started after January 1, 1997. If you omit the WHERE clause, all rows in the table are deleted. The second example on the slide deletes all the rows from the DEPARTMENT table because no WHERE clause has been specified. Note: The DEPARTMENT table has the same data as the DEPT table. SQL> DELETE FROM department; 4 rows deleted. SQL> SELECT * 2 FROM department 3 WHERE dname = 'DEVELOPMENT'; no rows selected. SQL> DELETE FROM emp 2 WHERE hiredate > TO_DATE(' ', 'DD.MM.YYYY'); 1 row deleted.
19
参照另一个表来删除行 使用子查询,使得 DELETE 语句能从另一个表中删除某些行.
SQL> DELETE FROM employee 2 WHERE deptno = (SELECT deptno FROM dept WHERE dname ='SALES'); 6 rows deleted. Deleting Rows Based on Another Table You can use subqueries to delete rows from a table based on values from another table. The example on the slide deletes all the employees who are in department 30. The subquery searches the DEPT table to find the department number for the SALES department. The subquery then feeds the department number to the main query, which deletes rows of data from the EMPLOYEE table based on this department number.
20
数据库事务 要么全部完成,要么全部废弃的操作集合。 一个事务可以包含下列语句: 对数据做出一致性修改的DML语句。 一个 DDL 语句
一个 DCL语句 Database Transactions The Oracle Server ensures data consistency based on transactions. Transactions give you more flexibility and control when changing data, and they ensure data consistency in the event of user process failure or system failure. Transactions consist of DML statements that make up one consistent change to the data. For example, a transfer of funds between two accounts should include the debit to one account and the credit to another account in the same amount. Both actions should either fail or succeed together. The credit should not be committed without the debit. Transaction Types
21
数据库事务 以第一个可执行的 SQL 语句开始。 以下列情况结束: 执行COMMIT 或者 ROLLBACK 语句
执行DDL或者 DCL语句 用户退出 系统崩溃 When Does a Transaction Start and End? A transaction begins when the first executable SQL statement is encountered and terminates when one of the following occurs: A COMMIT or ROLLBACK statement is issued A DDL statement, such as CREATE, is issued A DCL statement is issued The user exits SQL*Plus A machine fails or the system crashes After one transaction ends, the next executable SQL statement automatically starts the next transaction. A DDL statement or a DCL statement is automatically committed and therefore implicitly ends a transaction.
22
COMMIT和ROLLBACK语句的优点
保证数据的一致性 在数据永久改变之前,检查数据的改变 对逻辑相关的操作进行分组
23
控制事务 事务 INSERT INSERT UPDATE INSERT DELETE DELETE INSERT UPDATE 提交
回滚 INSERT 回滚到保存点 A INSERT UPDATE INSERT 回滚到保存点 B DELETE DELETE INSERT UPDATE 提交 保存点A 保存点B Explicit Transaction Control Statements You can control the logic of transactions by using the COMMIT, SAVEPOINT, and ROLLBACK statements. Note: SAVEPOINT is not ANSI standard SQL. Instructor Note Savepoints are not schema objects and cannot be referenced in the data dictionary.
24
隐式事务处理 在下列环境下,一个自动提交发生: 当 SQL*Plus中断或者系统失败时自动进行回滚. 处理DDL语句 处理DCL 语句
从 SQL*Plus中退出, 而没有明确指定COMMIT或者 ROLLBACK 当 SQL*Plus中断或者系统失败时自动进行回滚. Implicit Transaction Processing Note: A third command is available in SQL*Plus. The AUTOCOMMIT command can be toggled ON or OFF. If set to ON, each individual DML statement is committed as soon as it is executed. You cannot roll back the changes. If set to OFF, COMMIT can be issued explicitly. Also, COMMIT is issued when a DDL statement is issued or when you exit from SQL*Plus. System Failures When a transaction is interrupted by a system failure, the entire transaction is automatically rolled back. This prevents the error from causing unwanted changes to the data and returns the tables to their state at the time of the last commit. In this way, the Oracle Server protects the integrity of the tables.
25
在COMMIT 或ROLLBACK之前的数据状态
之前的状态可以被恢复. 当前的用户可以用SELECT语句来查看DML操作后的结果. 其它用户看不到当前用户使用 DML语句进行数据操纵的结果. 产生改变的数据被加锁,其它用户不能改变这些行. Committing Changes Every data change made during the transaction is temporary until the transaction is committed. State of the data before COMMIT or ROLLBACK is issued: Data manipulation operations primarily affect the database buffer; therefore, the previous state of the data can be recovered. The current user can review the results of the data manipulation operations by querying the tables. Other users cannot view the results of the data manipulation operations made by the current user. The Oracle Server institutes read consistency to ensure that each user sees data as it existed at the last commit. The affected rows are locked; other users cannot change the data in the affected rows. Instructor Note With the Oracle Server, data changes may actually be written to the database files before COMMIT, but they are still only temporary. If a number of users are making changes simultaneously to the same table, then each user sees only his or her changes until other users commit their changes. Other users see data as it is committed in the database (in other words, before changes). By default, the Oracle Server has row-level locking. It is possible to alter the default locking mechanism.
26
在 COMMIT之后的数据状态 数据的改变将被永久的反应到数据库中去. 事务前面的数据状态将被永久地丢弃. 所有的用户可以查看结果.
加在产生改变的行上的数据被解锁; 这些行对于其他用户是可用的. 所有的保存点被释放. Committing Changes (continued) Make all pending changes permanent by using the COMMIT statement. Following a COMMIT: State of the data after a COMMIT is issued: Data changes are written to the database. The previous state of the data is permanently lost. All users can view the results of the transaction. The locks on the affected rows are released; the rows are now available for other users to perform new data changes. All savepoints are erased.
27
提交数据 产生改变. Commit the changes. SQL> UPDATE emp 2 SET deptno = 10
3 WHERE empno = 7782; 1 row updated. Commit the changes. SQL> COMMIT; Commit complete. Committing Changes (continued) The slide example updates the EMP table and sets the department number for employee 7782 (Clark) to 10. It then makes the change permanent by issuing the COMMIT statement. Example Create a new ADVERTISING department with at least one employee. Make the data change permanent. Instructor Note Use this example to explain how COMMIT ensures that two related operations should occur together or not at all. In this case, COMMIT prevents empty departments from being created. Instructor Note (for page 9-32) Please run the script lab9_32.sql to create the test table and insert data into the table. SQL> INSERT INTO department(deptno, dname, loc) 2 VALUES (50, 'ADVERTISING', 'MIAMI'); 1 row created. SQL> UPDATE employee 2 SET deptno = 50 3 WHERE empno = 7876; 1 row updated. SQL> COMMIT; Commit complete.
28
回滚后的数据状态 使用 ROLLBACK语句丢弃所有的数据改变. 数据的改变失效. 事务之前的数据状态改变. 在改变行的上的锁被释放.
SQL> DELETE FROM employee; 14 rows deleted. SQL> ROLLBACK; Rollback complete. Rolling Back Changes Discard all pending changes by using the ROLLBACK statement. Following a ROLLBACK: Data changes are undone. The previous state of the data is restored. The locks on the affected rows are released. Example While attempting to remove a record from the TEST table, you can accidentally empty the table. You can correct the mistake, reissue the proper statement, and make the data change permanent. SQL> DELETE FROM test; 25,000 rows deleted. SQL> ROLLBACK; Rollback complete. SQL> DELETE FROM test 2 WHERE id = 100; 1 row deleted. SQL> SELECT * 2 FROM test 3 WHERE id = 100; No rows selected. SQL> COMMIT; Commit complete.
29
回滚到某个标记 使用 SAVEPOINT语句在当前事务中产生一个标记. 使用ROLLBACK TO SAVEPOINT语句回滚到那个标记.
SQL> UPDATE... SQL> SAVEPOINT update_done; Savepoint created. SQL> INSERT... SQL> ROLLBACK TO update_done; Rollback complete. Rolling Back Changes to a Savepoint You can create a marker in the current transaction by using the SAVEPOINT statement. The transaction therefore can be divided into smaller sections. You can then discard pending changes up to that marker by using the ROLLBACK TO SAVEPOINT statement. If you create a second savepoint with the same name as an earlier savepoint, the earlier savepoint is deleted. Instructor Note Savepoints are especially useful in PL/SQL or a 3GL program in which recent changes can be undone conditionally based on runtime conditions.
30
锁定 Oracle 锁: 在并发事务之间,阻止可能产生的破坏性相互影响。 不需要用户进行干预,自动使用。 在事务期间使用,事务结束时释放。
有两种基本的模式: 排它 共享 What Are Locks? Locks are mechanisms that prevent destructive interaction between transactions accessing the same resource, either a user object (such as tables or rows) or system objects not visible to users (such as shared data structures and data dictionary rows). How Oracle Locks Data Locking in an Oracle database is fully automatic and requires no user action. Implicit locking occurs for all SQL statements except SELECT. The Oracle default locking mechanism automatically uses the lowest applicable level of restrictiveness, thus providing the highest degree of concurrency and maximum data integrity. Oracle also allows the user to lock data manually. Locking Modes Oracle uses two modes of locking in a multiuser database:
31
总结 语句 INSERT UPDATE DELETE COMMIT SAVEPOINT ROLLBACK 描述 向表中增加一个新的行
更改表中现存的行 从表中删除现存的行 提交一个事务 允许回滚到保存点标记 废弃所有未提交的改变 Summary Manipulate data in the Oracle database by using the INSERT, UPDATE, and DELETE statements. Control data changes by using the COMMIT, SAVEPOINT, and ROLLBACK statements. The Oracle Server guarantees a consistent view of data at all times. Locking can be implicit or explicit. Instructor Note (for page 9-37) Please read the Instructor Note on page 9-43. For more information on locking refer to Oracle8i Concepts, Release 8.1.5, “Data Concurrency and Consistency,” and to:
32
作业概览 向表中插入数据行 修改和删除表中的数据行 控制事务 Practice Overview
In this practice, you will add rows to the MY_EMPLOYEE table, update and delete data from the table, and control your transactions.
33
Practice 9 Insert data into the MY_EMPLOYEE table. 1. Run the lab9_1.sql script to build the MY_EMPLOYEE table that will be used for the lab. 2. Describe the structure of the MY_EMPLOYEE table to identify the column names. Name Null? Type ID NOT NULL NUMBER(4) LAST_NAME VARCHAR2(25) FIRST_NAME VARCHAR2(25) USERID VARCHAR2(8) SALARY NUMBER(9,2) 3. Add the first row of data to the MY_EMPLOYEE table from the following sample data. Do not list the columns in the INSERT clause. 4. Populate the MY_EMPLOYEE table with the second row of sample data from the preceding list. This time, list the columns explicitly in the INSERT clause. 5. Confirm your addition to the table. ID LAST_NAME FIRST_NAME USERID SALARY Patel Ralph rpatel Dancs Betty bdancs
34
Practice 9 (continued) 6. Create a script named loademp.sql to load rows into the MY_EMPLOYEE table interactively. Prompt the user for the employee’s id, first name, last name, and salary. Concatenate the first letter of the first name and the first seven characters of the last name to produce the userid. 7. Populate the table with the next two rows of sample data by running the script that you created. 8. Confirm your additions to the table. ID LAST_NAME FIRST_NAME USERID SALARY 1 Patel Ralph rpatel Dancs Betty bdancs Biri Ben bbiri Newman Chad cnewman 9. Make the data additions permanent. Update and delete data in the MY_EMPLOYEE table. 10. Change the last name of employee 3 to Drexler. 11. Change the salary to 1000 for all employees with a salary less than 900. 12. Verify your changes to the table. LAST_NAME SALARY Patel Dancs Drexler Newman 13. Delete Betty Dancs from the MY_EMPLOYEE table. 14. Confirm your changes to the table. ID LAST_NAME FIRST_NAME USERID SALARY Patel Ralph rpatel Drexler Ben bbiri Newman Chad cnewman 1000
35
Practice 9 (continued) 15. Commit all pending changes. Control data transaction to the MY_EMPLOYEE table. 16. Populate the table with the last row of sample data by running the script that you created in step 6. 17. Confirm your addition to the table. ID LAST_NAME FIRST_NAME USERID SALARY Patel Ralph rpatel Drexler Ben bbiri Newman Chad cnewman Ropeburn Audry aropebur 18. Mark an intermediate point in the processing of the transaction. 19. Empty the entire table. 20. Confirm that the table is empty. 21. Discard the most recent DELETE operation without discarding the earlier INSERT operation. 22. Confirm that the new row is still intact. ID LAST_NAME FIRST_NAME USERID SALARY Patel Ralph rpatel Drexler Ben bbiri Newman Chad cnewman Ropeburn Audry aropebur 1550 23. Make the data addition permanent.
36
Instructor Note (for page 9-37)
Demo: l9select.sql Purpose: To illustrate the concept of reader does not lock reader. Login to SQL*PLUS using the teach/oracle account. Login to SQL*PLUS using the teach1/oracle account. Run the l9select.sql script in the teach/oracle account. (This script selects all records from the dept table). Run the l9select.sql script in the teach1/oracle account. (This script selects all records from the dept table). In both the logins, the script executes successfully. This demonstrates the concept: reader does not lock reader. Demo: l9grant.sql, l9update.sql, l9select.sql Purpose: To illustrate the concept of writer does not lock reader. Run the l9grant.sql script in the teach/oracle account. (This script grants SELECT and UPDATE privileges on the dept table to the teach1 account). Run the l9update.sql script in the teach/oracle account. (This script updates the dept table, changing location of the dept 40 to LA. The update places a lock on the dept table). Run the l9select.sql script in the teach/oracle account. (This script selects all records from the dept table. Observe that the location of deptno 40 is changed to LA). Observe that the script executes successfully in the teach1/oracle but the location for deptno 40 is still BOSTON. This demonstrates the concept: writer does not lock reader. Demo: l9update.sql, l9rollback.sql, l9select.sql Purpose: To illustrate the concept of writer locks writer. Run the l9update.sql script in the teach1/oracle account. (The script does not execute because the dept table is locked by the teach/oracle account.) Switch to the teach/oracle account and run the l9rollback.sql script. (This script rolls back the transaction, thereby releasing the lock on the dept table.) Switch to the teach1/oracle account. You see that the l9update.sql script has executed successfully because the lock on the dept table has been released Run the l9select.sql script in the teach1/oracle account. (This script selects all records from the dept table. Observe that the location of deptno 40 is changed to LA.) This demonstrates the concept: writer locks writer.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.