Advanced Database System - Ch09

23
Integrity Integrity Prof. Yin-Fu Huang Prof. Yin-Fu Huang CSIE, NYUST CSIE, NYUST Chapter 9 Chapter 9

Transcript of Advanced Database System - Ch09

Page 1: Advanced Database System - Ch09

IntegrityIntegrity

Prof. Yin-Fu HuangProf. Yin-Fu HuangCSIE, NYUST CSIE, NYUST

Chapter 9Chapter 9

Page 2: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

An integrity constraint is a boolean expression that is associated with some database and is required to evaluate at all times to TRUE.

e.g. Constraint SC1Is_Empty (S Where Status < 1 Or Status > 100);

Drop Constraint SC1;

9.19.1 IntroductionIntroduction

Page 3: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

The fact that a given variable is of some given type represents an a prior constraint on the variable in question.

Tutorial D examples:1. Constraint SC2

Forall SX (If SX.City = ‘London’ Then SX.Status = 20 End If);

2. Constraint PC3If Exists PX (True)Then Exists PX (PX.Color = Color(‘Blue’)) End If;

9.29.2 A Closer LookA Closer Look

Page 4: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

3. Constraint SC4Forall SX Forall SY (If SX.S# = SY.S# Then SX.Sname = SY.Sname And SX.Status = SY.Status And SX.City = SY.City End If);4. Constraint SSP5Forall SPX Exists SX (Sx.S# = SPX.S#)5. Constraint SSP6Forall SX Forall SPX(If Sx.S# = SPX.S# Then SX.Status 20 Or SPX.Qty 500 End If);

9.29.2 A Closer Look (Cont.)A Closer Look (Cont.)

Page 5: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.39.3 Predicates and PropositionsPredicates and Propositions

A constraint as formally stated is a predicate. When that constraint is checked, arguments are substituted for the

parameters and the predicate is thereby reduced to a proposition –and that proposition is then required to evaluated to TRUE.

Page 6: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.49.4 Relvar Predicates and Database PredicatesRelvar Predicates and Database Predicates

The relvar predicate for R is the logical And or conjunction of all of the constraints that apply to relvar R.

The Golden RuleNo update operation must ever assign to any relvar a valuethat causes its relvar predicate to evaluate to FALSE.

The database predicate for D is the conjunction of all of those relvar predicates.

The Golden RuleNo update operation must ever assign to any database avalue that causes its database predicate to evaluate to FALSE.

Page 7: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.59.5 Checking the ConstraintsChecking the Constraints

The sequence of events:1. Insert the new tuple.2. Check the constraint.3. Undo the update (because the check fails).But this is absurd!

All constraint checking is immediate? Most of the literature argues that “the unit of integrity” is the trans

action and that at least some checking has to be deferred until end-of-transaction (i.e., COMMIT time).

Page 8: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.69.6 Internal vs. External Predicates Internal vs. External Predicates

a formal construct vs. an informal construct. what the data means to the system vs. what the data means to the u

sers. A given internal predicate is the system’s approximation to the cor

responding external predicate. If (s S) = TRUE Then XPS(s) = TRUE

If (s S) = FALSE Then XPS(s) = FALSE s S XPS(s)

Page 9: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.79.7 Correctness vs. ConsistencyCorrectness vs. Consistency

Views too are subject to constraints, and they have relvar predicates, both internal and external.e.g., There exists some city City such that supplier S# is under contract, is named Sname, has status Status, and is located in City.

9.89.8 Integrity and Views Integrity and Views

The system cannot enforce truth, only consistency. If the database contains only true propositions, then it is

consistency, but the converse is not necessarily so. If the database is inconsistency, then it contains at least one

false proposition, but the converse is not necessarily so.

Page 10: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.99.9 A Constraint Classification SchemeA Constraint Classification Scheme

Four broad categories: database, relvar, attribute, and type constraints

Type constraintse.g., Type Weight Possrep { D Decimal (5, 1)

Constraint D > 0.0 And D < 5000.0 };Type constraints are always checked immediately.

Attribute constraintse.g., Var S Base Relation

{ S# S#, Sname Name, Status Integer, City Char } … ;

Page 11: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.99.9 A Constraint Classification Scheme (Cont.)A Constraint Classification Scheme (Cont.)

Relvar and database constraintsA transition constraint is a constraint on the legal transitions th

at a given variable-in particular, a given relvar or a given database- can make from one value to another.

e.g. Constraint TRC1Forall SX’ Forall SX ( SX’.S# SX.S# Or

SX’.Status SX.Status );

Constraint TRC2Forall PX

Sum ( SPX’ Where SPX’.P# = PX.P#, Qty )

Sum ( SPX Where SPX .P# = PX.P#, Qty );

The concept of transition constraints does not apply to type or attribute constraints.

Page 12: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.10 KeysKeys Candidate Keys

Properties:1. Uniqueness2. Irreducibility

Key { <attribute name commalist> } Tuple-level addressing mechanism

Primary Keys and Alternative Keys Exactly one of candidate keys the primary key The others alternative keys

Page 13: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.10 Keys (Cont.)Keys (Cont.) Foreign Keys

A foreign key in R2 is a set of attributes of R2, say FK, such that:1. There exists a relvar R1 (R1 and R2 not necessarily distinc

t) with a candidate key CK.2. It is possible to rename some subset of the attributes of FK,

such that FK becomes FK’ (say) and FK’ and CK are of the same (tuple) type.

3. For all time, each value of FK in the current value of R2 yields a value for FK’ that is identical to the value of CK in some tuple in the current value of R1.

Page 14: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.10 Keys (Cont.)Keys (Cont.)

Points arising:1. It will rarely be necessary in practice to perform any actua

l renaming.2. While every value of FK must appear as a value of CK, th

e converse is not a requirement.3. Simple or composite4. Referencing relvar vs. referenced relvar5. Referential diagrams

S# P# S ← SP → P

6. A given relvar can be both referenced and referencing.R3 → R2 → R1

Page 15: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.10 Keys (Cont.)Keys (Cont.)7. Relvars R1 and R2 in the foreign key definition are not necess

arily distinct.Var Emp Base Relation

{Emp# Emp#, ..., Mgr_emp# Emp#, ...} Key {Emp#} Foreign Key {Rename Mgr_emp# As Emp

#} References Emp;

8. Referential cycles9. Such matches represent certain relationships.10.Referential integrity

The database must not contain any unmatched foreign key values.

Page 16: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.10 Keys (Cont.)Keys (Cont.)

Referential ActionsForeign Key { ... } References <relvar>

On Delete <action> On Update <action>;

<action>• Cascade• Restrict• No Action

Page 17: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.11 Triggers (A Digression)Triggers (A Digression)

A triggered procedure is a precompiled procedure that is stored along with the database and invoked automatically whenever some specified event occurs.e.g., Create Trigger London_Supplier_Insert

Instead Of Insert On London_SupplierReferencing New Row As RFor Each RowInsert Into S ( S#, Sname, Status, City )

Values ( R.S#, R.Sname, R.Status, ‘London’) ;The eventThe conditionThe action

Page 18: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.129.12 SQL FacilitiesSQL Facilities

SQL does not support type constraints at all. SQL does support attribute constraints. SQL does not support relvar constraints as such.

base table constraints SQL does not support database constraints as such.

general constraints (“assertions”)

Page 19: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.129.12 SQL Facilities (Cont.)SQL Facilities (Cont.)

Base Table Constraints1. Candidate keys

Primary Key (<column name commalist>) at most oneUnique (<column name commalist>) any number

2. Foreign keysForeign key (<column name commalist>) References <base table name>

[(<column name commalist>)][On Delete <referential action>][On Update <referential action>]

• Action: No Action, Restrict, Cascade, Set Default, Set Null

Page 20: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

3. Check constraintsCheck (<bool exp>)

e.g.,Create Table Sp(S# S# Not Null, P# P# Not Null, Qty Qty Not Null,Primary Key (S#, P#),Foreign Key(S#) References S

On Delete CascadeOn Update Cascade,

Foreign Key(P#) References POn Delete CascadeOn Update Cascade,

Check (Qty Qty(0) And Qty Qty(5000)));

9.129.12 SQL Facilities (Cont.)SQL Facilities (Cont.)

Page 21: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.129.12 SQL Facilities (Cont.)SQL Facilities (Cont.)

AssertionsCreate Assertion <constraint name>

Check (<bool exp>);Drop Assertion <constraint name>;e.g., Create Assertion SC1 Check

(Not Exists (Select * From S Where S.Status < 0 Or S.Status > 100));

Deferred Checking In SQL, constraints can be defined to be Deferrable or Not

Deferrable.

Page 22: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

9.129.12 SQL Facilities (Cont.)SQL Facilities (Cont.)

Deferrable constraints can be dynamically switched on andoff by means of the statement

Set Constraints <constraint name commalist> <option>;• option: Immediate, Deferred

TriggersCreate Trigger <trigger name>

<before or after> <event> On <base table name>[ Referencing <naming commalist> ][ For Each <row or statement> ][ When ( <bool exp> ) ] <action>;

Drop Trigger <trigger name>;

Page 23: Advanced Database System - Ch09

Advanced Database System Yin-Fu Huang

The End.The End.