Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName...

34
Introduction to Data Management CSE 344 Lecture 16: Constraints CSE 344 - Winter 2014 1

Transcript of Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName...

Page 1: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Introduction to Data Management

CSE 344

Lecture 16: Constraints

CSE 344 - Winter 2014 1

Page 2: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Announcements

• WQ5 due tonight

– deadline extended only for this WQ

• HW4 due tomorrow

• HW 5 posted, due next Thursday (02/20)

• No class/office hour on Monday, February 17

(President’s day)

• Midterm: Wednesday, February 19, in class

CSE 344 - Winter 2014 2

Page 3: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Midterm

• All material up to and including Lecture 15

– SQL, basic evaluation + indexes, RA, datalog-with-

negation, RC, XML/XPath/XQuery, E/R diagram

• Open books, open notes

– Don’t waste paper printing stuff. Normally, you

shouldn’t need any notes during the exam. My

suggestion is to print, say, 5-6 selected slides from

the lecture notes that you had trouble with, and to

print your own homework, just in case you forget

some cool solution you used there.

– Make sure you understand all the concepts! 3

Page 4: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Where We Are?

We are learning about database design

• How to design a database schema?

• Last time: Real world -> E/R Diagrams -> Relations

Next, we will learn more about good schemas

• Today: Constraints and data integrity

– Reading: 7.1, 7.2, 7.4, 7.5

• Next time: Schema normalization, then Views

CSE 344 - Winter 2014 4

Page 5: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

5

Constraints in SQL

Constraints in SQL:

• Keys, foreign keys

– you already know these!

• Attribute-level constraints

• Tuple-level constraints

• Global constraints: assertions

• The more complex the constraint, the harder it is to

check and to enforce

simplest

Most

complex

CSE 344 - Winter 2014

Page 6: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

6

Key Constraints

OR:

CREATE TABLE Product (

name CHAR(30) PRIMARY KEY,

category VARCHAR(20))

CREATE TABLE Product (

name CHAR(30),

category VARCHAR(20)

PRIMARY KEY (name))

Product(name, category)

CSE 344 - Winter 2014

How can we specify both name and category as PK?

Page 7: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

7

Keys with Multiple Attributes

CREATE TABLE Product (

name CHAR(30),

category VARCHAR(20),

price INT,

PRIMARY KEY (name, category))

Name Category Price

Gizmo Gadget 10

Camera Photo 20

Gizmo Photo 30

Gizmo Gadget 40

Product(name, category, price)

How can we specify more than one keys?

Page 8: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Other Keys

CSE 344 - Winter 2014 8

CREATE TABLE Product (

productID CHAR(10),

name CHAR(30),

category VARCHAR(20),

price INT,

PRIMARY KEY (productID),

UNIQUE (name, category))

There is at most one PRIMARY KEY;

there can be many UNIQUE

How can we specify foreign keys?

Page 9: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Foreign Key Constraints

CSE 344 - Winter 2014 9

CREATE TABLE Purchase (

prodName CHAR(30)

REFERENCES Product(name),

date DATETIME)

prodName is a foreign key to Product(name)

name must be a key in Product

Referential

integrity

constraints

May write

just

Product

if name is

PK

Page 10: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

10

Name Category

Gizmo gadget

Camera Photo

OneClick Photo

ProdName Store

Gizmo Wiz

Camera Ritz

Camera Wiz

Product Purchase

CSE 344 - Winter 2014

Foreign Key Constraints

CREATE TABLE Purchase (

prodName CHAR(30)

REFERENCES Product(name),

date DATETIME)

Page 11: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Foreign Key Constraints

• Example with multi-attribute primary key

• (name, category) must be a KEY in Product

CSE 344 - Winter 2014 11

CREATE TABLE Purchase (

prodName CHAR(30),

category VARCHAR(20),

date DATETIME,

FOREIGN KEY (prodName, category)

REFERENCES Product(name, category)

Page 12: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

12

Name Category

Gizmo gadget

Camera Photo

OneClick Photo

ProdName Store

Gizmo Wiz

Camera Ritz

Camera Wiz

Product Purchase

What happens during updates ?

Types of updates:

• In Purchase: insert/update

• In Product: delete/update

CSE 344 - Winter 2014

Can you think of

some options?

Page 13: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

13

What happens during updates ?

• SQL has three policies for maintaining referential integrity:

• Reject violating modifications (default)

• Cascade: after delete/update do delete/update

• Set-null set foreign-key field to NULL – results in dangling tuples, that won’t participate in

joins

• Which policy to use? Depends on the application

CSE 344 - Winter 2014

Page 14: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Maintaining Referential Integrity

CSE 344 - Winter 2014 14

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 )

