Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SQL Workshop Day 3 Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Day 3 – SQL Agenda • SQL • • • • INSERT UPDATE DELETE MERGE • Database transactions • Practice © Accenture 2011. All Rights Reserved. 2 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 © Accenture 2011. All Rights Reserved. 3 INSERT statement INSERT INTO table [(column1, column2, …)] VALUES (value1, value2, …); • To add new rows you have to use INSERT statement • One row a time is inserted with the syntax INSERT INTO dept (deptno, dname, loc) VALUES (40, ‘IT’, ‘Lodz’); © Accenture 2011. All Rights Reserved. 4 Adding new rows INSERT SELECT FROM WHERE INTO table1 [(column1, column2, …)] value1, value2, … table2 …; • To add / copy rows you have to use INSERT - SELECT statement INSERT INTO dept (deptno, dname, loc) SELECT 50, ‘BPO’, ‘Warsaw’ FROM DUAL; © Accenture 2011. All Rights Reserved. 5 UPDATE statement UPDATE table SET column1 = value1 [, column2 = value2, …] [WHERE condition]; • To modify existing rows you have to use UPDATE statement • Update more than one row at a time if required UPDATE dept SET dname = ‘SUPER IT’ WHERE deptno = 40; © Accenture 2011. All Rights Reserved. 6 DELETE statement DELETE [FROM] table [WHERE condition]; • You can remove rows from a table by using DELETE statement DELETE FROM dept WHERE deptno = 40; © Accenture 2011. All Rights Reserved. 7 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 © Accenture 2011. All Rights Reserved. 8 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); © Accenture 2011. All Rights Reserved. 9 ?Question Which are DML statements ? A ) MERGE... B ) CREATE... C ) DROP... D ) DELETE... E ) COMMIT... F ) UPDATE... © Accenture 2011. All Rights Reserved. 10 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) © Accenture 2011. All Rights Reserved. 11 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. © Accenture 2011. All Rights Reserved. 12 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) © Accenture 2011. All Rights Reserved. 13 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; © Accenture 2011. All Rights Reserved. 14 ?Question Which of the following commands can be rolled back? (Choose all correct answers.) • • • • • • COMMIT DELETE INSERT MERGE TRUNCATE UPDATE © Accenture 2011. All Rights Reserved. 15 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 SELECT FROM WHERE FOR * table condition UPDATE; • 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) © Accenture 2011. All Rights Reserved. 16 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 SELECT o.owner, o.object_name, l.* FROM v$locked_object l, dba_objects o WHERE l.object_id = o.object_id; • Sessions waiting for locked resources can be found in dba_waiters view. © Accenture 2011. All Rights Reserved. 17 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# username sql_hash_value action,module © Accenture 2011. All Rights Reserved. – uniqe identifier of the session – name used when connecting the database – unique identifier of SQL currently executed by the session – name of the currently executing action as set by calling the DBMS_APPLICATION_INFO.SET_ACTION procedure 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 © 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 © 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 © 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 © Accenture 2011. All Rights Reserved. Laboratory database architecture APP_OWNER PERSON FRANIU © Accenture 2011. All Rights Reserved. TOY TOY_HISTORY BENIU Practice 1 Both users FRANIU and BENIU are accessing the PERSON table in APP_OWNER schema. They try to make update on some rows. APP_OWNER Operations from one session was successful however second session hanged. PERSON ?Question •Identify why the second session hang. •Propose solution. FRANIU © Accenture 2011. All Rights Reserved. BENIU Practice 2 Both users FRANIU and BENIU are accessing the PERSON table in APP_OWNER schema. They try to delete some rows. APP_OWNER PERSON TOY Operations from one session was successful however second session hanged. ?Question •Identify why the second session hang. •Propose solution. FRANIU © Accenture 2011. All Rights Reserved. BENIU Practice 3 One session is running operation on the TOY_HISTORY table. APP_OWNER TOY_HISTORY FRANIU © Accenture 2011. All Rights Reserved. ?Question •Identify why the second session hang. •Propose solution.