35
Introduction to Data Management CSE 344 Lecture 16: Constraints CSE 344 - Fall 2013 1

Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Introduction to Data Management CSE 344

Lecture 16: Constraints

CSE 344 - Fall 2013 1

Page 2: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Announcements

•  WQ6 due Thursday (there is no WQ5…)

•  Homework 4 posted, due Friday

•  Midterm: Monday, November 4th, in class

CSE 344 - Fall 2013 2

Page 3: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Midterm

•  All material up to and including XML –  SQL, basic evaluation + indexes, RA, datalog-

with-negation, RC, XML/XPath/XQuery •  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

CSE 344 - Fall 2013 3

Page 4: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Where We Are?

We are learning about database design •  How to design a database schema? •  Last time: Real world -> ER Diagrams -> Relations Next, we will learn more about good schemas •  Today: Constraints and data integrity •  Next time: Schema normalization, then Views

CSE 344 - Fall 2013 4

Page 5: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Person

Company Product

buys

makes

employs

name CEO

price

address name ssn

address

name

5

Page 6: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Person

Company Product

buys

makes

employs

name CEO

price

address name ssn

address

name

6

What does this say ?

Page 7: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

7

Weak Entity Sets

University Team affiliation

number sport name

Team(sport, number, universityName) University(name)

CSE 344 - Fall 2013

What does this say ?

Page 8: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

What Are the Keys of R ?

R

A

B

S

T

V

Q

U W

V

Z

C

D E G

K

H

F L

Page 9: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Integrity Constraints Motivation

•  ICs help prevent entry of incorrect information •  How? DBMS enforces integrity constraints

–  Allows only legal database instances (i.e., those that satisfy all constraints) to exist

–  Ensures that all necessary checks are always performed and avoids duplicating the verification logic in each application

CSE 344 - Fall 2013 9

An integrity constraint is a condition specified on a database schema that restricts the data that can be stored in an instance of the database.

Page 10: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Constraints in E/R Diagrams

Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a person. Single-value constraints: a person can have only one father. Referential integrity constraints: if you work for a company, it must exist in the database. Other constraints: peoples’ ages are between 0 and 150.

CSE 344 - Fall 2013 10

Page 11: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Keys in E/R Diagrams

address name ssn

Person

Product

name category

price

No formal way to specify multiple keys in E/R diagrams

Underline:

11

Page 12: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Single Value Constraints

makes

makes

v. s.

CSE 344 - Fall 2013 12

Page 13: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Referential Integrity Constraints

Company Product makes

Company Product makes

Each product made by at most one company. Some products made by no company

Each product made by exactly one company. CSE 344 - Fall 2013 13

Page 14: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Other Constraints

Company Product makes <100

CSE 344 - Fall 2013 14

Q: What does this mean ? A: A Company entity cannot be connected by relationship to more than 99 Product entities

Page 15: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

15

Constraints in SQL

Constraints in SQL: •  Keys, foreign keys •  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 - Fall 2013

Page 16: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

16

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 - Fall 2013

Page 17: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

17

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)

CSE 344 - Fall 2013

Page 18: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Other Keys

CSE 344 - Fall 2013 18

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

Page 19: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Foreign Key Constraints

CSE 344 - Fall 2013 19

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 20: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

20

Name Category

Gizmo gadget

Camera Photo

OneClick Photo

ProdName Store

Gizmo Wiz

Camera Ritz

Camera Wiz

Product Purchase

CSE 344 - Fall 2013

Foreign Key Constraints

Page 21: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Foreign Key Constraints

•  Example with multi-attribute primary key

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

CSE 344 - Fall 2013 21

CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20),

date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

Page 22: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

22

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 - Fall 2013

Page 23: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

23

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

CSE 344 - Fall 2013

Page 24: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Maintaining Referential Integrity

CSE 344 - Fall 2013 24

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 25: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Constraints on Attributes and Tuples

•  Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition !

•  Constraints on tuples CHECK condition

CSE 344 - Fall 2013 25

Page 26: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Constraints on Attributes and Tuples

CSE 344 - Fall 2013 26

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

Page 27: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Constraints on Attributes and Tuples

CSE 344 - Fall 2013 27

CREATE TABLE Product ( productID CHAR(10),

name CHAR(30), category VARCHAR(20), price INT CHECK (price > 0),

PRIMARY KEY (productID), UNIQUE (name, category))

Page 28: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

28

CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN

(SELECT Product.name FROM Product), date DATETIME NOT NULL)

CSE 344 - Fall 2013

Constraints on Attributes and Tuples

What is the difference from

Foreign-Key ?

What does this constraint do?

Page 29: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

29

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 - Fall 2013

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

Page 30: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

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 - Fall 2013 30

Page 31: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

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 - Fall 2013 31

Page 32: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Database Triggers Example

CSE 344 - Fall 2013 32

When Product.price is updated, if it is decreased then set Product.category = ‘On sale’

Page 33: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Database Triggers Example

CSE 344 - Fall 2013 33

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 34: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

SQL Server Example

CSE 344 - Fall 2013 34

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 35: Introduction to Data Management CSE 344€¦ · Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a

Discussion

•  Both constraints and triggers are tools that help us keep the database consistent

•  What are their pros and cons?

CSE 344 - Fall 2013 35