Page 15: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Constraints on

Attributes and Tuples

• Constraints on attributes:

NOT NULL -- obvious meaning...

CHECK condition -- any condition !

• Constraints on tuples

CHECK condition

• NOT NULL:

– (i) we cannot insert a tuple with null attribute value, and

(ii) cannot use SET NULL on update

CSE 344 - Winter 2014 15

Page 16: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Constraints on

Attributes and Tuples

CSE 344 - Winter 2014 16

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))

Attribute-based

Tuple-based

Page 17: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

CHECK condition

• Attribute-based

– is checked whenever (and only when) any tuple gets a

new value of that attribute by UPDATE or INSERT

– can involve only one attribute

• Tuple-based

– is checked every time a tuple is inserted/updated in that

relation

– can involve one or more attributes

• For both,

– modification is rejected if condition is not satisfied

– Can also use other relations

(e.g. attr-based) B int CHECK B IN (SELECT C FROM R)

Page 18: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Constraints on

Attributes and Tuples

CSE 344 - Winter 2014 18

CREATE TABLE Product (

productID CHAR(10),

name CHAR(30),

category VARCHAR(20),

price INT CHECK (price > 0),

PRIMARY KEY (productID),

UNIQUE (name, category))

Page 19: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

19

CREATE TABLE Purchase (

prodName CHAR(30)

CHECK (prodName IN

(SELECT Product.name

FROM Product),

date DATETIME NOT NULL)

CSE 344 - Winter 2014

Constraints on

Attributes and Tuples

What

is the difference from

Foreign-Key ?

What does this constraint do?

Page 20: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

20

CREATE TABLE Purchase (

prodName CHAR(30)

CHECK (prodName IN

(SELECT Product.name

FROM Product),

date DATETIME NOT NULL)

Constraints on

Attributes and Tuples

What

is the difference from

Foreign-Key ?

What does this constraint do?

Constraints on attributes are only checked when the value of the attribute

changes (so they could potentially be violated by other changes).

So, unlike a FK, if Product changes, this check won’t catch the problem

Page 21: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

21

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)

)

But most DBMSs do not implement assertions

Because it is hard to support them efficiently

Instead, they provide triggers

Page 22: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

CSE 344 - Winter 2014 22

Assertion and Trigger

• An assertion is a Boolean-valued expression that

must be true all the time

o Easy for programmers

o Hard for DBMS to implement

o DBMS has to deduce whether a change can affect the

truthfulness of an assertion

• A trigger is a series of actions associated with

certain events (like insert, update, delete) o More frequently used

Page 23: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

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

• Trigger is awakened when Event occurs, then it

tests the Condition, if satisfied, Action is

performed, otherwise nothing happens 23

Page 24: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

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 2014 24

Page 25: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 25

When Product.price is updated, if it is decreased then

set Product.category = ‘On sale’

Page 26: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 26

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’

Page 27: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 27

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

CREATE TRIGGER statement

Page 28: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 28

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

• Whether the trigger uses the db state BEFORE or AFTER

the triggering event

• For BEFORE, if condition is true, the triggering event is

executed irrespective of whether the condition still holds

Page 29: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 29

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

• To refer to the tuple being modified

• Can give name to old or new ROW (tuple) or TABLE

• Options: INSERT, DELETE, UPDATE (OF)

• OLD (resp. NEW) ROW is disallowed for INSERT (resp. DELETE)

Page 30: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 30

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

• Whether the trigger executes once for each modified row

or once for all the modifications made by the SQL statement

• FOR EACH ROW vs. STAEMENT

Page 31: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 31

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

“Condition” to be tested

Page 32: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Database Triggers Example

CSE 344 - Winter 2014 32

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

•“Action” with one or more SQL statements

• Multiple SQL statements are separated by ;

and all within BEGIN.. END

Page 33: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

SQL Server Example

CSE 344 - Winter 2014 33

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

Page 34: Introduction to Data Management CSE 344€¦ · Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Product Purchase CSE 344 - Winter 2014 Foreign

Summary

• Both constraints and triggers are tools that

help us keep the database consistent, have

their pros and cons

• We learnt

– Key/Referential Integrity constraints

– Attribute-based CHECK constraints

– Tuple-based CHECK constraints

– Assertions

– Triggers

CSE 344 - Winter 2014 34