04 constraints and updates

20
1 CS 338: Computer Applications in Business: Databases (Fall 2014) ©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.) CS 338: Computer Applications in Business: Databases The Relational Model & Relational Database Constraints ©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning Slides adapted and modified from Fundamentals of Database Systems (5 /6) (Elmasri et al.), Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.) Rice University Data Center Fall 2014 Chapter 3 Questions & Review 2

Upload: nathan-wilson

Post on 19-Dec-2015

218 views

Category:

Documents


0 download

DESCRIPTION

Course Notes on Database design and use.

TRANSCRIPT

Page 1: 04 Constraints and Updates

1

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

CS 338: Computer Applications in Business: Databases

The Relational Model & Relational Database Constraints

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5 /6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.) Rice University Data Center

Fall 2014

Chapter 3

Questions & Review

2

Page 2: 04 Constraints and Updates

2

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Review Terminologies…Relational Model

A tuple represents a row which contains data elements representing facts that typically correspond to a real-world entity or relationship

3

What is a tuple?What is a tuple?

A column header which gives some meaning of the data items in that column

What is an attribute?What is an attribute?

• Each attribute has a domain or a set of valid (atomic) values• The domain may have a data type and/or format

What is a domain of an attribute?What is a domain of an attribute?

Review Formal Definitions…

A. a database

B. a relation state

C. a relation schema

D. a tuple

4

r(R) denotes r(R) denotes

iClicker: Q1

Page 3: 04 Constraints and Updates

3

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Review Formal Definitions…

A. cardinality

B. degree

C. domain

D. schema

5

the number of tuples m of a relation represents ____ of a relationthe number of tuples m of a relation represents ____ of a relation

iClicker: Q2

Review Definition Summary

6

Complete the following table…Complete the following table…

Nathan Wilson
RelationAttributeDomainTupleSchemaState
Nathan Wilson
Page 4: 04 Constraints and Updates

4

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

The Relational Model & Relational Database ConstraintsChapter 2

7

Summary Properties of Relations

Relation name is distinct from all other relation names in relational schema.1

Each cell of relation contains exactly one atomic (single) value.2

8

Each attribute has a distinct name.3

Values of an attribute are all from the same domain.4

Each tuple is distinct; there are no duplicate tuples.5

Order of attributes has no significance.6

Order of tuples has no significance, theoretically.7

Page 5: 04 Constraints and Updates

5

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Relational Model Constraints

• Restrictions on the actual values in a database state

• Constraints are conditions that must hold on all valid relation state

Constraints

Inherent constraints (also called implicit constraints)1

Schema-based constraints (also called explicit constraints)2

Application-based or semantic constraints or business rules3

There are three main different types of constraints in the relational model:

Domain Constraintsa

Key constraints and constraints on Nullb

Entity integrity constraintsc

Referential integrity constraintsd

9

Relational Model Constraints

• Refers to the constraints that are inherent in the model• Examples:

• Ordering of tuples in the relation • No duplicated tuples allowed in a relation

10

Inherent constraints

• Can be directly expressed in schemas of the data model• Example:

• Films have only one director

Schema-based constraints

• Not directly expressed in schemas (enforced by application programs) Example:• this year’s salary increase can be no more than last year’s

Application-based constraints

Page 6: 04 Constraints and Updates

6

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Schema-based Constraints Domain Constraints

Every value in a tuple must be from the domain of its attribute Must be an atomic value value is indivisible No object or complex data type allowed New domains must be created from existing data types

Atomic data types include: Standard numeric data types for integers and real numbers, Character, Booleans, Fixed-length strings, Variable-length strings, Date, time, timestamp Money Other special data types

11

• Atomic means that if we attempt to separate an atomic value, the value itself become meaningless

• Examples:• Social Insurance Number• Age

Schema-based Constraints Key Constraints

12

• Superkey is a subset of attributes that uniquely identifies the tuples of a relation

• No two distinct tuples in any valid relation state r(R) can have the same

value for SK

• That is, for any distinct tuples t1 and t2 in r(R),

t1[SK] ≠ t2[SK]

where SK is superkey satisfying uniqueness constraint

Superkey (SK)

• A relation is defined as a set of tuples• all elements of a set are distinct; no two tuples can have the same

combination of values for all their attributes

Page 7: 04 Constraints and Updates

7

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Schema-based Constraints Key Constraints

13

• Removing any attribute A from K leaves a set of attributes K that is not a superkey of R any more

• Key satisfies two properties:• Two distinct tuples in any state of relation cannot have identical

