9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Managing the Oracle Instance.
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
9 Copyright © 2004, Oracle. All rights reserved. Managing Data.
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
Creating Triggers.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Locking and concurrency. Overview In multi-user systems, several users may update the same information concurrently – i.e. at the same time. Locking allows.
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
10 Copyright © 2005, Oracle. All rights reserved. Implementing Oracle Database Security.
1 Copyright © 2005, Oracle. All rights reserved. Introduction.
4 Copyright © 2004, Oracle. All rights reserved. Database Interfaces.
Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
1 Copyright © 2004, Oracle. All rights reserved. Introduction.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
20 Copyright © Oracle Corporation, All rights reserved. Oracle9 i Extensions to DML and DDL Statements.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
IT Database Administration SECTION 01. Starting Up and Shutting Down the Database Database Administration Facilities – A number of tools are available.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
7 Copyright © 2005, Oracle. All rights reserved. Managing Undo Data.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
13 Copyright © Oracle Corporation, All rights reserved. Controlling User Access.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
1 Copyright © 2009, Oracle. All rights reserved. Controlling User Access.
10 Copyright © 2004, Oracle..All rights reserved. PL/SQL.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
מימוש מערכות מסדי נתונים (236510) Lecture 2 & 3 : Oracle 12c Database SQL Server Data Concurrency : Transactions and Locking B+ Trees By David Itshak
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
4 Copyright © 2004, Oracle. All rights reserved. Managing the Oracle Instance.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
1 Copyright © 2005, Oracle. All rights reserved. Oracle Database Administration: Overview.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
9 Copyright © 2005, Oracle. All rights reserved. Managing Undo Data.
10 Copyright © 2007, Oracle. All rights reserved. Managing Undo Data.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Controlling User Access
Managing Privileges.
Managing Data and Concurrency
ATS Application Programming: Java Programming
Interacting with the Oracle Server
Introduction To Database Systems
SQL Stored Triggers Presented by: Dr. Samir Tartir
LAB: Web-scale Data Management on a Cloud
Manipulating Data.
مقدمة في قواعد البيانات
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Presentation transcript:

9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency

Copyright © 2007, Oracle. All rights reserved 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

Copyright © 2007, Oracle. All rights reserved Manipulating Data by Using SQL. SQL> INSERT INTO employees VALUES 2 3 'IT_PROG',NULL,NULL,100,90); 1 row created. SQL> UPDATE employees SET SALARY= WHERE EMPLOYEE_ID = 9999; 1 row updated. SQL> DELETE from employees 2 WHERE EMPLOYEE_ID = 9999; 1 row deleted.

Copyright © 2007, Oracle. All rights reserved 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. 1 2

Copyright © 2007, Oracle. All rights reserved UPDATE Command Use the UPDATE command to change zero or more rows of a table. SQL> UPDATE EMP SET SALARY= salary +500 Where emp_no =117; 1 row updated. SQL> UPDATE EMP Set dept_no= (select dept_no from emp where emp_no =117); 6 rows updated. 1 2

Copyright © 2007, Oracle. All rights reserved DELETE Command Use the DELETE command to remove zero or more rows from a table. SQL> DELETE FROM emp WHERE emp_no =9000; 0 rows deleted. SQL> DELETE emp; 6 rows deleted. 1 2

Copyright © 2007, Oracle. All rights reserved 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);

Copyright © 2007, Oracle. All rights reserved MERGE Command Full Notes Page

Copyright © 2007, Oracle. All rights reserved COMMIT and ROLLBACK Commands To finish a transaction: COMMIT makes the change permanent ROLLBACK undoes the change SQL> COMMIT; Commit complete.

Copyright © 2007, Oracle. All rights reserved PL/SQL Oracle’s 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

Copyright © 2007, Oracle. All rights reserved Administering PL/SQL Objects 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

Copyright © 2007, Oracle. All rights reserved PL/SQL Objects There are many types of PL/SQL database objects: Package Package body Type body Procedure Function Trigger

Copyright © 2007, Oracle. All rights reserved Functions

Copyright © 2007, Oracle. All rights reserved 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

Copyright © 2007, Oracle. All rights reserved Packages Packages are collections of functions and procedures. Each package should consist of two objects: Package specification Package body Package specification

Copyright © 2007, Oracle. All rights reserved Package Specification and Body

Copyright © 2007, Oracle. All rights reserved Built-in Packages Oracle Database comes with several built-in PL/SQL packages that provide: –Administration and maintenance utilities –Extended functionality Use the DESCRIBE command to view subprograms. SQL>DESC DBMS_LOCK

Copyright © 2007, Oracle. All rights reserved Triggers

Copyright © 2007, Oracle. All rights reserved Triggering Events Event TypeExamples of Events DML INSERT, UPDATE, DELETE DDL CREATE, DROP, ALTER, GRANT, REVOKE, RENAME Database LOGON, LOGOFF, STARTUP, SHUTDOWN, SERVERERROR, SUSPEND

Copyright © 2007, Oracle. All rights reserved 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*1.1 3 WHERE employee_id=100; SQL> UPDATE employees 2 SET salary=salary WHERE employee_id=100; Transaction 2

Copyright © 2007, Oracle. All rights reserved Locking Mechanism High level of data concurrency: –Row-level locks for inserts, updates, and deletes –No locks required for queries Automatic queue management Locks held until the transaction ends (with the COMMIT or ROLLBACK operation) Transaction 1 SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id=101; SQL> UPDATE employees 2 SET salary=salary WHERE employee_id=100; Transaction 2

Copyright © 2007, Oracle. All rights reserved Data Concurrency Time: 09:00:00 Transaction 1 UPDATE hr.employees SET salary=salary+100 WHERE employee_id=100; Transaction 2 UPDATE hr.employees SET salary=salary+100 WHERE employee_id=101; Transaction 3 UPDATE hr.employees SET salary=salary+100 WHERE employee_id=102;... Transaction x UPDATE hr.employees SET salary=salary+100 WHERE employee_id=xxx;

Copyright © 2007, Oracle. All rights reserved Data Concurrency Full Notes Page

Copyright © 2007, Oracle. All rights reserved DML Locks 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 SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id= 106; 1 row updated. SQL> UPDATE employees 2 SET salary=salary*1.1 3 WHERE employee_id= 107; 1 row updated. Transaction 2Transaction 1

Copyright © 2007, Oracle. All rights reserved Enqueue Mechanism The enqueue mechanism keeps track of: Sessions waiting for locks Requested lock mode Order in which sessions requested the lock

Copyright © 2007, Oracle. All rights reserved Lock Conflicts UPDATE employees SET salary=salary+100 WHERE employee_id=100; 1 row updated. 9:00:00 UPDATE employees SET salary=salary+100 WHERE employee_id=101; 1 row updated. UPDATE employees SET COMMISION_PCT=2 WHERE employee_id=101; Session waits enqueued due to lock conflict. 9:00:05 SELECT sum(salary) FROM employees; SUM(SALARY) Session still waiting! 16:30:00 Many selects, inserts, updates, and deletes during the last 7.5 hours, but no commits or rollbacks! 1 row updated. Session continues. 16:30:01 commit; Transaction 1Transaction 2Time

Copyright © 2007, Oracle. All rights reserved Possible Causes of Lock Conflicts Uncommitted changes Long-running transactions Unnecessarily high locking levels

Copyright © 2007, Oracle. All rights reserved Detecting Lock Conflicts Select Blocking Sessions on the Performance page. Click the Session ID link to view information about the locking session, including the actual SQL statement.

Copyright © 2007, Oracle. All rights reserved Resolving Lock Conflicts To resolve a lock conflict: Have the session holding the lock commit or roll back Terminate the session holding the lock (in an emergency)

Copyright © 2007, Oracle. All rights reserved Resolving Lock Conflicts with SQL SQL statements can be used to determine the blocking session and kill it. SQL> alter system kill session '144,8982' immediate; SQL> select sid, serial#, username from v$session where sid in (select blocking_session from v$session) Result: 1 2

Copyright © 2007, Oracle. All rights reserved Deadlocks Transaction 2Transaction 1 UPDATE employees SET salary = salary x 1.1 WHERE employee_id = 1000; UPDATE employees SET salary = salary x 1.1 WHERE employee_id = 2000; ORA-00060: Deadlock detected while waiting for resource UPDATE employees SET manager = 1342 WHERE employee_id = 2000; UPDATE employees SET manager = 1342 WHERE employee_id = 1000; 9:00 9:15 9:16

Copyright © 2007, Oracle. All rights reserved 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

Copyright © 2007, Oracle. All rights reserved Practice 9 Overview: Managing Data and Concurrency This practice covers the following topics: Identifying locking conflicts Resolving locking conflicts

Copyright © 2007, Oracle. All rights reserved