Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

E-R to Tables

Relational Model
• Attributes are characteristics of an entity,
which represent the columns in a
relation(table).
• e.g., st_id, name, birth_date, gender for
student entity.
• Tuples are rows in a relation(table).
• E.g., 2007P7PS001 Arun 20/12/1989 m
2007P7PS020 Jyoti 01/01/1990 f

4/13/2021 UCS310-Database systems 2


Schemas
• Relation schema = relation name and
attribute list and types of attributes.
– Example: student(st_id: string, name: string,
birth_date: date, gender: character)
• Database = collection of relations.
• Database schema = set of all relation
schemas in the database.

4/13/2021 UCS310-Database systems 3


Why Relations?
• Very simple model.
• Often matches how we think about data.
• Abstract model that underlies SQL, the
most important database language today.

4/13/2021 UCS310-Database systems 4


From E/R Diagrams to Relations
• Relational database: It is a set of relations
• Entity set -> relation.
– Attributes -> attributes.
• Relation – a two dimensional table (different
from ‘Relationships’)
• Relationships -> relations whose attributes are
only:
– The keys of the connected entity sets.
– Attributes of the relationship itself.

4/13/2021 UCS310-Database systems 5


From E/R Diagrams to Relations
• A Relation consists of a relation schema and
a relation instance.
- Schema of a relation: name of a relation and
its attributes.
- Instances: a set of tuple(rows), in which each
tuple has the same number of attributes as the
relation schema(table), with rows and columns.
#Rows = cardinality,
#Columns(Fields) = degree
• e.g, Students(stid: string, name: string,
birthdate: date, gender: character)
• Relation is set of rows or tuples with all rows
distinct.
4/13/2021 UCS310-Database systems 6
Entity Set -> Relation
title ltp
Course_no units

course sections

• course(course_no, title, units, ltp,


no_of_sections)

4/13/2021 UCS310-Database systems 7


Instance of a Relation
• Example: Instance of Students Relation
st_id st_name birthdate gender
2007A7PS040 Amit 30/06/1989 m
2007A7PS150 Abhiral 28/03/1990 m
2007A7PS200 Vaishali 20/09/1989 f

- Cardinality = 3, degree = 4, all rows distinct
- Do all columns in a relation instance have
to be distinct?( yes/no)
4/13/2021 UCS310-Database systems 8
ER to Relational Model
• General principles
– Entity set => a relation with same attributes
– Relationship => a relation whose attributes
are keys from participating entity sets +
descriptive attributes if any
• Special situations
– Weak entity sets
– Entity Hierarchies (ISA relationships)
– Merging relations

4/13/2021 UCS310-Database systems 9


Example
Emp-name Dept-id Dept-name
emp-
id

Employee Works-in Department

Since-when

heads

Since-when

4/13/2021 UCS310-Database systems 10


Relational model for employee DB

• Database schema for employee DB


Employee(emp_id, name, birth_date,
gender, addr_l1, addr_l2, addr_l3)
Department(dept_id, name, location, tel_no)
Emp_works(emp_id, dept_id, since_when)
Dept_head(dept_id, emp_id, since_when)

4/13/2021 UCS310-Database systems 11


Merging Many-One relations
• A relation of Many-One relationship can be
merged with the relation of the entity set
on Many side

4/13/2021 UCS310-Database systems 12


Merging Many-One relations
▪ Is it possible to merge Many-One relations in this
case? (yes/no) Room_no

student allotted Hostel

Hostel_no
No_of_rooms
st-id name Hostel_name

Student(st_id, name, birth_date, gender)


Hostel(hostel_no,hostel_name, no_of_rooms)
Hostel_allocation(st_id, hostel_no, room_no)

4/13/2021 UCS310-Database systems 13


Merging Many-One relations
• Is it possible to merge Many-One relations in this
case? (yes/no) no_of_students

student allotted Practice_school2

ps2_no
ps2_location
st-id name
ps2_name

student(st_id, name, birth_date, gender, ps2_no)


practice_school2(ps2_no, ps2_name, ps2_location,
no_of_students)

4/13/2021 UCS310-Database systems 14


Merging of many-many relationship
• Merging of many-many relationship is not ‘good’
course-no title
st-id name

students register courses

birth-date units ltp

– Consider merging ‘registers’ with ‘students’


– This introduces data ‘redundancy’ as student
information is repeated for every course the student
registers.

4/13/2021 UCS310-Database systems 15


Merging of many-many relationship
• Merging of many-many relationship is not
‘good’

– Consider merging ‘registers’ with ‘students’


– This introduces data ‘redundancy’ as student
information is repeated for every course the
student registers.

4/13/2021 UCS310-Database systems 16


Weak entity
birth-date

employee has dependent

emp-id emp-name dep-name


relationship
gender

▪ There must be ‘referential integrity’ constraint


from weak entity set to the supporting entity
sets.
4/13/2021 UCS310-Database systems 17
Relational model for dependents DB
• Database schema for dependents DB
Employee(emp_id, name, birth_date, gender,
addr_l1, addr_l2, addr_l3)
Dependents(emp_id, dep_name, birth_date,
gender, relation_with_emp)

4/13/2021 UCS310-Database systems 18


Example
name thesis
st-id

students

ISA

UG PG

ps-1 courses courses

4/13/2021 UCS310-Database systems 19


Entity hierarchy to relations: method 1
• Only one relation having all attributes
belonging to any entity set of the hierarchy
– Student (st_id, name, gender, birth_date,
thesis_title, no_of_courses,
ps_1)
• An entity is represented by a single tuple
• Simple method and only one relation
• Queries could be faster
• But tuples could be too long with many
Nulls
• Could involve certain kind of redundancies
4/13/2021 UCS310-Database systems 20
Entity hierarchy to relations: method 2
• For each entity set in the hierarchy, create a
relation that includes the key from the root entity
set + its attributes
– student(st_id, name, gender, birth_date,
thesis_title, no_of_courses, ps_1)
– pg_student(st_id, no_of_courses)
– ug_student(st_id, no_of_course, ps_1)
• No relation required for ISA relationship
• An entity may have tuples in several relations
• Queries may require ‘Joining’ of relations

4/13/2021 UCS310-Database systems 21


END

4/13/2021 UCS310-Database systems 22

You might also like