IT1090 - Lecture 06
IT1090 - Lecture 06
Lecture - 06
Learning Outcome
LO3: Model data requirements using data models
Relational Model
•Data model on which most DBMS implementations
are based. CODD 1970
Relation consists of
Relation schema
Relation instance (table)
Relation
RELATION:
Schema
Instance
Relation Schema
Describes the column heads (attributes) of the
relation
Relation Schema
Schema Name(relation name)
•Students
(sid:string,
name:string,
login:string, domain
Field name
age :integer,
gpa :real )
Relation Instance
Set of tuples or records or rows :
Each tuple has the same number of fields as the
relation schema
Example : Relation Instance
Degree of a relation
The degree of R is the number of attributes in R
(ID,Name,Address,Phone)=4
Example
Make a list of students in the class, keeping their ID,
name and phone number.
ID Name Phone
S01 Mike 111
S02 Elisa 222
Formalizing : Relations
Definition: A relation is a named table of data
Table is made up of rows (records or tuples), and columns
(attributes or fields)
Not all tables qualify as relations. Requirements:
1. Every relation has a unique name
2. Every attribute value is atomic (not multivalued, not
composite)
3. Every row is unique (can’t have two rows with exactly the same
values for all their fields)
4. Attributes (columns) in tables have unique names
5. The order of the columns is irrelevant
6. The order of the rows is irrelevant
Foreign Key
A constraint involving two relations
referencing relation
referenced relation.
Tuples in the referencing relation have attributes FK
(called foreign key attributes) that reference the
primary key attributes PK of the referenced relation
referencing Enrolled (cid ,grade, studid) FK
Integrity Constraints - IC
DBMS must prevent entry of incorrect information.
To prevent : Constraints / conditions are specified on
a relational schema = ICs
Database which satisfies all constraints specified on
a database schema is a legal instance.
DBMS enforces constraints - permits only legal
instances to be stored.
When the application is run the DBMS checks for the
violation and disallows the changes to the data that
violates the specified IC.
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
Integrity Constraints
Specified and enforced at different times.
Integrity Constraints
Many kinds of ICs:
Domain constraints
Key constraints
Entity integrity constraints
Referential integrity constraints
Domain Constraints
•Domain constraints: value in the Column must be
drawn from the domain associated with that
column
•Restricts the :
• Type
• Values that can appear in the field
Eg.
• Name Char (25)
• GPA (real >=0, =<4)
Key constraints
Is a statement that;
A certain minimal subset of the fields of a relation
is a unique identifier for a tuple.
Which Means
Two tuples in a legal instance cannot have
identical values in all the fields of a key.
Constraints…
•Entity Integrity Constraints: states that primary key
values cannot be null.
• This is because primary key values are used to identify
the individual tuples.
•Referential Integrity Constraints
• Sometimes information stored in one relation is linked
to information stored in another relation.
• If one is modified the other must be modified to keep
the data consistent.
• An IC involving both relations must be specified.
• IC involving 2 relations is a foreign key constraint.
• Foreign keys enforce referential integrity constraints.
Referential Integrity
The value in the foreign key column ( can be either:
a value of an existing primary key in the referenced relati
or a null.
FK
cid grade stuid PK Students
Carnatic101 C 53666 sid name login age gpa
B 53666 Jones jones@cs 18 3.4
Reggae203 53666
53688 Smith smith@eecs 18 3.2
Topology112 A 53650
53650 Smith smith@math 19 3.8
History105 B 53666
Enrolled
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
Insert Operation
The insert operation can violate the following
constraints:
Domain constraints (invalid value)
Key constraints (duplicate key values)
Entity integrity constraints (null primary key value)
Referential integrity constraint (non-existing
primary key value)
Examples
Students sid name login age gpa
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8
Examples
Students sid name login age gpa
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8
Examples
sid name login age gpa
Students
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8
Delete Operation
•Delete operation can violate referential integrity.
Enrolled Students
cid grade stuid sid name login age gpa
Carnatic101 C 53666 53666 Jones jones@cs 18 3.4
Reggae203 B 53666 53688 Smith smith@eecs 18 3.2
Topology112 A 53650 53650 Smith smith@math 19 3.8
•Two
History105
options:B 53666
• Reject the deletion
• Cascade the delete
Update Operation
Update operation can be considered as a deleting a
tuple and re-inserting the tuple with new values.
All constraints discussed in Insert & Delete need to
be considered.
Domain constraints (invalid value)
Key constraints (duplicate key values)
Entity integrity constraints (null primary key
value)
Referential integrity constraint (non-existing
primary key value)
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
ER to Relational Mapping…
In the Database Design process, we firstly derive a
conceptual model (ER Diagram)
This model needs to be mapped to the relational model
in order to be implemented using a relational DBMS
(RDBMS). Moving from Conceptual (ER) to lower level
Logical Model (Relational)
ER is independent of the details of the implementation
(relational, network or OO)_
This section discusses the rules that can be used for this
process
Entity(strong) Relation
For example,
name ARTIST
ID
Primary key
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
fullname
firstname
surname
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
Employee
Employee
name id name
id phone
Employee_contact
phone id
eid
Employee manage
Dept Dept
A has B AB
C-TYPE
C-MAX C-NAME
Course C-ID
TEL
1 DIRECTOR
FK
Is registered
N S-DEGREE
Student
S-DOB
S-ID S-NAME
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
FK
FK
A B
R pkA pkB pkC pkD
r
C D
C D pkC pkD
1
Employee EMPLOYEE
Policy id
id
N
name Dependents DEPENDENTS
id name age
age
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling
Lecture Outline
• ER-to-Relational Mapping Algorithm
End of Lecture 06
Questions ?
43
SLIIT - Faculty of Computing