2018, Fall Pusan National University Ki-Joune Li

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Fall 2001Arthur Keller – CS 1808–1 Schedule Today Oct. 18 (TH) Schemas, Views. u Read Sections u Project Part 3 extended to Oct. 23 (T). Oct.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
SQL 2015, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Database Systems Defining Database Schema Views.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Views, Indexes Virtual and Materialized Views Speeding Accesses to Data.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
1 Constraints, Views, Indexes Foreign Keys Constraints Triggers Virtual and Materialized Views Speeding Accesses to Data.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
1 Introduction to Database Systems, CS420 SQL Constraints.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
CPSC-310 Database Systems
Virtual and Materialized Views Speeding Accesses to Data
Slides are reused by the approval of Jeffrey Ullman’s
Chapter 6 - Database Implementation and Use
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CMSC-461 Database Management Systems
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Database Design: DBS CB, 2nd Edition
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
Defining a Database Schema
CMSC-461 Database Management Systems
Virtual and Materialized Views Speeding Accesses to Data
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Transactions, Views, Indexes
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

2018, Fall Pusan National University Ki-Joune Li More about SQL 2018, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web page (http://infolab.stanford.edu/~ullman/dscb/gslides.html)

Views A view is a “virtual table,” a relation that is defined in terms of the contents of other tables and views. Declare by: CREATE VIEW <name> AS <query>; In contrast, a relation whose value is really stored in the database is called a base table. Example View CanDrink(Drinker, Beer) is created CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar;

Example: Accessing a View You may query a view as if it were a base table. There is a limited ability to modify views if the modification makes sense as a modification of the underlying base table. Example: SELECT beer FROM CanDrink WHERE drinker = ‘Sally’; PROJbeer SELECTdrinker=‘Sally’ CanDrink PROJdrinker, beer JOIN Frequents Sells

DMBS Optimization It is interesting to observe that the typical DBMS will then “optimize” the query by transforming the algebraic expression to one that can be executed faster. Key optimizations: Push selections down the tree. Eliminate unnecessary projections. PROJbeer JOIN SELECTdrinker=‘Sally’ Sells Frequents Notice how most tuples are eliminated from Frequents before the expensive join.

Materialized View Simple View Materialized View What's the difference? CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; Materialized View CREATE MATERIALIZED VIEW CanDrink AS What's the difference?

Referential Constraint and Foreign Key Key in other table used to make a reference to tuple in other table SQL Studio(name, address, presC#) MovieExec(name, address, cert#, netWorth)  CREATE TABLE Studio ( name CHAR(20) PRIMARY KEY, address VARCHAR(100), presC# INT REFERENCE MovieExec(cert#) ); Referential Constraint CREATE TABLE Studio ( name CHAR(20) PRIMARY KEY, address VARCHAR(100), presC# INT, FOREIGN KEY (presC#) REFERENCE MovieExec(cert#) );

Enforcing Foreign-Key Constraints If there is a foreign-key constraint from attributes of relation R to the primary key of relation S, two violations are possible: An insert or update to R introduces values not found in S. A deletion or update to S causes some tuples of R to “dangle.”

Actions Taken Suppose R = Sells, S = Beers. Insertion or update to Sells with a nonexistent beer: Rejected. Deletion or update to Beers that removes a beer value found in some tuples of Sells: Default : Reject the modification. Cascade : Make the same changes in Sells. Deleted beer: delete Sells tuple. Updated beer: change value in Sells. Set NULL : Change the beer to NULL.

Example: Cascade Deletion of the Bud tuple from Beers. Then delete all tuples from Sells that have beer = ’Bud’. Update of the Bud tuple by changing ’Bud’ to ’Budweiser’. Then change all Sells tuples with beer = ’Bud’ so that beer = ’Budweiser’. Cascade deletion Bud Delete Bud Bud Cascade updates Update Beers Sells

Example: Set NULL Deletion of the Bud tuple from Beers. Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. NULL NULL Bud Delete Beers Sells

Attribute-Based Checks Put a constraint on the value of a particular attribute. CHECK( <condition> ) must be added to the declaration for the attribute. The condition may use the name of the attribute, but any other relation or attribute name must be in a subquery. Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK ( beer IN SELECT name FROM Beers)), price REAL CHECK ( price <= 5.00 ) );

