Docsity

Understanding Foreign Key Constraints in Database Management Systems, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Understanding Foreign Key Constraints in Database Management Systems and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Constraints

Foreign Keys

Local and Global Constraints

Triggers

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.

Foreign Keys

  • Consider Relation Sells(store, candy, price).
  • We might expect that a candy value is a real candy --- something appearing in Candies.name.
  • A constraint that requires a candy in Sells to be a candy in Candies is called a foreign - key constraint.

Expressing Foreign Keys

  • Use the keyword REFERENCES, either:
    • Within the declaration of an attribute (only for one-attribute keys).
    • As an element of the schema: FOREIGN KEY ( ) REFERENCES ( )
  • Referenced attributes must be declared PRIMARY KEY or UNIQUE.

Example: As Element

CREATE TABLE Candies ( name CHAR(20) PRIMARY KEY, manf CHAR(20) );

CREATE TABLE Sells ( store CHAR(20), candy CHAR(20), price REAL, FOREIGN KEY(candy) REFERENCES Candies(name));

Enforcing Foreign-Key Constraints

  • If there is a foreign-key constraint from attributes of relation R to a 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 --- (2)

  • Default : Reject the modification.
  • Cascade : Make the same changes in Sells.
    • Deleted candy: delete Sells tuple.
    • Updated candy: change value in Sells.
  • Set NULL : Change the candy to NULL.

Example: Cascade

  • Delete the Twizzler tuple from Candies:
    • Then delete all tuples from Sells that have candy = ’Twizzler’.
  • Update the Twizzler tuple by changing ’Twizzler’ to ’Twiz.’: - Then change all Sells tuples with candy = ’Twizzler’ so that candy = ’Twiz.’.

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.

Example

CREATE TABLE Sells (

store CHAR(20), candy CHAR(20), price REAL, FOREIGN KEY(candy) REFERENCES Candies(name) ON DELETE SET NULL ON UPDATE CASCADE

);

Example

CREATE TABLE Sells (

store CHAR(20), candy CHAR(20) CHECK ( candy IN (SELECT name FROM Candies)), price REAL CHECK ( price <= 5.00 )

);

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 (candy IN (SELECT name FROM Candies))is not checked if a candy is deleted from Candies (unlike foreign- keys).

Example: Tuple-Based Check

  • Only 7-11 can sell candy for more than $5: CREATE TABLE Sells ( store CHAR(20), candy CHAR(20), price REAL, CHECK (store = ’7-11’ OR price <= 5.00) );

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.