values for (all) attributes in key uniqueness (i.e., it is a superkey)

• Minimal superkey• Cannot remove any attributes and still have uniqueness constraint (in

the above condition) hold

Key (K)

• A superkey can have redundant attributes

• A more useful concept is that of a KEY (no redundancy)

Schema-based Constraints Key Constraints

• Consider STUDENT relation• The attribute set {Ssn} is a key of STUDENT because no two student

tuples can have the same value for Ssn

• Any set of attributes that includes Ssn, for example, {Ssn, Name, Age} is a superkey

• However, the superkey {Ssn, Name, Age} is not a key of STUDENT• Any key is a also a superkey (but not vice versa)

• Any set of attributes that includes a key is a superkey

• A minimal superkey is also a key

Example 1

14

Nathan Wilson
Minimum superkey
Nathan Wilson
Page 8: 04 Constraints and Updates

8

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Schema-based Constraints Key Constraints

• In general, a relation schema may have more than one key• Each of the keys is called a candidate key

Candidate key

15

• If a relation has several candidate keys, one is chosen arbitrarily to be the primary key

• The primary key value is used to uniquely identify each tuple in a relation

• The primary key attributes are underlined• General rule: Choose a primary key with a single attribute or a small

number of attributes• Other candidate keys are designated as unique keys and are not underlined

Primary Key (PK)

Schema-based Constraints Key Constraints

Candidate key can be any key

Example: SSN, Student ID, Email, etc…

In Summary…

16

Primary key is a designated candidate key

Example: SSN

.

.

.

Page 9: 04 Constraints and Updates

9

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Schema-based Constraints Key Constraints

• Consider the CAR relation schema:

• CAR(State, Reg#, SerialNo, Make, Model, Year)

• We chose SerialNo as the primary key

• Key constraint statement:• Not two tuples in a relation must have the same value

for the key attribute(s)

Example 2

17

Schema-based Constraints Key Constraints

• CAR table with two candidate keys –

• License_Number and Engine_serial_number

• License_Number chosen as Primary Key (PK)

Example 3

18

Page 10: 04 Constraints and Updates

10

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

• If car must have a valid, non-Null value for the

SerialNo attribute, then the SerialNo of Car is required

to be NOT NULL

Schema-based Constraints NOT NULL Constraint

• This condition satisfies when an attribute (Ai) has some

value (i.e. not empty or inapplicable)

NOT NULL Constraint

19

Example

Entity Integrity Constraint

• The primary key attributes of each relation schema R in S

cannot have null values in any tuple of r(R)

• This is because primary key values are used to identify the

individual tuples

• t[PK] ≠ null for any tuple t in r(R)

• If PK has several attributes, null is not allowed in any of these

attributes

Entity Integrity Constraint

20

Summary:• Primary Key (PK) can not be NULL• Why?

Page 11: 04 Constraints and Updates

11

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Referential Integrity Constraint

• A constraint used to specify a relationship among tuples in

two relation schemas:

• The referencing relation and the referenced relation

• Works with the notion of a Foreign Key (FK)• Used to establish and enforce a link between data in two tables (e.g.

primary key in one table used as an attribute in another table)• Example: Dnumber is PK in Department but works as FK in

Employee (Dno)

Referential Integrity Constraint

21

Summary:Referential integrity constraint is used to maintain consistency among tuples of the two relations

Ex ample Referential Integrity Constraint

22

Page 12: 04 Constraints and Updates

12

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Referential Integrity Constraint

• Foreign Key rules:

1. Attribute(s) in FK of R1 have the same domain(s) as the primary key

attributes PK of R2

• attributes FK are said to reference or refer to the Relation R2

2. Value of FK in a tuple t1 of current state r(R1) either occurs as a value of PK for some tuple t2 in current state r(R2) or is NULL.

• In the former case, we have t1[FK]= t2[PK] and we say that the tuple t1 references

or refers to the tuple t2

• Tuples in the referencing relation R1 have attributes FK (called

foreign key attributes) that reference the primary key attributes

PK of the referenced relation R2.

23

Displaying Relational Database with Constraints

• Each relation schema can be displayed as a row of attribute

names

• The name of the relation is written above the attribute

names

• The primary key attribute (or attributes) will be underlined

• A foreign key (referential integrity) constraint is displayed

as a directed arc (arrow) from the foreign key attributes to

the referenced table

24

Page 13: 04 Constraints and Updates

13

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Ex ample Referential Integrity Constraints for COMPANY

database

25

Ex ample Constraints for UNIVERSITY database

26

Page 14: 04 Constraints and Updates

14

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Other Types of Constraints

• May have to be specified and enforced on a relational database• Use triggers and assertions • Enforced on database using an application program

• More common to check for these types of constraints within the application programs

Semantic integrity constraints

27

Example:“the max. no. of hours per employee for all projects he or she works on is 56 hrs per week”

Populated Database State

• Each relation will have many tuples in its current relation state

• The relational database state is the union of all the individual relation states

• Whenever the database is changed, a new state is generated

Basic operations that change the states of relations in the database: Insert

Delete

Update (or Modify)

28

Page 15: 04 Constraints and Updates

15

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

Ex ample Populated Database State

29

Ex ample Populated Database State

30

Page 16: 04 Constraints and Updates

16

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

The Insert Operation

Provides a list of attribute values for a new tuple t that is to be inserted into a relation R

Can violate any of the four types of constraints • Domain constraint:• if one of the attribute values provided for the new tuple is not of the

specified attribute domain

• Key constraint:• if the value of a key attribute in the new tuple already exists in another

tuple in the relation

• Referential integrity:• if a foreign key value in the new tuple references a primary key value that

does not exist in the referenced relation• Entity integrity:• if the primary key value is null in the new tuple

31

• NOT OK• Violates the entity integrity constraint (Null for PK), • Will be rejected

Ex ample 1 The Insert Operation

insert <‘John’, ‘H’, ‘Doe’, Null, ‘1960-04-05’,’55 Main Street, Miami, FL, 33165’, M, 28000, Null, 4> into EMPLOYEE

Figure 3.6

32

Result

Page 17: 04 Constraints and Updates

17

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

• NOT OK• Violates the key constraint because another tuple with the

same Ssn value already exists in the EMPLOYEE relation • Will be rejected

Ex ample 2 The Insert Operation

insert <‘John’, ‘H’, ‘Doe’, ‘999887777’, ‘1960-04-05’, ’55 Main Street, Miami, FL, 33165’, M, 28000, ‘987654321’, 4> into EMPLOYEE

Figure 3.6

33

Result

• NOT OK• Violates the referential integrity constraint since Dno=7

does not exist• Will be rejected

Ex ample 3 The Insert Operation

insert <‘John’, ‘H’, ‘Doe’, ‘677678989’, ‘1960-04-05’, ’55 Main Street, Miami, FL, 33165’, M, 28000, ‘987654321’, 7> into EMPLOYEE

Figure 3.6

34

Result

Page 18: 04 Constraints and Updates

18

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

• OK• Satisfies all constraints, it is acceptable

Ex ample 4 The Insert Operation

insert <‘John’, ‘H’, ‘Doe’, ‘677678989’, ‘1960-04-05’, ’55 Main Street, Miami, FL, 33165’, M, 28000, ‘987654321’, 4> into EMPLOYEE

Figure 3.6

35

Result

The Delete Operation

Can violate only referential integrity If tuple being deleted is referenced by foreign keys from other tuples

• Options for delete:• Restrict: Reject deletion

• Cascade: Attempt to propagate the deletion by deleting tuples that reference the tuple being deleted

• Set null or default: Modify the referencing attribute values that cause the violation

36

Page 19: 04 Constraints and Updates

19

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

OK

Ex ample 1 The Delete Operation

delete the WORKS_ON tuple with ESSN=‘999887777’ and PNO = 30

Figure 3.6

37

Result

• NOT OK• tuples in WORKS_ON refer to this tuple (referential

integrity violation)

delete the EMPLOYEE tuple with Ssn=‘999887777’

Result

The Update Operation

Necessary to specify a condition on attributes of relation Select the tuple (or tuples) to be modified

• Any of the other constraints may also be violated, depending on the attribute being updated:• Updating the primary key (PK):• Similar to a DELETE followed by an INSERT • Need to specify similar options to DELETE

• Updating a foreign key (FK):• May violate referential integrity

• Updating an ordinary attribute (neither PK nor FK):• Can only violate domain constraints

38

Page 20: 04 Constraints and Updates

20

CS 338: Computer Applications in Business: Databases (Fall 2014)

©1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage LearningSlides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)

OK

Ex ample 1 The Update Operation

Update the salary of the EMPLOYEE tuple with Ssn = ‘999887777’ to 28000

Figure 3.6

39

Result

• NOT OK• Violates referential integrity constraint Dno=7 does not

exist

Update the Dno of the EMPLOYEE tuple with Ssn = ‘999887777’ to 7

Result

Practice Question(s)Do exercise 3.11 on page 81 (6th Edition)

40