Section 7 CSE 344 - Winter 2015 1 Foreign Key Constraints Example - - PowerPoint PPT Presentation

section 7
SMART_READER_LITE
LIVE PREVIEW

Section 7 CSE 344 - Winter 2015 1 Foreign Key Constraints Example - - PowerPoint PPT Presentation

Section 7 CSE 344 - Winter 2015 1 Foreign Key Constraints Example with multi-attribute primary key CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES


slide-1
SLIDE 1

Section 7

CSE 344 - Winter 2015 1

slide-2
SLIDE 2

Foreign Key Constraints

  • Example with multi-attribute primary key
  • (name, category) must be a KEY in Product

CSE 344 - Winter 2015 2

CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

slide-3
SLIDE 3

3

Name Category Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz

Product Purchase

What happens when data changes?

Types of updates:

  • In Purchase: insert/update
  • In Product: delete/update

CSE 344 - Winter 2015

slide-4
SLIDE 4

4

  • SQL has three policies for maintaining

referential integrity:

  • NO ACTION reject violating modifications

(default)

  • CASCADE after delete/update do delete/update
  • SET NULL set foreign-key field to NULL
  • SET DEFAULT set foreign-key field to default

value

– need to be declared with column, e.g., CREATE TABLE Product (pid INT DEFAULT 42)

CSE 344 - Winter 2015

What happens when data changes?

slide-5
SLIDE 5

Maintaining Referential Integrity

CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category) ON UPDATE CASCADE ON DELETE SET NULL )

Name Category Gizmo gadget Camera Photo OneClick Photo ProdName Category Gizmo Gizmo Snap Camera EasyShoot Camera

Product Purchase

slide-6
SLIDE 6

Constraints on Attributes and Tuples

  • Constraints on attributes:

NOT NULL

  • - obvious meaning...

CHECK condition

  • - any condition !
  • Constraints on tuples

CHECK condition

CSE 344 - Winter 2015 6

slide-7
SLIDE 7

Constraints on Attributes and Tuples

CSE 344 - Winter 2015 7

CREATE TABLE R ( A int NOT NULL, B int CHECK (B > 50 and B < 100), C varchar(20), D int, CHECK (C >= 'd' or D > 0))

slide-8
SLIDE 8

Constraints on Attributes and Tuples

CSE 344 - Winter 2015 8

CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT CHECK (price > 0), PRIMARY KEY (productID), UNIQUE (name, category))

slide-9
SLIDE 9

9

CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN (SELECT Product.name FROM Product), date DATETIME NOT NULL)

CSE 344 - Winter 2015

Constraints on Attributes and Tuples

What is the difference from Foreign-Key ? What does this constraint do?

slide-10
SLIDE 10

10

General Assertions

CREATE ASSERTION myAssert CHECK (NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) )

CSE 344 - Winter 2015

But most DBMSs do not implement assertions Because it is hard to support them efficiently Instead, they provide triggers

slide-11
SLIDE 11

Database Triggers

  • Event-Condition-Action rules
  • Event

– Can be insertion, update, or deletion to a relation

  • Condition

– Can be expressed on DB state before or after event

  • Action

– Perform additional DB modifications

CSE 344 - Winter 2015 11

slide-12
SLIDE 12

More About Triggers

  • Row-level trigger

– Executes once for each modified tuple

  • Statement-level trigger

– Executes once for all tuples that are modified in a SQL statement

CSE 344 - Winter 2015 12

slide-13
SLIDE 13

Database Triggers Example

CSE 344 - Winter 2015 13

CREATE TRIGGER ProductCategories AFTER UPDATE OF price ON Product REFERENCING OLD ROW AS OldTuple NEW ROW AS NewTuple FOR EACH ROW WHEN (OldTuple.price > NewTuple.price) UPDATE Product SET category = ‘On sale’ WHERE productID = OldTuple.productID When Product.price is updated, if it is decreased then set Product.category = ‘On sale’

slide-14
SLIDE 14

SQL Server Example

CSE 344 - Winter 2015 14

CREATE TRIGGER ProductCategory ON Product AFTER UPDATE AS BEGIN UPDATE Product SET category=‘sale’ WHERE productID IN (SELECT i.productID from inserted i, deleted d WHERE i.productID = d.productID AND i.price < d.price) END

slide-15
SLIDE 15

15

Boyce-Codd Normal Form

There are no “bad” FDs:

  • Definition. A relation R is in BCNF if:

Whenever Xà B is a non-trivial dependency, then X is a superkey. Equivalently: Definition. A relation R is in BCNF if: ∀ X, either X+ = X or X+ = [all attributes]

CSE 344 – Winter 2015

slide-16
SLIDE 16

Problem 1

CSE 344 - Winter 2015 16

R(A,B,C,D,E,F,G) A à D D à C F à E,G D,C à B,F

From A à D, {A}+ = {A,B,C,D,E,F,G}, it is useless From DàC, {D}+ = {D,C,B,F,E,G}, we can decompose R into R1 = {D,C,B,F,E,G} and R2 = {A,D} From F à E,G, {F}+ = {F,E,G} so we can further decompose R1 into: R11 = {E,F,G} and R12 = {C,D,B,F}

slide-17
SLIDE 17

Problem 2

CSE 344 - Winter 2015 17

R(A,B,C,D,E,F) A à BC D à AF

From A à BC, {A}+ = {A,B,C}, since closure is not {A,B,C,D,E,F} this violates BCNF. So decompose R into R1 = {A,B,C} and R2 = {A,D,E,F} R1 is in BCNF. From Dà AF, {D}+ = {D,A,F} which violates BCNF. So we split R2 into: R21= {D,A,F} and R22 = {D,E}