Docsity

Constraints-Database systems-Lecture 09 Slides-Computer Science, Slides of Database Management Systems (DBMS)

Toggle sidebar
bg1
Constraints
Commercial relational systems allowmuch more
\๎˜Œne-tuning" of constraints than do the modeling
languages we learned earlier.
๎˜
In essence: SQL programming is used to
describe constraints.
Outline
1. Primary key declarations (already covered).
2. Foreign-keys = referential integrity
constraints.
3. Attribute- and tuple-based checks =
constraints within relations.
4. SQL Assertions = global constraints.
โœฆ
Not found in Oracle.
5. Oracle Triggers.
โœฆ
A substitute for assertions.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Constraints-Database systems-Lecture 09 Slides-Computer Science and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

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

  1. Primary key declarations (already covered).
  2. Foreign-keys = referential integrity constraints.
  3. Attribute- and tuple-based checks = constraints within relations.
  4. SQL Assertions = global constraints.

F Not found in Oracle.

  1. Oracle Triggers.

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:

  1. Insert or up date a Sells tuple so it refers to a nonexistent b eer. F Always rejected.
  2. Delete or up date a Beers tuple that has a beer value some Sells tuples refer to. a) Default: reject.

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 CHECK();

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

  1. Can omit OR REPLACE. But if you do, it is an error if a trigger of this name exists.
  2. AFTER can b e BEFORE.
  3. If the relation is a view, AFTER can b e INSTEAD OF. F Useful for allowing \mo di cations" to a view; you mo dify the underlying relations instead.
  4. INSERT can b e DELETE or UPDATE OF . F Also, several conditions like INSERT ON Sells can b e connected by OR.
  5. FOR EACH ROW can b e omitted, with an imp ortant e ect: the action is done once for the relation(s) consisting of all changes.

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