Timing of Checks An attribute-based check is checked only when a value for that attribute is inserted or updated. Example CHECK (price <= 5.00) checks every new price and rejects it if it is more than $5. CHECK (beer IN (SELECT name FROM Beers)) not checked if a beer is deleted from Beers Can NOT be used for the Referential Constraint

Tuple-Based Checks CHECK ( <condition> ) may be added as another element of a schema definition. The condition may refer to any attribute of the relation, but any other attributes or relations require a subquery. Checked on insertion or update only (NOT delete). Example Only Joe’s Bar can sell beer for more than $5: CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, CHECK (bar = ’Joe’’s Bar’ OR price <= 5.00) );

Assertions Assertion: database-schema elements, like relations Defined by: CREATE ASSERTION <name> CHECK ( <condition> ); Condition may refer to any relation or attribute in the database schema. Example: In Sells(bar, beer, price), no bar may charge an average of more than $5. CREATE ASSERTION NoRipoffBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price) )); Bars with an average price above $5

Example: Assertion In Drinkers(name, addr, phone) and Bars(name, addr, license), there cannot be more bars than drinkers. CREATE ASSERTION FewBar CHECK ( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) );

Timing of Assertion Checks In principle, we must check every assertion after every modification to any relation of the database. A clever system can observe that only certain changes could cause a given assertion to be violated. Example: No change to Beers can affect FewBar. Neither can an insertion to Drinkers.

Triggers Attribute- and tuple-based checks have limited capabilities. Assertions sufficiently general for most constraint applications, they are hard to implement efficiently. DBMS must have real intelligence to avoid checking assertions that couldn’t possibly have been violated. Trigger: a more powerful expression on ECA (Event-Condition- Action) Only in SQL3

Example: Trigger Definition CREATE TRIGGER BeerTrigger AFTER INSERT ON Sells REFERENCING NEW ROW AS NewTuple FOR EACH ROW WHEN (NewTuple.beer NOT IN (SELECT name FROM Beers)) INSERT INTO Beers(name) VALUES(NewTuple.beer); The event The condition The action

Transaction - Example Flights (fltNo, FltDate, seatNo, seatStatus) SELECT seatNo FROM Flights WHERE fltNo=123 AND FltDATE=DATE ‘2008-12-25’ AND seatStatus=‘available’;  22A is found as empty seat UPDATE Flights SET seatStatus=‘occupied’ WHERE FltNo=123 AND FltDATE=DATE ‘2008-12-25’ and seatNo=‘22A’  What may happen?

Transaction Transaction A set of operations Atomic : All or Nothing : Consistent State of Database Example : Flight Reservation Cf. Partially Done : Inconsistent State Partially Committed Committed ALL Activated NOTHING Failed Aborted

Transaction – BEGIN TRAN and COMMIT TRAN Syntax BEGIN TRAN T1; Select/Insert/Delete/Update SQL Statements COMMIT TRAN T1; DBMS must maintain the transaction correctly ACID Atomic Concurrent Isolated Duarable

Serializability Problems may happen in concurrent transactions What happens after these transactions ? Serial Schedule : Always Correct T1 T2 and T2 T1 Concurrent Schedule Serializable if Result (T1 || T2) = Result(T1 T2) or Result(T2 T1) We assume that serial executions are correct Make the results of concurrent execution as the result of one of the serial executions.

Referential Constraint - Deferable Violation of Referential Constraint Reference to Non-existing Foreign Key Example Deletion (or Update) of a tuple referenced by other tuple Insertion of a tuple with a reference to non-existing tuple Should be protected by DBMS DEFERABLE vs. NON DEFERABLE CREATE TABLE Studio ( name CHAR(20) PRIMARY KEY, address VARCHAR(100), presC# INT UNIQUE , REFERENCE MovieExec(cert#) DEFERABLE INITIALLY DEFERED );

Index Index SQL Accelerating search speed B+-tree, Hash etc. CREATE INDEX ScoreIndex ON Student(score); CREATE INDEX KeyIndex ON Movies(title, year);

Choosing a Policy When we declare a foreign key, we may choose policies SET NULL or CASCADE independently for deletions and updates. Follow the foreign-key declaration by: ON [UPDATE, DELETE][SET NULL/CASCADE] Default: reject Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY(beer) REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE );