Sunteți pe pagina 1din 20

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

The Relational
Model & Relational
Database
Constraints
Chapter 3

Fall 2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified fro m Fundamentals of Database Syst ems (5 /6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.)

CS 338: Computer Applications in Business: Databases


Rice University Data Center

Questions & Review

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 (Fall 2014)

Review
TerminologiesRelational Model
What is a tuple?
A tuple represents a row which contains data elements representing
facts that typically correspond to a real-world entity or relationship

What is an attribute?
A column header which gives some meaning of the data items in that
column

What is a domain of an attribute?


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

Review
Formal Definitions

iClicker: Q1

r(R) denotes
A. a database
B. a relation state
C. a relation schema
D. a tuple

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 (Fall 2014)

iClicker: Q2

Review
Formal Definitions

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


A. cardinality
B. degree
C. domain
D. schema

Review
Definition Summary
Complete the following table

Relation
Attribute
Domain
Tuple
Schema
State
6

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 (Fall 2014)

The Relational
Model & Relational
Database
Constraints
Chapter 2

Summary
Properties of Relations
1 Relation name is distinct from all other relation names in relational schema.
2 Each cell of relation contains exactly one atomic (single) value.
3 Each attribute has a distinct name.
4 Values of an attribute are all from the same domain.
5 Each tuple is distinct; there are no duplicate tuples.
6 Order of attributes has no significance.
7 Order of tuples has no significance, theoretically.
8

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 (Fall 2014)

Relational Model Constraints


Constraints
Restrictions on the actual values in a database state
Constraints are conditions that must hold on all valid relation state
There are three main different types of constraints in the relational model:
1 Inherent constraints (also called implicit constraints)
2 Schema-based constraints (also called explicit constraints)
a Domain Constraints
b Key constraints and constraints on Null
c Entity integrity constraints
d Referential integrity constraints
3 Application-based or semantic constraints or business rules

Relational Model Constraints

Inherent 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

Schema-based constraints
Can be directly expressed in schemas of the data model
Example:

Films have only one director

Application-based constraints
Not directly expressed in schemas (enforced by application programs)
Example:

this years salary increase can be no more than last years

10

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 (Fall 2014)

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,
Atomic means that if we attempt to
Fixed-length strings,
separate an atomic value, the value
Variable-length strings,
itself become meaningless
Date, time, timestamp
Examples:
Money
Social Insurance Number
Age
Other special data types
11

Schema-based Constraints
Key Constraints

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

Superkey (SK)
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
12

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 (Fall 2014)

Schema-based Constraints
Key Constraints
A superkey can have redundant attributes
A more useful concept is that of a KEY (no redundancy)
Minimum
superkey

Key (K)
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
13

Schema-based Constraints
Key Constraints

Example 1
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

14

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 (Fall 2014)

Schema-based Constraints
Key Constraints

Candidate key
In general, a relation schema may have more than one key
Each of the keys is called a candidate key
Primary Key (PK)
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

15

Schema-based Constraints
Key Constraints

In Summary
Candidate key can be any key
Example: SSN, Student ID, Email, etc
.
.
.

Primary key is a designated candidate key


Example: SSN

16

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 (Fall 2014)

Schema-based Constraints
Key Constraints

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

17

Schema-based Constraints
Key Constraints

Example 3
CAR table with two candidate keys
License_Number and Engine_serial_number
License_Number chosen as Primary Key (PK)

18

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 (Fall 2014)

Schema-based Constraints
NOT NULL Constraint

NOT NULL Constraint


This condition satisfies when an attribute (Ai) has some
value (i.e. not empty or inapplicable)

Example
If car must have a valid, non-Null value for the
SerialNo attribute, then the SerialNo of Car is required
to be NOT NULL
19

Entity Integrity Constraint


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

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

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

10

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

Referential Integrity Constraint


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)

Summary:

Referential integrity constraint is used to maintain consistency among


tuples of the two relations
21

Ex ample
Referential Integrity Constraint

22

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

11

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

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

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

12

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

Ex ample

Referential Integrity Constraints for COMPANY


database

25

Ex ample
Constraints for UNIVERSITY database

26

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

13

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

Other Types of Constraints


Semantic integrity 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
Example:

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

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

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

14

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

Ex ample
Populated Database State

29

Ex ample
Populated Database State

30

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

15

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

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

Ex ample 1
The Insert Operation

Figure 3.6
insert <John, H, Doe, Null, 1960-0405,55 Main Street, Miami, FL, 33165, M,
28000, Null, 4> into EMPLOYEE
Result
NOT OK
Violates the entity integrity constraint (Null for PK),
Will be rejected

32

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

16

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

Ex ample 2
The Insert Operation

Figure 3.6
insert <John, H, Doe, 999887777,
1960-04-05, 55 Main Street, Miami, FL,
33165, M, 28000, 987654321, 4> into
EMPLOYEE
Result
NOT OK
Violates the key constraint because another tuple with the
same Ssn value already exists in the EMPLOYEE relation
Will be rejected

33

Ex ample 3
The Insert Operation

Figure 3.6
insert <John, H, Doe, 677678989,
1960-04-05, 55 Main Street, Miami, FL,
33165, M, 28000, 987654321, 7> into
EMPLOYEE
Result
NOT OK
Violates the referential integrity constraint since Dno=7
does not exist
Will be rejected

34

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

17

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

Ex ample 4
The Insert Operation

Figure 3.6
insert <John, H, Doe, 677678989,
1960-04-05, 55 Main Street, Miami, FL,
33165, M, 28000, 987654321, 4> into
EMPLOYEE
Result
OK
Satisfies all constraints, it is acceptable

35

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

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

18

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

Ex ample 1
The Delete Operation

Figure 3.6
delete the WORKS_ON tuple with
ESSN=999887777 and PNO = 30
Result
OK

delete the EMPLOYEE tuple with Ssn=999887777


Result
NOT OK
tuples in WORKS_ON refer to this tuple (referential
integrity violation)
37

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

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

19

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

Ex ample 1
The Update Operation

Figure 3.6
Update the salary of the EMPLOYEE tuple with
Ssn = 999887777 to 28000
Result
OK

Update the Dno of the EMPLOYEE tuple with


Ssn = 999887777 to 7
Result
NOT OK
Violates referential integrity constraint Dno=7 does not
exist
39

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

40

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

20

S-ar putea să vă placă și