Outline

• Active elements CS 235: – Maintain integrity and consistency. Introduction to – Part of database schema. • Constraints Svetlozar Nestorov

Lecture Notes #13

Constraints Constraint Types

• Restrictions on the data in your database. 1. Primary key declarations (already covered). • Commercial relational systems allow much 2. Foreign-keys = constraints. more fine-tuning of constraints than do the 3. Attribute- and tuple-based checks = constraints modeling languages we learned earlier. within relations. • In essence: SQL programming is used to 4. SQL Assertions = global constraints. describe constraints. – Not found in MySQL. 5. MySQL Triggers. – A substitute for assertions.

Foreign Keys Example

• In R a clause that attribute A references CREATE Beers ( S(B) says that whatever values appear in the A name CHAR(20) PRIMARY KEY, of R must also appear in the B column of manf CHAR(20) relation S. ); • B must be declared the primary key (or unique) for S. CREATE TABLE Sells ( bar CHAR(20), – Why is this restriction necessary? beer CHAR(20) REFERENCES Beers(name) , price REAL );

1 Alternative Declaration Foreign Keys in MySQL

• Add another element declaring the , • Both the referenced and referencing as: tables must be of type InnoDB. CREATE TABLE Sells ( – Default type is MyISAM (indexed sequential bar CHAR(20), access method) beer CHAR(20), price REAL, • The FOREIGN KEY syntax must be FOREIGN KEY (beer) REFERENCES Beers(name) used. ); • In the referenced table, there must be an • Extra element essential if the foreign key is more index on the referenced columns than one attribute. – PRIMARY KEY or UNIQUE create one • MySQL recognizes only this declaration. automatically.

MySQL Example Foreign Key Constraint Violations

CREATE TABLE Beers ( 1. Insert or update a Sells tuple so it refers to a name CHAR(20) PRIMARY KEY, nonexistent beer. manf CHAR(20) • Always rejected. ) TYPE = InnoDB; 2. Delete or update a Beers tuple that has a beer value some Sells tuples refer to: CREATE TABLE Sells ( a) Default: reject the modification. bar CHAR(20), b) Cascade: Ripple changes to referring Sells beer CHAR(20), tuple. price REAL, FOREIGN KEY (beer) REFERENCES Beers(name) c) Set Null: Change referring tuples to have ) TYPE = InnoDB; NULL in referring components.

Example (Cascade) Example (Set-Null)

• Delete Bud. • Delete Bud. • Cascade deletes all Sells tuples that mention • Set-null makes all Sells tuples with Bud in Bud. the beer component have NULL there.

• Update Bud to Budweiser. • Change all Sells tuples with Bud in beer column • Update Bud to Budweiser. to be Budweiser. • Set-null makes all Sells tuples with Bud in the beer component have NULL there.

2 Selecting a Policy Attribute-Based Checks

• Add ON [DELETE, UPDATE] [CASCADE, SET NULL] to • Follow an attribute by a condition that must hold foreign key declaration. for that attribute in each tuple of its relation. CREATE TABLE Sells ( bar CHAR(20), • CHECK (condition). beer CHAR(20), – Condition may involve the checked attribute. price REAL, FOREIGN KEY (beer) REFERENCES Beers(name) – Other attributes and relations may be involved, but ON DELETE SET NULL only in subqueries. ON UPDATE CASCADE – MySQL: CHECK parsed but ignored. ); • Condition is checked only when the associated • Correct policy is a design decision. – E.g., what does it mean if a beer goes away? What if a beer attribute changes (i.e., an insert or update changes its name? occurs).

Example Tuple-Based Checks CREATE TABLE Sells ( bar CHAR(20), • Separate element of table declaration. beer CHAR(20) CHECK (beer IN ( • Form: like attribute-based check. SELECT name FROM Beers)), • But condition can refer to any attribute of price REAL CHECK (price <= 5.00) the relation. ); – Or to other relations/attributes in subqueries. • Check on beer is like a foreign-key constraint, – Again: MySQL parses but ignores checks. except: – The check occurs only when we add a tuple or change • Checked whenever a tuple is inserted or the beer in an existing tuple, not when we delete a tuple from Beers. updated.

Example SQL Assertions

• Only Ripoff bar can sell beer for more than • Database-schema constraint. $10. • Not present in MySQL. CREATE TABLE Sells ( • Checked whenever a mentioned relation bar CHAR(20), changes. beer CHAR(20), •Syntax: price REAL, CREATE ASSERTION CHECK(bar = ‘Ripoff’ OR CHECK(); price <= 10.00) );

3 Example Example • No bar may charge an average of more than $5 for beer. Sells(bar, beer, price) • There cannot be more bars than drinkers. Bars(name, addr, license) Drinkers(name, addr, phone) CREATE ASSERTION NoRipoffBars CHECK(NOT EXISTS( CREATE ASSERTION FewBars SELECT bar CHECK( FROM Sells (SELECT COUNT(*) FROM Bars) <= GROUP BY bar (SELECT COUNT(*) FROM Drinkers) HAVING 5.0 < AVG(price) ); ) ); • Checked whenever Bars or Drinkers changes. • Checked whenever Sells changes.

Example Aggregation Queries

• Find the person who likes the most beers. • Find the most likely pairing of a person and a beer. – Most bars, frequented by a person, that serve the beer. – Another condition? • Find the most likely couple: drinkers that frequent the most bars and like the most beers in common. – Can we weigh number of bars and beers differently?

4