











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Constraints
Commercial relational systems allow much more \ ne-tuning" of constraints than do the mo deling languages we learned earlier.
In essence: SQL programming is used to describ e constraints.
Outline
F Not found in Oracle.
F A substitute for assertions.
Foreign Keys
In relation R a clause that \attribute A references S (B )" says that whatever non-null values app ear in the A column of R must also app ear in the B column of relation S.
B must b e declared the primary key for S.
Example
CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) REFERENCES Beers(name), price REAL );
What Happ ens When a Foreign Key Constraint is Violated?
Two ways:
b) Cascade : Ripple changes to referring Sells tuple.
Example
Delete \Bud." Cascade deletes all Sells tuples that mention Bud.
Up date \Bud"! \Budweiser." Change all Sells tuples with \Bud" in beer column to b e \Budweiser."
c) Set Nul l : Change referring tuples to have NULL in referring comp onents.
Example
Delete \Bud." Set-null makes all Sells tuples with \Bud" in the beer comp onent have NULL there.
Up date \Bud"! \Budweiser." Same change.
Attribute-Based Checks
Follow an attribute by a condition that must hold for that attribute in each tuple of its relation.
Form: CHECK (condition).
F Condition may involve the checked attribute. F Other attributes and relations may b e involved, but only in sub queries. F Oracle: No subqueries al lowed in condition.
Condition is checked only when the asso ciated attribute changes (i.e., an insert or up date o ccurs).
Example
CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK( beer IN (SELECT name FROM Beers) ), price REAL CHECK( price <= 5. ) );
Check on beer is like a foreign-key constraint, except:
F The check o ccurs only when we add a tuple or change the b eer in an existing tuple, not when we delete a tuple from Beers.
Example
Only Jo e's Bar can sell b eer for more than $5.
CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, CHECK(bar = 'Joe''s Bar' OR price <= 5.00) );
SQL Assertions
Database-schema constraint.
Not present in Oracle.
Checked whenever a mentioned relation changes.
Syntax:
CREATE ASSERTION
Example
There cannot b e more bars than drinkers.
Bars(name , addr, license) Drinkers(name , addr, phone)
CREATE ASSERTION FewBar CHECK( (SELECT COUNT() FROM Bars) <= (SELECT COUNT() FROM Drinkers) );
Checked whenever Bars or Drinkers changes.
Triggers (Oracle Version)
Often called event-condition-acti on rules.
Event = a class of changes in the DB, e.g., \insertions into Beers."
Condition = a test as in a where-clause for whether or not the trigger applies.
Action = one or more SQL statements.
Di er from checks or SQL assertions in that triggers are invoked by the event; the system do esn't have to gure out when a trigger could b e violated.
Options
Notes
More information in on-line do cument or- plsql.html
There are two sp ecial variables new and old, representing the new and old tuple in the change. F old makes no sense in an insert, and new makes no sense in a delete.
Notice: in WHEN we use new and old without a colon, but in actions, a preceding colon is needed.
The action is a PL/SQL statement.
F Simplest form: surround one or more SQL statements with BEGIN and END. F However, select-from-where has a limited form.
Example
Maintain a list of all the bars that raise their price for some b eer by more than $1.
Sells(bar , beer , price) RipoffBars(bar )
CREATE TRIGGER PriceTrig AFTER UPDATE OF price ON Sells FOR EACH ROW WHEN(new.price > old.price + 1.00) BEGIN INSERT INTO RipoffBars VALUES(:new.bar); END; . run