Docsity

Nulls, 3 Valued Logic, Outer Join - Lecture Slides | CS 610, Study notes of Deductive Database Systems

Partial preview of the text

Download Nulls, 3 Valued Logic, Outer Join - Lecture Slides | CS 610 and more Study notes Deductive Database Systems in PDF only on Docsity!

Nulls

In place of a value in a tuple's comp onent.

 Interpretation is not exactly \missing value."

 There could b e many reasons why no value is present, e.g., \value inappropriate."

Comparing Nulls to Values

 3rd truth value UNKNOWN.

 SELECT clause only lists tuples if the condition evaluates to TRUE (UNKNOWN is not sucent).

Example

bar b eer price Jo e's bar Bud NULL

SELECT bar FROM Sells WHERE price < 2.00 OR price >= 2.00;


UNKNOWN UNKNOWN


UNKNOWN

Outerjoin

R (^) ./ S = R ./ S with dangling tuples padded with nulls and included in the result.

 A tuple is dangling if it do esn't join with any other tuple.

R =

A B

1 2 3 4

S =

B C 2 5 2 6 7 8

R (^) ./ S =

A B C 1 2 5 1 2 6 3 4 NULL NULL 7 8

Outerjoin in SQL

A numb er of forms are provided.

 Can b e used either stand-alone (in place of a select-from-where) or to de ne a relation in the FROM-clause. R NATURAL JOIN S R JOIN S ON condition e.g., condition: R :B = S:B R CROSS JOIN S R OUTER JOIN S

 The last of these can b e mo di ed by:

  1. Optional NATURAL in front.
  2. Optional ON condition at end.
  3. Optional LEFT, RIGHT, or FULL b efore OUTER. F LEFT = pad dangling tuples of R only; RIGHT = pad dangling tuples of S only.

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 (covered).
  2. Foreign-keys = referential integrity constraints.
  3. Attribute- and tuple-based checks = constraints within relations.
  4. SQL2 Assertions = global constraints.

F Not found in Oracle.

  1. Oracle Triggers.

F A substitute for assertions.

  1. SQL3 triggers and assertions.

Foreign Keys

In relation R a clause that \attribute A references S (B )" says that whatever 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) );