You are on page 1of 14

Tutorial 4

Relational Data Model

1
The table is called A column header is
relation. called an attribute.

A row is
called a
tuple.

The data type describing the types of values in each columns is called a
domain. 2
Relational schema
• A domain D is a set of atomic values which means it is
indivisible for this relation.
• For the relational schema R(A1,A2,A3, …., An):
• R is the name of the relation
• A1,A2,A3, …., An are the attributes
• Each attribute Ai is the name of a role played by some
domain D (dom(Ai)) in the relation schema R.
• The degree of a relation is the number of attributes n of its
relation schema.
• Ex: STUDENT(Name,
SSN,HomePhone,Adrress,OfficePhone,Age,GPA)

3
Relational Constraints

Domain Key Null


Constraints Constraints Constraints

• It is called uniqueness
Each attribute constraint that no two Not Null
A must be an distinct tuples in r can constraint
atomic value have the same values specifies that
from the for SK. an attribute
domain • A superkey can have must have a
dom(A). redundant attributes. valid value.
4
Integrity Constraints

Entity integrity
Referential integrity constraints
constraint

• It is specified between two relations.


No • It is used to maintain consistency among
primary tuples of the two relations.
key • It states that a tuple in one relation that
value refers to another relation must refer to
can be an existing tuple in that relation.
null. • The foreign key is a concept used to
define the referential integrity constraint.
5
Relational DB Schemas

 A relational DB schema is a set of schemas S =


{R1,R2,R3, …., Rm} and a set of integrity
constraints IC.
 Data Definition Language (DDL) is used to define
the schema.
 Have Domain, key, entity and referential integrity
constraints

6
7
8
Relational model operations

Retrieval operations. Update operations

Insert Delete Update

 All integrity constraints specified on the relational DB schema


should not be violated.
 Three options are available if a deletion operation causes a
violation:
 Reject the operation.
 Cascade the deletion.
 Modify the referencing attribute values (set to null or
reference another valid tuple). 9
Exercise 3.14.
Consider the following six relations for an order-processing
database application in a company:
CUSTOMER(Cust#, Cname, City)
ORDER(Order#, Odate, Cust#, Ord_amt)
ORDER_ITEM(Order#, Item#, Qty)
ITEM(Item#, Unit_price)
SHIPMENT(Order#, Warehouse#, Ship_date)
WAREHOUSE(Warehouse#, City)
Here, Ord_amt refers to total dollar amount of an order; Odate is
the date the order was placed; and Ship_date is the date an order
(or part of an order) is shipped from the warehouse. Assume that an
order can be shipped from several warehouses.
Specify the primary and foreign keys for this schema, stating any
assumptions you make. What other constraints can you think of for
this database? 10
Answer 3.14.
• The schema of this question has the following six
primary keys:
• The attribute Cust# in relation CUSTOMER is PK.
• The attribute Order# in relation ORDER is PK.
• The attributes (Order#, Item#) in relation ORDER_ITEM are PK.
• The attribute Item# in relation ITEM is PK.
• The attributes (Order#, Warehouse#) in relation SHIPMENT are PK.
• The attribute Warehouse# in relation WAREHOUSE is PK.
• The schema of this question has the following five
foreign keys:
• The attribute Cust# of relation ORDER that references relation CUSTOMER.
• The attribute Order# in relation ORDER_ITEM that reference relation ORDER.
• The attribute Item# in relation ORDER_ITEM that reference relation ITEM.
• The attribute Order# in relation SHIPMENT that reference relation ORDER.
• The attribute Warehouse# of relation SHIPMENT that references relation
WAREHOUSE. 11
Exercise 3.17.
Consider the following relations for a database that keeps
track of automobile sales in a car dealership (OPTION refers
to some optional equipment installed on an automobile):
CAR(Serial_no, Model, Manufacturer, Price)
OPTION(Serial_no, Option_name, Price)
SALE(Salesperson_id, Serial_no, Date, Sale_price)
SALESPERSON(Salesperson_id, Name, Phone)
First, specify the primary and foreign keys for this schema,
stating any assumptions you make. Next, populate the
relations with a few sample tuples, and then give an
example of an insertion in the SALE and SALESPERSON
relations that violates the referential integrity constraints
and of another insertion that does not. 12
Answer 3.17.

• The schema of this question has the following four


primary keys:
• The attribute Serial_no in relation CAR is PK.
• The attributes (Serial_no, Option_name) in relation OPTION are PK.
• The attributes (Serial_no) in relation SALE are PK.
• The attribute Salesperson_id in relation SALESPERSON is PK.
• The schema of this question has the following three
foreign keys:
• The attribute Serial_no of relation OPTION that references relation CAR.
• The attribute Salesperson_id in relation SALE that reference relation
SALESPERSON.
• The attribute Serial_no in relation SALE that reference relation CAR.

13
Answer 3.17. (con’t)
Sample tuples
• insert into CAR(Serial_no, Model, Manufacturer, Price) values
(‘sd435jb’,’kia’,’japan’,125000);
insert into OPTION(Serial_no, Option_name, Price) values
(‘sd435jb’,’automatic’,10000);
insert into SALESPERSON(Salesperson_id, Name, Phone) values
(1,’jason’,’01234565649’);
insert into SALE(Salesperson_id, Serial_no, Date, Sale_price) values
(1,’sd435jb’, '2007-05-06',130000);
• Violation statements
insert into SALE(Salesperson_id, Serial_no, Date, Sale_price) values
(11,’sd435jb’, '2007-05-06',130000);

14

You might also like