Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3.

Similar presentations


Presentation on theme: "Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3."— Presentation transcript:

1 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3

2 © Accenture 2011. All Rights Reserved. 2 Day 3 – SQL Agenda SQL INSERT UPDATE DELETE MERGE Database transactions Practice

3 © Accenture 2011. All Rights Reserved. 3 Modifying Data DML is the subset of SQL that is employed to change data INSERT Adds rows to a table UPDATE Changes the value stored in a table MERGE Updates or inserts rows from one table into another DELETE Removes rows from a table SELECT FOR UPDATE Prevents other sessions from performing DML on selected rows LOCK TABLE Prevents other sessions from performing DML on a table

4 © Accenture 2011. All Rights Reserved. 4 INSERT statement To add new rows you have to use INSERT statement One row a time is inserted with the syntax INSERT INTO table [(column1, column2, …)] VALUES (value1, value2, …); INSERT INTO dept (deptno, dname, loc) VALUES (40, ‘IT’, ‘Lodz’);

5 © Accenture 2011. All Rights Reserved. 5 Adding new rows To add / copy rows you have to use INSERT - SELECT statement INSERT INTO table1 [(column1, column2, …)] SELECT value1, value2, … FROM table2 WHERE …; INSERT INTO dept (deptno, dname, loc) SELECT 50, ‘BPO’, ‘Warsaw’ FROM DUAL;

6 © Accenture 2011. All Rights Reserved. 6 UPDATE statement To modify existing rows you have to use UPDATE statement Update more than one row at a time if required UPDATE table SET column1 = value1 [, column2 = value2, …] [WHERE condition]; UPDATE dept SET dname = ‘SUPER IT’ WHERE deptno = 40;

7 © Accenture 2011. All Rights Reserved. 7 DELETE statement You can remove rows from a table by using DELETE statement DELETE [FROM] table [WHERE condition]; DELETE FROM dept WHERE deptno = 40;

8 © Accenture 2011. All Rights Reserved. 8 MERGE statement Provides the ability to conditionally update or insert data into a database table Performs UPDATE when the row exists and an INSERT when it is new row: –Increases performance –Useful in data warehouse applications

9 © Accenture 2011. All Rights Reserved. 9 MERGE statement example MERGE INTO bonuses B USING ( SELECT employee_id, salary FROM employee WHERE dept_no =20) E ON (B.employee_id = E.employee_id) WHEN MATCHED THEN UPDATE SET B.bonus = E.salary * 0.1 WHEN NOT MATCHED THEN INSERT (B.employee_id, B.bonus) VALUES (E.employee_id, E.salary * 0.05);

10 © Accenture 2011. All Rights Reserved. 10 ?Question Which are DML statements ? A ) MERGE... B ) CREATE... C ) DROP... D ) DELETE... E ) COMMIT... F ) UPDATE...

11 © Accenture 2011. All Rights Reserved. 11 Database transactions A database transactions consists of one of the following: –DML statements which constitute one consistent change to the data –One DML statement (CREATE, DROP, …) –One DCL statement (GRANT, REVOKE)

12 © Accenture 2011. All Rights Reserved. 12 Transaction is ACID Atomic: all changes to the database made in a transaction are rolled back if any change fails. Consistent: the effects of a transaction take the database from one consistent state to another consistent state. Isolated: the intermediate steps in a transaction are not visible to other users of the database. Durable: when a transaction is completed (committed or rolled back), its effects persist in the database.

13 © Accenture 2011. All Rights Reserved. 13 Transaction begin - end Begin when 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 connection is broken (automatic rollback) –The system crashes (automatic rollback)

14 © Accenture 2011. All Rights Reserved. 14 Rolling back transaction to a marker Create a marker in a current transaction by using SAVEPOINT statement Rollback to the marker by using ROLLBACK TO SAVEPOINT statement UPDATE … (10 rows updated) SAVEPOINT after_update; INSERT … (2 rows inserted) ROLLBACK TO SAVEPOINT after_update; COMMIT;

