ER/EER to Relational Data Model
1
Database Design
Database DesignDatabase Design
2
Steps in building a database for an application:
Real-world domain
Conceptualmodel
DBMS data model
Create Schema
(DDL)
Load data(DML)
Relational ModelRelational Model
3
It uses the concept of mathematical relation
In the relational model, all data is logically structured within relations (tables).
Relational Data StructureRelational Data Structure
4
4170010
4182000
4182034
4188134
4189860
4192134
StudentNo
Al-Saleh
Al-Ghanem
Al-Fahad
Saod
Rashed
Al-Fahad
LName
Amal
Nora
Laila
Amal
Rana
Rania
FName Initial
M.
A.
A.
F.
I.
M.
DOB
04-06-78
02-12-79
01-11-74
22-04-73
30-01-78
19-03-79
GPA
3.91
4.20
4.01
3.01
2.31
3.50
STUDENT
Attributes
Tuples
Cad
inal
ity
Rel
atio
n
Degree
Relation’s name
Relational Data StructureRelational Data Structure
5
Relation is a table with columns & rows. Holds information about entities.
Attribute is a named column of a relation.
Domain is the set of allowable values for one or more attributes. Every attribute in a relation is defined on a domain.
Tuple is a row of a relation.
Relational Data StructureRelational Data Structure
6
Degree of a relation is the number of attributes it contains.
Cardinality of a relation is the number of tuples it contains.
Relational database is a collection of normalized relations with distinct relation names.
Tuples
7
A tuple is a set of (<attribute,value>) pairs.These tuples are identical:
T1 = <(name, ahmad), (age, 16), ( address, Riyadh)>
T2= <(age, 16), ( address, Riyadh),(name, ahmad)>
The ordering of the tuples is not part of the relation.
Domain
8
Domain : set of atomic valuesDomain Constraints : values of each
attributes must be an atomic (not divisable into components ) value from the domain.
Associated to a data type.Dom(colors): {black,red, white},
DomainsDomains
9
4170010
4182000
4182034
StudentNo
Al-Saleh
Al-Ghanem
Al-Fahad
LName
Amal
Nora
Laila
FName Initial
M.
A.
A.
DOB
04-06-78
02-12-79
01-11-74
GPA
3.91
4.20
4.01
STUDENT
StudentNo
LName
FName
Initial
Student Name
Last Name
First Name
Initial
DefinitionAttribute
DOB
GPA
Domain Name
Date of Birth
Great Point Average
Digits: size 7
Character: size 15
Character: size 15
Character: size 3
Date: range 01-01-20, format dd-mm-yy
Real: size 3, decimal 2, range 0-5
Relational KeysRelational Keys
10
Candidate key (CK) It’s a minimal set of attributes necessary to identify a row. Its also called minimal super key.A relation schema may have more than one key, each of the keys is called a candidate key.
Primary Key (PK) is the CK that is selected to identify tuples uniquely within the relation
Foreign Key (FK) is an attribute, or set of attributes, within one relation that matches the CK of some relation. Used to represent relationship between tuples of two relations.
Relational KeysRelational Keys
11
4170010
4182000
4182034
4188134
4189860
StudentNo
Al-Saleh
Al-Ghanem
Al-Fahad
Saod
Rashed
LName
Amal
Nora
Laila
Amal
Rana
FName Initial
M.
A.
A.
F.
I.
DOB
04-06-78
02-12-79
01-11-74
22-04-73
30-01-78
GPA
3.91
4.20
4.01
3.01
2.31
STUDENT
D001
D002
D003
DeptNo
Computer Science
Business Administration
Science
Department Name
Build # 20
Dept
D001
D001
D002
D003
D001
Location
Build # 45
Build # 6
DEPARTMENTPrimary Key
Foreign Key
Basic Definitions
12
Relational Keys:PK : underlined with a straight line Composite keysFK: underlined with a dashed line
Properties of tables:Each table has a unique nameEach entry stores a single valueEach row is uniqueEach column has a unique nameThere is no sequence of rows.
DB RelationsDB Relations
13
Relation schema is a named relation defined by a set of attributes
If A1, A2, .., An are a set of attributes, then relation schema R is:
R = (A1, A2, .., An)Relational database schema is a set of
relation schemas, each with a distinct name If R1, R2, .., Rn are a set of relation schemas,
then relational schema R is:R= {R1, R2, .., Rn}
Relation SchemaRelation Schema
14
STUDENT (StudentNo, Lname, Fname, Initial, DOB, GPA, Dept)
4170010
4182000
4182034
4188134
4189860
StudentNo
Al-Saleh
Al-Ghanem
Al-Fahad
Saod
Rashed
LName
Amal
Nora
Laila
Amal
Rana
FName Initial
M.
A.
A.
F.
I.
DOB
04-06-78
02-12-79
01-11-74
22-04-73
30-01-78
GPA
3.91
4.20
4.01
3.01
2.31
STUDENTDept
D001
D001
D002
D003
D001
Relation SchemaRelation Schema
15
DEPARTMENT (DeptNo, Department Name, Location)
D001
D002
D003
DeptNo
Computer Science
Business Administration
Science
Department Name
Build # 20
Location
Build # 45
Build # 6
DEPARTMENT
Relational IntegrityRelational Integrity
16
Data integrity refers to the validity, consistency, and accuracy of the data in the database.
Integrity rules are constraints that apply to all instances of the DB.
Two integrity rules for the relational model:- Entity integrity- Referential integrity
Entity Integrity
17
Entity Integrity: (in a single relation)Ensures that there are no duplicate records within the table. In a base relation, no attribute of a PK can be null.T[PK] ≠ Null for any tuple in R.
Referential Integrity
18
Referential Integrity: ( two relations) If a FK exists in a relation, either the FK value
must match a CK value of some tuple in its home relation or the FK value must be wholly null.
The referencing relation R1 has the FK that reference a PK in the referenced relation R2.
=> t1[FK] =t2[PK], t1 in R1 and t2 in R2
Referential Integrity Constraints
19
SSN | Fname | LName | Bdate | DNO
Dnumber | Dname | MGRSSN | MgrStartDate
EMPLOYEE
DEPARTMENT
Properties of Relations
20
1. The relation has a distinct name from all other relation names in the relational DB (oracle enforces this rule)
2. Each cell of the relation (attribute) contains exactly single value
3. Each attribute has a distinct name (oracle enforces this rule)
4. The values of an attribute are all of the same domain5. Each tuple is distinct. There are no duplicate tuples6. The order of attributes has no significance7. the order of tuples has no significance; theoretically.
Integrity constraints
21
Domain constraintsEntity integrity constraintsReferential integrity constraintsKey ConstraintOperational constraints
Operations of the Relational Model
22
Updates: changes the database’s state.InsertDeleteUpdate/modify
Retrievals: does not change the current state of the database.
The Insert Operation
23
This operation can violate all constraints of the relation. How?
Domain constraint: attribute value does not apear in the corresponding domain.
Key constraint: if the key value already exist in another tuple.
Entity integrity: if the PK of the new tuple is NULLReferential integrity: if any value of the FK refers to
a tuple that does not exist in the other relation.
The Delete Operation
24
This operation can violate only the referential integrity constraint. How?
E.g: you can not delete a course that many students study in already.
The Update Operation
25
The new value must be of the correct data type and domain.
26
Step -1- Strong Entity Type
27
Represent each entity type with a relationEntity type attributes become the relation
attributese.g.
STUDENT (StudentNo, Lname, Fname, Initial, DOB, GPA, Dept)DEPARTMENT (DeptNo, Department Name, Location)
Step -2- Weak Entity Type
28
A weak entity type relation must include its key and its strong entity type PK as a FK. The combination of the two keys form the PK of the weak entity.
EMPLOYEE
EmpNo
LName FName
DOBDepNo FName
DEPENDENThas
EMPLOYEE (EmpNo, Lname, Fname, DOB)
DEPENDENT (DepNo, EmpNo, FName)FK
Step -3- Binary 1:1 Relationship Type
29
Identify an entity type (S) (preferably total participator) Include the PK of the other entity (T) as a FK in S Add attributes that describes the relationship
BRANCHmanageEMPLOYEE
EMPLOYEE(EmpNo, Lname, Fname, DOB)
BRANCH(BrnNo, Name, EmpNo, StartDate, EndDate)
1 1
(0,1) (1,1)
StartD EndDEmpNo
LName FName
DOBName
BrnNo
FK
………..
Binary 1:1 Important Notes
30
For a 1:1 relationship, the PK of the partial is a FK in the total.
What happens if have an equal multiplicity?Then it depends on the path analysis.
Step -4- Binary 1:N Relationship Type
31
Identify a participating entity type (S) on the m-side Include the PK of the other entity type (T) as a FK in S Add attributes that describes the relationship
BRANCHallocateEMPLOYEE
EMPLOYEE(EmpNo, Lname, Fname, DOB, BrnNo)
BRANCH(BrnNo, Name)
M 1
EmpNo
LName FName
DOBName
BrnNo
FK………..
Binary 1:N Important Notes
32
For a 1:N relationship, the schema of the N side will have the PK of the 1 side as a foreign key.
What if the 1 side has a composite key?All keys will be copied to the many side as
foreign keys.
Step -5- Binary M:N Relationship Type
33
Create a relation R to represent the relationship Include the PK of participating entity types (T & S) as FK in R. The combination of the two FK will form the PK of R Add attributes that describes the relationship
EMPLOYEE(EmpNo, Lname, Fname, DOB)
PROJECT(ProjNo, Name)
Work-on(EmpNo,ProjNo, hours)
M N
hoursEmpNo
LName FName
DOBName
ProjNo
FKFK
EMPLOYEE work-on PROJECT
Binary M:N Important Notes
34
For a M:N relationship , a new schema is created that contains the PK of N and PK of M plus any other attributes.
Some times the situation of insufficient PK occurs … !! How?
E.g. a patient gets a treatment several times a resulting table will contain the PK of patient and the PK of the treatment.. But the patient may have the same treatment for several times ….. Therefore a date field must be included as a PK of the relation.
Step -6- n-ary Relationship Type
35
Create a relation R to represent the relationship Include the PK of the participating entities as FK in R. The combination of all FK form the PK of R. Add attributes that describes the relationship
SUPPLIERcontractBUSINESS
BUSINESS(BizNo) LAWYER(LawNo) SUPPLIER(SupNo)
contract(BizNo, SupNo, LawNo, StartDate, EndDate)
StartD EndDBizNo
LawNo
SupNo
FK
Lawyer
FK FK
Ternary Relationship Important Notes
36
If the Ternary relationship is:N:N:N then a new intersection table is
created that includes the 3 entities keys as a PK of that table.
1:N:N then the PK of the 1 is a FK in the intersection table.
Recursive Relationship 1:1
37
The schema for this relationship is going to have a new field called MarriedToPersonID, which shares the same domain of the PersonID.
PERSON Married to
PersonID1
1
PERSON(PersonID, PersonName,MarriedToPersonID)
FK
Recursive Relationship 1:N
38
The schema for this relationship is going to have a new field, Its exactly the same is a 1:1 uniary relationship
EMPLOYEE supervises
EmpID1
N
EMPLOYEE(EmpD, EmpName,DOB, ManagerEmpID)
FK
Step-7- Multi-valued Attribute
39
Suppose A is a relation that contains the multi-valued attribute Create a relation R to represent the attribute Include the PK of A as FK in R The PK of R is the combination of the PK of A (FK) & the multi-valued attribute
EMPLOYEE
EmpNo
DOB
Tel_no
EMPLOYEE(EmpNo, DOB)
TELEPHONE(EmpNo, tel_no)FK
Composite Attribute
40
Include its simple components in the relation
EMPLOYEE
emp_no
name DOB
LNameinitial FName
EMPLOYEE(EmpNo, Fname, initial, Lname, DOB)
41
EER To Relational Data Model
EER Relational Model
42
In EER Model, there are sub-classes and super-classes and to convert them into relational schema we have to use one of the four options which will discuss in next slides:
We have to remember these concepts which related to EER.
o Total : all entities in super class must be a member of subclass.
o Partial :an entity in super class may not belong to any subclass.
o Disjoint : an entity be a member only in one subclass.
o Over-lapping: an entity may be a member of more than one subclass
EER Relational ModelMandatory(Total)/NonDisjoint(Overlapping)
43
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C Create a relation L to represent C with PK
& attributes Include the unshared attributes for each
subclass Si, 1<= i <= m Add discriminator to distinguish the type of
each tuple
EER Relational ModelTotal/Non-Disjoint (Overlapping)
Single Relation
R
PK(K)
Boolean type attribute
o
EER Relational ModelTotal/Non-Disjoint (Overlapping)
45
EMPLOYEE( EmpNo, Fname, Lname, DOB, Salary,TypingSpeed,TGrade,EngType, Secretary Flag, Technician Flag, Engineer Flag)
EER Relational ModelTotal/Disjoint
46
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C Create a relation Li, 1≤ i ≤m, to represent
each combination of super/subclass
EER Relational ModelTotal/Disjoint
Relation R2 for subclass
TRUCK
Relation R1 for subclass
CAR
PK(K)
Tonnage
d
EER Relational ModelTotal/Disjoint
d
Car( VehicleId ,LicensePlateNo, Price, MaxSpeed ,NoOfPassengers)
Truck ( VehicleId ,LicensePlateNo, Price,NoOfAxles,Tonnago)
EEROptional/NonDisjoint(Overlapping)
49
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C Create a relation L to represent C with PK
& attributes Create a relation Li to represent each
subclass Si, 1≤ i ≤m
Relation R1
EEROptional/NonDisjoint(Overlapping)
Relation R
Relation R2
Relation R3
PK(K)O
EEROptional/NonDisjoint(Overlapping)
O
Employee( SSN ,Fname,Minit,Lname,BirthDate,Address,JobType)Secretary( SSN ,TypingSpeed)Technician( SSN , Tgrade)Engineer( SSN ,EngType)
EEROptional/Disjoint
52
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C Create a relation L1 to represent C with PK
& attributes Create a relation L2 to represent all
subclasses Si, 1≤ i ≤ m Add discriminator to distinguish the type of
each tuple
Relation R1
EEROptional/Disjoint
Relation R
Relation R2
Relation R3
PK(K)d
EEROptional/Disjoint
54
EMPLOYEE ( EmpNo, Fname, Lname, DOB, Salary)SECRETARY( EmpNo, TypingSpeed)TECHNICIAN( EmpNo, Tgrade)ENGINEER( EmoNo, EngType)
Views
55
Base relation is a named relation corresponding to an
entity in the conceptual schema, whose tuples are physically stored in the DB
View is a derived relation. Virtual, may not exist, but dynamically derived from one or more base relations.
The only information about a view that is stored in the database is its structure.
The external model can consist of both conceptual level relations (base relations) and derived views.
View
56
Purpose of Views
57
Provides security mechanism by hiding parts of the DB from certain users
Customize data to user’s needs, so that the same data can be seen by different users in different ways
Simplify complex operations. It allow you to work with data from different tables simultaneously.
Supports logical data independence
Updating Views
58
All updates to a base relation should be immediately reflected in all views that reference that base relation.
All updates to a view should be reflected in the underlying base relation, under the following rules:- Updates are allowed through a view defined using a
simple query involving a single base relation & containing either the PK or a CK of the base relation
- Update are NOT allowed through views involving multiple base relations
- Updates are not allowed through views involving aggregation or grouping operations
Relational Integrity
59
Data integrity refers to the validity, consistency, and accuracy of the data in the database.
Integrity rules are constraints that apply to all instances of the DB.
Two integrity rules for the relational model:- Entity integrity- Referential integrity
Relational Integrity
60
Entity Integrity: Ensures that there are no duplicate records within the table. In a base relation, no attribute of a PK can be null
Referential Integrity: If a FK exists in a relation, either the FK value must match a CK value of some tuple in its home relation or the FK value must be wholly null.
Enterprise constraints: rules specified by the users or DBA of the DB based on the ways an organization perceives and uses its data
(e.g.number of staff working in a branch is at most 20)
References
61
Elmasri,Ramez &Navathe,Shamkant B., Fundamentals Of Database System, fifth edition, Pearson ,2007
Fatimah Alakeel’s Lectures http://webdocs.cs.ualberta.ca/~drafiei/291/notes/4.
ER2Rel.pdfhttp://www.cs.sfu.ca/CC/354/zaiane/material/notes/
Chapter2/node10.html#SECTION00190000000000000000
http://web.uettaxila.edu.pk/CMS/seADMSbsSp09/notes%5CADBMS-Lecture-6%20ER%20to%20Relational%20Mapping.pdf
www.cs.gsu.edu/~cscbecx/.../Chapter%207.ppthttp://www.mscs.mu.edu/~praveen/Teaching/
Fa06/Db/Lectures/ch7.pdf
www.cs.uwm.edu/classes/cs557/powerpoint/chapter9.ppt