Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides

Slides:



Advertisements
Similar presentations
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
Advertisements

SQL CSET 3300.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
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 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
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.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
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.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
1 IT 244 Database Management System Lecture 11 More SQL Constraints &Triggers, SQL Authorization,Transactions Foreign Keys, Local and Global Constraints,
+ Assignment 5 Q’s CSCI 2141 W Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
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.
Databases 1 Second lecture.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
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 Constraints.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
Select-From-Where Statements Multirelation Queries Subqueries
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
CPSC-310 Database Systems
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CPSC-310 Database Systems
CMSC-461 Database Management Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides 1

Reading Ullman and Widom: Chapter 2.5 2

3 Core Relational Algebra Union, intersection, and difference. – Usual set operations, but both operands must have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes.

4 4 Expression Tree: BarsSells σ addr = “Maple St.” σ price<3 AND beer=“Bud” π name ρ R(name) π bar ∪

5 Why SQL? SQL is a very-high-level language. – Say “what to do” rather than “how to do it.” – Avoid a lot of data-manipulation details needed in procedural languages like C++ or Java. Database management system figures out “best” way to execute query. – Called “query optimization.”

6 SQL Statement A typical SQL query form: SELECT A 1, A 2,..., A n FROM r 1, r 2,..., r m WHERE C – A i s represent attributes to be returned – r i s represent relations – C is a condition

7 Our Running Example All our SQL queries will be based on the following database schema. – Underline indicates key attributes. Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

8 Example Using Beers(name, manf), what beers are made by Anheuser-Busch?  name (  manf = ’Anheuser-Busch’ Beers) or SELECT name FROM Beers WHERE manf = ’Anheuser-Busch’;

9 * In SELECT clauses When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” Example: Using Beers(name, manf): SELECT * FROM Beers WHERE manf = ’Anheuser-Busch’;  name,manf (  manf = ’Anheuser-Busch’ Beers)

10 Renaming Attributes If you want the result to have different attribute names, use “AS ” to rename an attribute. Example: Using Beers(name, manf): SELECT name AS beer, manf FROM Beers WHERE manf = ’Anheuser-Busch’  Beers(beer,manf) (  name,manf (  manf = ’Anheuser-Busch’ Beers))

11 Example: Constants as Expressions Using Likes(drinker, beer): SELECT drinker, ’likes Bud’ AS whoLikesBud FROM Likes WHERE beer = ’Bud’;

12 Complex Conditions in WHERE Clause Boolean operators AND, OR, NOT. Comparisons =, <>,, =. – And many other operators that produce boolean- valued results.

13 Example: Complex Condition Using Sells(bar, beer, price), find the price Joe’s Bar charges for Bud: SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’;  name,manf (  bar = ’Joe’s’ AND beer = ’Bud’ Beers)

14 Multirelation Queries Interesting queries often combine data from more than one relation. We can address several relations in one query by listing them all in the FROM clause. Distinguish attributes of the same name by “. ”.

15 Example: Joining Two Relations Using relations Likes(drinker, beer) and Frequents(drinker, bar), find the beers liked by at least one person who frequents Joe’s Bar. SELECT beer FROM Likes, Frequents WHERE bar = ’Joe’’s Bar’ AND Frequents.drinker = Likes.drinker;  beer (  bar = ’Joe’s’ AND Frequents.drinker = Likes.drinker (Likes  Frequents))

Constraints and Triggers A constraint is a relationship among data elements that the DBMS is required to enforce. – Example: key constraints. Triggers are only executed when a specified condition occurs, e.g., insertion of a tuple. – Easier to implement than complex constraints. 16

Kinds of Constraints Keys. Foreign-key, or referential-integrity. Value-based constraints. – Constrain values of a particular attribute. Tuple-based constraints. – Relationship among components. Assertions: any SQL boolean expression. 17

Review: Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example: CREATE TABLE Beers ( nameCHAR(20) UNIQUE, manfCHAR(20) ); 18

Review: Multiattribute Key The bar and beer together are the key for Sells: CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL, PRIMARY KEY (bar, beer) ); 19

Foreign Keys Values appearing in attributes of one relation must appear together in certain attributes of another relation. Example: in Sells(bar, beer, price), we might expect that a beer value also appears in Beers.name. 20

Expressing Foreign Keys Use keyword REFERENCES, either: 1.After an attribute (for one-attribute keys). 2.As an element of the schema: FOREIGN KEY ( ) REFERENCES ( ) Referenced attributes must be declared PRIMARY KEY or UNIQUE. 21

Example: With Attribute CREATE TABLE Beers ( nameCHAR(20) PRIMARY KEY, manfCHAR(20) ); CREATE TABLE Sells ( barCHAR(20), beerCHAR(20) REFERENCES Beers(name), priceREAL ); 22

Example: As Schema Element CREATE TABLE Beers ( nameCHAR(20) PRIMARY KEY, manfCHAR(20) ); CREATE TABLE Sells ( barCHAR(20), beerCHAR(20), priceREAL, FOREIGN KEY(beer) REFERENCES Beers(name)); 23

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

Actions Taken --- (1) Example: suppose R = Sells, S = Beers. An insert or update to Sells that introduces a nonexistent beer must be rejected. A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways (next slide). 25

Actions Taken --- (2) 1.Default : Reject the modification. 2.Cascade : Make the same changes in Sells. – Deleted beer: delete Sells tuple. – Updated beer: change value in Sells. 3.Set NULL : Change the beer to NULL. 26

Example: Cascade Delete the Bud tuple from Beers: – Then delete all tuples from Sells that have beer = ’Bud’. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: – Then change all Sells tuples with beer = ’Bud’ to beer = ’Budweiser’. 27

Example: Set NULL Delete the Bud tuple from Beers: – Change all tuples of Sells that have beer = ’Bud’ to have beer = NULL. Update the Bud tuple by changing ’Bud’ to ’Budweiser’: – Same change as for deletion. 28

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] Two such clauses may be used. Otherwise, the default (reject) is used. 29

Example: Setting Policy CREATE TABLE Sells ( barCHAR(20), beerCHAR(20), priceREAL, FOREIGN KEY(beer) REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE ); 30

Attribute-Based Checks Constraints on the value of a particular attribute. Add CHECK( ) 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. 31

Example: Attribute-Based Check CREATE TABLE Sells ( barCHAR(20), beerCHAR(20)CHECK ( beer IN (SELECT name FROM Beers)), priceREAL CHECK ( price <= 5.00 ) ); 32

Timing of Checks Attribute-based checks are performed only when a value for that attribute is inserted or updated. – Example: CHECK (price <= 5.00) checks every new price and rejects the modification (for that tuple) if the price is more than $5. – Example: CHECK (beer IN (SELECT name FROM Beers)) not checked if a beer is deleted from Beers (unlike foreign-keys). 33

Tuple-Based Checks CHECK ( ) may be added as a relation-schema element. The condition may refer to any attribute of the relation. – But other attributes or relations require a subquery. Checked on insert or update only. 34

Example: Tuple-Based Check Only Joe’s Bar can sell beer for more than $5: CREATE TABLE Sells ( barCHAR(20), beerCHAR(20), priceREAL, CHECK (bar = ’Joe’’s Bar’ OR price <= 5.00) ); 35

Next Class Oracle implementation (hand-outs) Simple queries 36