Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.

Slides:



Advertisements
Similar presentations
Active database concepts
Advertisements

SQL2-ch2 管理綱要物件.
1 Constraints, Triggers and Active Databases Chapter 9.
Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Referential Integrity & Foreign Keys Objectives of the Lecture : To consider Referential Integrity & Foreign Keys; To consider Referential Integrity Constraints.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
SQL Constraints and Triggers
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Transaction Processing Lecture ACID 2 phase commit.
ACS R McFadyen 1 Transaction A transaction is an atomic unit of work that is either completed in its entirety or not done at all. For recovery purposes,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Objectives of the Lecture :
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
The Relational Model These slides are based on the slides of your text book.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules.
Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.
Page 1 Topic 4 Relational Databases CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSC271 Database Systems Lecture # 17. Summary: Previous Lecture  View updatability  Advantages and disadvantages of views  View materialization.
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Further Consolidation Objectives of the Lecture : To use simple queries to check out a DB. To use insertions, deletions and amendments to maintain a DB.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
Database Management COP4540, SCS, FIU Database Trigger.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL Triggers, Functions & Stored Procedures Programming Operations.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Chapter 3 An Introduction to Relational Databases.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Chapter 6: Integrity (and Security)
DCL – Data Control Language
Constraints and Triggers
ATS Application Programming: Java Programming
Active Database Concepts
Rules in active databases and integrity constraints
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Objectives of the Lecture :
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.

Relational Amendment l Strictly speaking, relational amendment is unnecessary. We could just use a Delete followed by an Insert. RelVar  Delete ‘set of tuples to be amended’ RelVar  Insert ‘set of tuples that have been amended’ l However it is more convenient for the user to have a single assignment that does the complete amendment. l To achieve this, it must be possible to : l amend a subset of a relation’s tuples; l amend a subset of a tuple’s attributes. ‘Subset of RelVar’  Amend[ amendment to subset of attributes ]

Example of Amendment  Amend[Sal  (1.1 * Sal )] ENo E3 E5 E EName Smith Mitchell Robson M-S S M D Sal ,500 21,000 32, E6 E8 888Blake Jones 888M W 88854,000 68,000 EMPLOYEE ENo E3 E5 E EName Smith Mitchell Robson M-S S M D Sal ,500 21,000 32, E6 E8 888Blake Jones 888M W 88854,000 68,000 EMPLOYEE Sal 23,600 14,250 59,900 75,300 36,250 Amendment Variable Resulting value of variable. All salaries increased by 10% plus £500.

Amendment in Principle ‘Subset of RelVar’  Amend[ ‘set of attribute assignments’ ] This is a subset of any relation in the DB. Each of one or more assignments replaces an attribute value with a new value. The new attribute value may be : - a scalar constant; - an attribute value, of the same type, and taken from the same tuple; - a function or calculation producing a scalar value; - any combination of the above. The set of attribute assignments are executed in parallel. In principle, there can be any number of tuples in the set, including zero !

SQL : Amend l The SQL syntax is :- Update RELATION_NAME Set attribute assignment(s) Where condition ; In the Where phrase, a condition is written that selects the subset of required tuples from RELATION_NAME. These tuples are then amended. l The Where phrase is optional. One can just write Update RELATION_NAME Set attribute assignment(s) ; In this case, all the tuples in RELATION_NAME are amended.

Examples of SQL Amend 1. Amend all the tuples to give everyone a salary increase of 10% plus £500; i.e. the earlier graphical example : Update EMPLOYEE Set Sal = ( 1.1 * Sal ) ; 2. Amend Mitchell’s name to ‘Wilson’, give him/her a new salary of £27,000, and amend their marital status to widowed : Update EMPLOYEE Set Sal = 27000, EName = ‘Wilson’, M-S = ‘W’ ; This changes the three attributes’ values in every tuple affected by the amendment. Oops !! Every tuple in EMPLOYEE is affected by this amendment. Can the effect of this amendment be undone ?

Further Example of SQL Amend Only employee E5‘s attribute values are altered. 111E3555Smith222S22212,500 33E177Robson66D6632,500 E8JonesW68,000 22E566Mitchell44M4421, E6888Blake888M88854,000 ENoENameM-SSal EMPLOYEE 22E566Mitchell44M4421,000 ENoENameM-SSal 66Wilson44W4427,000 ENameM-SSal A candidate key value was used to pick out the correct individual tuple. Update EMPLOYEE Set Sal = 27000, EName = ‘Wilson’, M-S = ‘W’ Where ENo = ‘E5’ ;

Integrity Constraints l A tuple to be inserted into a relation must meet all the integrity constraints for that relation. If it does not, it is rejected with an error message. l An amended version of a tuple must meet all the integrity constraints for that relation. If it does not, it is rejected with an error message. l Even the deletion of a tuple can be rejected (!) if the deletion breaks an integrity constraint, e.g. to keep a fixed number of tuples in the relation.

Time of Validation Checking l It is possible to check some integrity constraints immediately after the update. They are : l data type constraints, l candidate key constraints, l some referential integrity constraints, l some ad hoc constraints. There is no necessity to delay the checking. l It is not possible to check other integrity constraints immediately after the update. They must be deferred to an appropriate time. They are : l some referential integrity constraints, l some ad hoc constraints.

Example of Deferred Checking ENo EName Smith Mitchell Robson 8Blake M-S S M D 8M Sal ,500 21,000 32, ,000 EMPLOYEE JonesW68, E3 E5 E1 4 E6 E RegNo K123 ABC W811 STA JON 1 8V771 PQ Type Corsa 1.3 Starlet GLi Jaguar XK Volvo S80 CAR 22 E3 E5 E1 E6 Owner Referential Integrity means that the new CAR tuple can only be added after the new EMPLOYEE tuple. W333 LNYFocus 1.6LE4 ArcherS40,000E4

Transactions l A transaction is a means of managing updates. At the end of a transaction, the DB must be in a valid state, i.e. all integrity constraints must apply to all relations in the DB. l Definition : a transaction is a complete and indivisible logical unit of work to update the database. It is completely atomic even when it consists of several updates. l All integrity checks, whatever their nature, must be carried out by the end of the transaction, to ensure that the DB remains in a valid state.

Example : a Circular Referential Path Suppose a DB contains 3 relations : Details about projects for departments. Details about departments, including their location. Details about locations, including its project. PROJECT( ProjNo, DeptID, ….. ) DEPARTMENT( Loc, DeptID, …. ) LOCATION( Loc, ProjNo, ….. ) Each relation has a foreign key that references another relation, so they are linked circularly.

Example Transaction with Circularity The 3 relations are shown as tables, with the corresponding referencing and referenced attributes. Transaction BEGIN Update PROJECT Transaction END Update DEPARTMENT Update LOCATION Integrity can only be checked at the transaction’s end.

Timing of Integrity Checks Thus integrity checks can be split into 2 classes, immediate and deferred. l Immediate integrity checks should be done as soon as the update is completed. There is no point in waiting to complete the transaction. As soon as an error is found, the transaction can be terminated without wasting any more time and processing. l Deferred integrity checks must be done by the end of the transaction. They are typically done at the end of the transaction. The transaction must be designed so that the updates put the DB in the correct state by the end of the transaction.

SQL Transactions l A transaction automatically starts whenever the first SQL Insert / Delete / Update statement is met. There is no special command to start a transaction. More Insert / Delete / Update statements, or none, may follow in the transaction. l A transaction ends when a Commit or Rollback statement is met. l Commit ; Ensures any deferred checks are executed; if there are no integrity errors, it makes the updates permanent. l Rollback ; Undoes all the updates to return the DB to its state at the beginning of the transaction.

Deferrability of SQL Checks l In SQL, integrity constraints are deferrable or not deferrable. l Not deferrable constraints are checked immediately. l Deferrable constraints are further defined to be : l I nitially deferred - not checked until a Commit statement is reached. l Initially immediate - checked immediately. l Deferrable constraints can be switched between these two, using the Set Constraint statement. Examples : Set Constraint FKEY1, FKEY9 Deferred ; Set Constraint PKEY6 Immediate;