Sei sulla pagina 1di 12

CSC271 Database Systems

Relational Model
Sets and Cross Product
Relational model of the data is based on the mathematical foundation
of sets and was proposed by E. F. Codd.
Consider following sets
name = {Junaid, Sarmad, Kutab, }

! Set of all customer names

city = {Multan, Lahore, Peshawar, }


program = {MSCS, BSCS, BSSE, }

! Set of all degree programs

Compute name x city x program

Now find a subset of the cross product and call it r

2
Relation as a Set

Say r = { (Junaid, Multan, MSCS),


(Sarmad, Lahore, BSCS),
(Kutab, Lahore, BSCS),
(Junaid, Peshawar, BSSE) }
Is r a set?

IF r (name x city x program)

THEN r is a relation on (name, city, program)

3
Relation Example - Students
Columns as
attributes

name city program


Junaid Multan MSCS
Rows as
Sarmad Lahore BSCS
tuples
Kutab Lahore BSCS
Junaid Peshawar BSSE

Order of tuples is irrelevant;


tuples may be stored in an arbitrary order
WHY?
4
Basic Structure

Given sets of domains D1, D2, . Dn

E.g. Person names and City names


A relation r is a subset of
D1 x D2 x x Dn
Thus, a relation is a set of tuples (a1, a2, , an)
where each ai Di
Degree (or arity) of the relation is number of
attributes n involved in the relations.

5
Attributes and Domains

Every attribute (column) of a relation has a name


The set of allowed values for each attribute is
called the domain of the attribute
The special value null is a member of every
domain
Attribute values are (normally) required to be
atomic; that is, indivisible
E.g. the value of an attribute can be a grade,
but cannot be a set of grades.
Domain is said to be atomic if all its members are
atomic

6
Relation Schema

A1, A2, , An are attributes

S = (A1, A2, , An ) is a relation schema


Example: StudentSchema = (name, city, program)

r (S) denotes a relation r on the relation schema S


Example: student (StudentSchema)

7
Database

A database consists of multiple relations

Information about an enterprise or a business


function is broken up into parts, with each
relation storing one part of the information
Student : basic information about students
Course: basic information about courses
Enrollment: information about which student
registered in which course

8
Example Database

Student Enrollment
rn name city program rn code when
01 Junaid Multan MSCS 02 101 2014-08-30
02 Sarmad Lahore BSCS 03 101 2014-09-01
03 Kutab Lahore BSCS 04 101 2014-09-01
04 Junaid Peshawar BSSE 04 123 2015-01-30
02 271 2015-09-01
Course 03 272 2015-09-01
code title credits lab 04 271 2015-09-02
101 ICT 3 1 03 302 2016-01-30
123 RE 3 0 04 302 2016-01-30
271 DB 4 1
302 HCI 3 0
9
The Banking Schema
branch = (branch_name, branch_city, assets)
customer = (customer_id, customer_name, customer_street, customer_city)
loan = (loan_number, amount)
account = (account_number, balance)
employee = (employee_id. employee_name, telephone_number, start_date)
dependent_name = (employee_id, dname)
account_branch = (account_number, branch_name)
loan_branch = (loan_number, branch_name)
borrower = (customer_id, loan_number)
depositor = (customer_id, account_number)
cust_banker = (customer_id, employee_id, type)
works_for = (worker_employee_id, manager_employee_id)
payment = (loan_number, payment_number, payment_date, payment_amount)
savings_account = (account_number, interest_rate)
checking_account = (account_number, overdraft_amount)

10
CMS Schema
Program (pid, level, subject, title)
Student (rn, name, city, pid)
Course (ccode, title, credits, lab)
Faculty (fid, name, phone, department, appointment_date)
Staff (sid, name, phone, appointment_date)
Class (cid, ccode, fid, year, semester)
Enrollment (rn, cid, when, status)
Activity (aid, cid, title, description, category, total_marks, start_date, deadline)
Evaluation (rn, aid, marks)
Evaluation (rn, code, marks)
Grade (rn, oid, marks, letter_grade)

11
Case Study

AgroSafe: Fertilizer and Pesticide Distribution


Agency in Vehari
Customers: id, name, address, city, phone
Vendors: id, name, address, city, phone
Products: id, name, category, price
Purchases: date, material, quantity,
purchase_price, vendor
Sales: date, product, quantity, sale_price,
customer, sales_agent

12

Potrebbero piacerti anche