15 © Accenture 2011. All Rights Reserved. 15 ?Question Which of the following commands can be rolled back? (Choose all correct answers.) COMMIT DELETE INSERT MERGE TRUNCATE UPDATE

16 © Accenture 2011. All Rights Reserved. 16 Locking rows In order to perform some actions it is required to have exclusive access to particular object (in example we want to edit lines of order and we have to assure that nobody will approve the order simultaneously) – we have to lock master object (in this case it is row in table ORDERS) In order to lock a row or a group of rows we have to perform We can also extend the statement with NOWAIT clause –When it is possible to lock the rows – standard SELECT is performed and rows are locked –When it is not possible to lock the rows – exception is thrown: ORA-00054 resource busy and acquire with NOWAIT specified. (It is required to define exception in PL/SQL) The lock will be released when the transaction finishes (commit or rollback) SELECT * FROM table WHERE condition FOR UPDATE;

17 © Accenture 2011. All Rights Reserved. 17 Locks and waiters DML and SELECT FOR UPDATE statements cause locking rows for the time of transaction Locked objects can be found by following query Sessions waiting for locked resources can be found in dba_waiters view. SELECT o.owner, o.object_name, l.* FROM v$locked_object l, dba_objects o WHERE l.object_id = o.object_id;

18 © Accenture 2011. All Rights Reserved. Session view All the sessions running on Oracle database are visible in v$session view TIP use v$session view. Useful columns of v$session view: sid,serial# – uniqe identifier of the session username – name used when connecting the database sql_hash_value– unique identifier of SQL currently executed by the session action,module – name of the currently executing action as set by calling the DBMS_APPLICATION_INFO.SET_ACTION procedure

19 © Accenture 2011. All Rights Reserved. Session wait view Displays the current or last wait for each session. TIP use v$session_wait view. Useful columns of v$session_wait view: sid– uniqe identifier of the session event – Resource or event for which the session is waiting seconds_in_wait– If the session is currently waiting, then the value is the amount of time waited for the current wait. If the session is not in a wait, then the value is the amount of time since the start of the last wait. state – current session state

20 © Accenture 2011. All Rights Reserved. List of waiting sessions Shows all the sessions that are waiting for a lock. TIP use dba_waiters view. Useful columns of dba_waiters view: WAITING_SESSION- The waiting session HOLDING_SESSION- The holding session

21 © Accenture 2011. All Rights Reserved. List of locked objects Shows all objects currentlu locked TIP use v$locked_objects view. Useful columns of v$locked_objects view: OBJECT_ID- Locked object ID SESSION_ID- The holding session LOCKED_MODE- Mode of the lock

22 © Accenture 2011. All Rights Reserved. List of objects in the database Shows all objects existing in the database TIP use dba_objects view. Useful columns of dba_objects view: OBJECT_ID- Uniqye object ID OWNER- Owner (schema name) OBJECT_NAME- Name of the object

23 © Accenture 2011. All Rights Reserved. Laboratory database architecture APP_OWNER PERSONTOYTOY_HISTORY FRANIU BENIU

24 © Accenture 2011. All Rights Reserved. Practice 1 APP_OWNER PERSON FRANIU BENIU Both users FRANIU and BENIU are accessing the PERSON table in APP_OWNER schema. They try to make update on some rows. Operations from one session was successful however second session hanged. ?Question Identify why the second session hang. Propose solution.

25 © Accenture 2011. All Rights Reserved. Practice 2 APP_OWNER PERSON FRANIU BENIU Both users FRANIU and BENIU are accessing the PERSON table in APP_OWNER schema. They try to delete some rows. Operations from one session was successful however second session hanged. ?Question Identify why the second session hang. Propose solution. TOY

26 © Accenture 2011. All Rights Reserved. Practice 3 APP_OWNER FRANIU One session is running operation on the TOY_HISTORY table. ?Question Identify why the second session hang. Propose solution. TOY_HISTORY


Download ppt "Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3."

Similar presentations


Ads by Google