Beruflich Dokumente
Kultur Dokumente
Objectives
After completing this lesson, you should be able to: Manage data by using SQL Identify and administer PL/SQL objects Describe triggers and triggering events Monitor and resolve locking conflicts
9-2
SQL> INSERT INTO employees VALUES 2 (9999,'Bob','Builder','bob@abc.net',NULL,SYSDATE, 3 'IT_PROG',NULL,NULL,100,90); 1 row created. SQL> UPDATE employees SET SALARY=6000 2 WHERE EMPLOYEE_ID = 9999; 1 row updated. SQL> DELETE from employees 2 WHERE EMPLOYEE_ID = 9999;
1 row deleted.
9-3
INSERT Command
Create one row at a time. Insert many rows from another table.
SQL> INSERT INTO emp VALUES (9999,'Bob','Builder',100,SYSDATE, 10000);
SQL> INSERT INTO emp select employee_id , last_name, first_name,department_id, hire_date, salary FROM HR.employees where department_id =30;
6 rows created.
9-4
UPDATE Command
6 rows updated.
9-5
DELETE Command
6 rows deleted.
9-6
MERGE Command
Use the MERGE command to perform both INSERT and UPDATE in a single command.
MERGE INTO EMP a USING (Select * FROM HR.employees) b ON (a.emp_no =b. employee_id ) WHEN MATCHED THEN UPDATE SET a.salary =b.salary WHEN NOT MATCHED THEN INSERT (emp_no, last_name, first_name, dept_no, hire_date, salary) VALUES (employee_id,last_name, first_name, department_id, hire_date, salary);
9-7
To finish a transaction: COMMIT makes the change permanent ROLLBACK undoes the change
SQL> COMMIT; Commit complete.
9-9
PL/SQL
Oracles Procedural Language extension to SQL (PL/SQL) is a fourth-generation programming language (4GL). It provides: Procedural extensions to SQL Portability across platforms and products Higher level of security and data-integrity protection Support for object-oriented programming
9 - 10
Database administrators should be able to: Identify problem PL/SQL objects Recommend the appropriate use of PL/SQL Load PL/SQL objects into the database Assist PL/SQL developers in troubleshooting Use supplied packages for administrative and database maintenance tasks
9 - 11
PL/SQL Objects
There are many types of PL/SQL database objects: Package Package body Type body Procedure Function Trigger
9 - 12
Functions
9 - 13
Procedures
Are used to perform specific actions Pass values in and out by using an argument list Can be called from within other programs using the CALL command
9 - 14
Packages
Packages are collections of functions and procedures. Each package should consist of two objects: Package specification Package body
Package specification
9 - 15
9 - 16
Built-in Packages
Oracle Database comes with several built-in PL/SQL packages that provide:
Administration and maintenance utilities Extended functionality
9 - 17
Triggers
9 - 18
Triggering Events
Event Type
DML DDL Database
Examples of Events
INSERT, UPDATE, DELETE
CREATE, DROP, ALTER, GRANT, REVOKE, RENAME LOGON, LOGOFF, STARTUP, SHUTDOWN, SERVERERROR, SUSPEND
9 - 19
Locks
Prevent multiple sessions from changing the same data at the same time Are automatically obtained at the lowest possible level for a given statement Do not escalate
Transaction 1
SQL> UPDATE employees 2 SET salary=salary+100 3 WHERE employee_id=100;
Transaction 2
SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id=100;
9 - 20
Locking Mechanism
Automatic queue management Locks held until the transaction ends (with the COMMIT or ROLLBACK operation)
Transaction 1
SQL> UPDATE employees 2 SET salary=salary+100 3 WHERE employee_id=100;
Transaction 2
SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id=101;
9 - 21
Data Concurrency
Time:
Transaction 1
Transaction 2 09:00:00
Transaction 3
... Transaction x
...
UPDATE hr.employees SET salary=salary+100 WHERE employee_id=xxx;
9 - 22
Like any request for a lock, manual lock statements wait until all sessions that either already have locks or have previously requested locks release their locks. The LOCK command accepts a special argument that controls the waiting behaviour NOWAIT. NOWAIT returns control to you immediately if the specified table is already locked by another session. It is usually not necessary to manually lock objects. The automatic locking mechanism provides the data concurrency needed for most applications. Oracle recommends that you avoid using manual locks, especially when developing applications. Severe performance issues often occur from unnecessarily high locking levels
Copyright 2008, Oracle. All rights reserved.
9 - 25
DML Locks
Transaction 1
SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id= 107; 1 row updated.
Transaction 2
SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id= 106; 1 row updated.
Each DML transaction must acquire two locks: EXCLUSIVE row lock on the row or rows being updated ROW EXCLUSIVE table-level lock on the table containing the rows
9 - 26
Enqueue Mechanism
The enqueue mechanism keeps track of: Sessions waiting for locks Requested lock mode Order in which sessions requested the lock
9 - 27
Lock Conflicts
Transaction 1 Time Transaction 2
UPDATE employees SET salary=salary+100 WHERE employee_id=101; 1 row updated. SELECT sum(salary) FROM employees; SUM(SALARY) ----------692634 Many selects, inserts, updates, and deletes during the last 7.5 hours, but no commits or rollbacks!
UPDATE employees SET 9:00:00 salary=salary+100 WHERE employee_id=100; 1 row updated. UPDATE employees SET COMMISION_PCT=2 WHERE employee_id=101; Session waits enqueued due to lock conflict. Session still waiting! 9:00:05
16:30:00
16:30:01
commit;
9 - 28
9 - 29
Click the Session ID link to view information about the locking session, including the actual SQL statement.
9 - 30
To resolve a lock conflict: Have the session holding the lock commit or roll back Terminate the session holding the lock (in an emergency)
9 - 31
SQL statements can be used to determine the blocking session and kill it.
SQL> select sid, serial#, username from v$session where sid in (select blocking_session from v$session)
Result:
9 - 32
Deadlocks
Transaction 1
Transaction 2
UPDATE employees SET salary = salary x 1.1 WHERE employee_id = 1000; UPDATE employees SET salary = salary x 1.1 WHERE employee_id = 2000;
9:00
UPDATE employees SET manager = 1342 WHERE employee_id = 2000; UPDATE employees SET manager = 1342 WHERE employee_id = 1000;
9:15
9:16
9 - 33
Summary
In this lesson, you should have learned how to: Manage data by using SQL Identify and administer PL/SQL objects Describe triggers and triggering events Monitor and resolve locking conflicts
9 - 34
9 - 35