1 Introduction to Database Systems, CS420 SQL Constraints.

Slides:



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

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and 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.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Constraints. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
1 Constraints Foreign Keys Local and Global Constraints Triggers Source: slides by Jeffrey Ullman.
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.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
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.
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.
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.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
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.
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,
1 CS1368 Introduction* Relational Model, Schemas, SQL Semistructured Model, XML * The slides in this lecture are adapted from slides used in Standford's.
+ 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.
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.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
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. 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.
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 JOIN, Aggregate, Grouping, HAVING and DML Clauses.
CPSC-310 Database Systems
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CMSC-461 Database Management Systems
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Database Design: DBS CB, 2nd Edition
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
Presentation transcript:

1 Introduction to Database Systems, CS420 SQL Constraints

2 Agenda Pattern (Like) Review Keys Foreign-key Check Assertion

3 Review: Create Table CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL );

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

5 Review: Basic Query Constructs SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables

6 Review: LIKE for Patterns Using Drinkers(name, addr, phone) find the drinkers with exchange 555: SELECT name FROM Drinkers WHERE phone LIKE ’%555-_ _ _ _’;

More on Using % SELECT first_name || ' ' || last_name FROM employees e WHERE last_name LIKE 'Green%'; --returns: --Nancy Greenberg --Danielle Greene 7

More on Using _ any employee last names that start with “Green…” AND have exactly one character more in the last name SELECT first_name || ' ' || last_name FROM employees WHERE last_name LIKE 'Green_'; --returns: --Danielle Greene 8

Potential Mistakes -- % not used (meaning equals (=)) SELECT first_name || ' ' || last_name FROM employees WHERE last_name LIKE 'Kin'; --returns: [no rows] 9

Potential Mistakes -- * used SELECT first_name || ' ' || last_name FROM employees WHERE last_name LIKE 'Kin*'; --returns: [no rows] 10

11 Constraints

12 Constraints A constraint is a relationship among data elements that the DBMS is required to enforce.  Example: key constraints.

13 Kinds of Constraints Keys. Foreign-key, or referential-integrity. Value-based constraints.  Constrain values of a particular attribute. Tuple-based constraints.  Relationship among components.

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

15 Multi-attribute Key The bar and beer together are the key for Sells: CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL, PRIMARY KEY (bar, beer) );

16 Foreign Key – Referential Integrity

17 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.

18 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.

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

20 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));

21 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.”

22 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).

23 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.

24 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’.

25 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.

26 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.

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

FK in Action T (C integer Primary Key, D integer); S (B integer Primary key, C integer Foreign Key references T(C) on delete cascade); R (A integer Primary Key, B integer Foreign Key references S(B) on delete set null); 28

FK in Action (cont.) Q: After executing the following command: Delete from T; What will be the result in table R? 29

FK in Action (cont.) Answer (1, NULL) and (2, NULL) 30

31 Checks

32 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.

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

34 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.

35 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.

36 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) );

37 Assertions

38 Assertions These are database-schema elements, like relations or views. Defined by: CREATE ASSERTION CHECK ( ); Condition may refer to any relation or attribute in the database schema.

39 Example: Assertion 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

40 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) );

41 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.

42 Summary Keys Foreign keys Checks  Value-based constraints  Tuple-based constraints Assertions

43 END