0% found this document useful (0 votes)
59 views21 pages

5 - Chapter 2 - Relational Model of Data - P4

The document discusses using relational algebra to express constraints on relations in a database, such as foreign key constraints to link values between relations, primary key constraints to ensure uniqueness of attribute values within a relation, and check constraints to restrict attribute values to a fixed set of values. It provides examples of expressing different types of constraints through relational algebra operations and poses exercises for the reader to practice expressing additional constraints in this way.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views21 pages

5 - Chapter 2 - Relational Model of Data - P4

The document discusses using relational algebra to express constraints on relations in a database, such as foreign key constraints to link values between relations, primary key constraints to ensure uniqueness of attribute values within a relation, and check constraints to restrict attribute values to a fixed set of values. It provides examples of expressing different types of constraints through relational algebra operations and poses exercises for the reader to practice expressing additional constraints in this way.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Database Systems

Session 5
Chapter 2 - Relational Model of
Data - Part 4
Objectives

1 Understand Constraints on Relations

2 Know how to express constrains by relational algebra


Contents

1 Defining different constraints on relations

2 Relational algebra as a constraint language


2.5. Constraints on Relations
How to express constraints via Relation Algebra
Introduction

We now take up the third important aspect


of a data model: the ability to restrict the
data that may be stored in a database
In this section, we show how to express
both key constraints and “referential-
integrity” constraints
Introduction

A constraint is a mechanism that may


be used to limit the values entered into
a column.
CONSTRAINTS

NULL CHECK FOREIGN PRIMARY

Attribute-based check
Tube-based check
Assertions
2.5.1. Relation Algebra as a constraint Language

2 ways to express constraints with RA


Express Foreign Key Constraints
Express Key Constraints
Express Check Constraint
Two ways to express constraints with RA

2 statements following are the same:


 If R is an expression of relational algebra, then
is a constraint that says: “the value of RA must
be empty”, or equivalently “there are no tuples
in the result of R”
 If R and S are expressions of relational algebra,
then is constraint that says: “Every tuple in the
result of R must also be in the result of S”. Of
course the result of S may contain additional
tuples not produced by R

could also expressed as


Express Foreign Key Constraints

 Foreign key constraints (or referential integrity


constraint) asserts that a value appearing in one
context also appears in another related context.
 Example:
Classes(ClassID, ClassName, year)
Students(StudentID, Name, BirthDate, ClassID)
If we found a ClassID ‘SE1016’ in relation
Students, we would expect that ‘SE1016’ appears
as the ID of one class in relation ‘Classes’
Express Foreign Key Constraints

Suppose we have 2 relations R and S.


R contains an attribute A
S contains an attribute B

So, we say: “R references to S via A and


B” if: or
Example: Express Foreign Key Constraints

Sells( bar, beer, price ) Bars( bar, addr )


Joe’s Bud 2.50 Joe’s Maple St.
Joe’s Miller 2.75 Sue’s River Rd.
Sue’s Bud 2.50
Sue’s Coors 3.00


Express Primary Key Constraints

Suppose we have a schema R(A1,A2,A3)


A1 is the primary key of R means no two
tuples agree on the A1 component.
To express A1 as the primary key of R:

R1 and R2 are two new names of R.


Example: Express Primary Key Constraints

 δR1.OrderId = R2.OrderId AND R1.Customer≠R2.Customer(R1 x R2) = Φ


Express Check Constraints

 Give a schema:
EMPLOYEE(EmpId, Name, Sex)

 If we want to specify that the only legal values


for SEX attribute are ‘F’ and ‘M’, we can write:

δ sex <> ‘F’ and sex <> ‘M’ (EMPLOYEE) = Φ


3. Exercises 1

Give a schema:
PC (Model, Speed, RAM, HDD, Price)

Use Relational Algebra to express following


constraints:

A PC with a processor speed less than 3.00


must not sell for more than $800
3. Exercises 2

Give a schema:
LAPTOP (Model, speed, RAM, HDD,
Screen, Price)

Use Relational Algebra to express following


constraints:

A laptop with a screen size less than 15.4


inches must have at least a 120GB hard disk
or sell for less than $1000
3. Exercises 3

PC (Model, Speed, RAM, HDD, Price)


LAPTOP (Model, speed, RAM, HDD,
Screen, Price)

Use Relational Algebra to express following


constraints:

If a laptop has a larger RAM than a PC, then


the laptop must also have a higher price
than the PC
3. Exercises 4

PC (Maker, Model)


PRINTER (Maker, Model)

Use Relational Algebra to express following


constraints:

No manufacturer of PC’s may also make


printers
3. Exercises 5

PRODUCT (Maker, model)


PC (Model, speed, ram, hd, price)
PRINTER (Model, color, type, price)

Use Relational Algebra to express following


constraints:

No manufacturer of PC’s may also make


printers
3. Exercises 6

PC (Model, Speed, RAM, HDD, Price)

Use Relational Algebra to express following


constraints:
 Higher model, higher price
 With the same model, higher Speed and RAM
and HDD, higher price

You might also like