Unit 4 Data Models

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Database Systems

Logical Data Models


Record Based Models

Note:
Some slides and/or pictures are adapted from Lecture slides /
Books of
• Peter Rob
• Jeffery. A. Hoffer
• Thomas M. Connolly
• Ramez Elmasri
• Silberschtaz

Database Systems Slide 1


Logical Data Models

1) Hierarchical Data Model


2) Network Data Model
3) Relational Data Model

Database Systems Slide 2


Data Models
1. Hierarchical Data Model (HDM)
• The records are arranged in a top down structure that resembles
a tree.
Terminologies:
a) Node (also called segment) corresponds to a record
b) Root Node: The top most node
c) Parent & Child Nodes
d) Leaf Node is a node having no children
e) Siblings: The children of same parent
f) Family of Nodes: All children of same node
g) Hierarchical Path is a path from root node to any node
h) Height of a tree refers to no. of levels
• Examples….
Database Systems Slide 3
Hierarchical Data Model (HDM)
Background
• It was developed in 1960s (Moon landing project).
• The earliest DBMS were based on HDM.
• One of the most popular HDM was IBM’s Information
Management System (IMS).
• For this reason, the term HDM and IMS are sometime used
interchangeably.

Database Systems Slide 4


Hierarchical Data Model (HDM)
Advantages
• Conceptual Simplicity: The relationship between the various
layers of the model is logically simple.
• Data Independence
• Database Integrity
• Efficiency: The HDM is very efficient when a database
contains a large volume of data in 1:M relationship that are
fixed over time.

Database Systems Slide 5


Hierarchical Data Model (HDM)
Disadvantages
• Duplication: The student information is duplicated for every
course .
• Root Node dominates the other nodes and other nodes can’t
exist independently.
• Not capable to answer all sorts of queries with the same
efficiency.
• Supports one-to-one and one-to-many association but doesn’t
support many-to-many association.
• Complex
• The relationships are predefined (pointer based) and new
relationships cant be accommodated unless we redraw the
model.

Database Systems Slide 6


Data Models
2. Network Data Model (NDM)
• It is an extension to HDM.
• It was introduced to overcome the limited scope of the HDM.
• In NDM, each node can be associated with an arbitrary number
of nodes and there is no concept of root node.
• Example….
• In NDM, a relationship is called a Set.

Database Systems Slide 7


Network Data Model (NDM)
Background
• The Conference on Data System Language (CODASYL) formed
Database Task Group (DBTG) in 1960 for the standardization of
DBMS.
• It released an initial report on NDM in 1971
• The Integrated DBMS is the most popular example of NDM.
• It is also known as CODASYL System or DBTG System.
• The ANSI Standard Planning and Requirements Committee
(SPARC) augmented the database standards in 1975.

Database Systems Slide 8


Network Data Model (NDM)
Advantages
• Conceptual Simplicity: The relationship between the various
layers of the model is logically simple.
• Data Independence
• Database Integrity
• Handles more relationship types
• Data Access Flexibility: An application can access an owner
record and all member records in a set.

Database Systems Slide 9


Network Data Model (NDM)
Disadvantages
• Complex
• Inflexible
• The relationships are predefined (pointer based) and new
relationships cant be accommodated unless we redraw the
model.
• Lack of structural independence: Given its navigational data
access, it remains difficult to answer all sorts of queries with
the same efficiency.

Database Systems Slide 10


Data Models
3. Relational Data Model (RDM)
• A Relation is a mathematical concept based on the ideas of sets.
• It was first proposed by Dr. E.F. Codd of IBM Research in 1970
in a research paper "A Relational Model for Large Shared Data
Banks," Communications of the ACM, June 1970.
• The RDM caused a major revolution in the field of database.
• The RDM is widely used database model and now the terms
DBMS and RDMBS (Relational DBMS) are used
interchangeably.
• It represents data in form of relations.
• A Relation (sometimes called a Table) is a 2-dimsnesional table
of data consisting of rows & columns.
• Example….

Database Systems Slide 11


Data Models
3. Relational Data Model (RDM)

• A Row in a relation is also called Tuple (values are written inside


angular brackets <…>)
• Number of tuples in a relation is called Cardinality of the relation.
• Number of columns/attributes in a relation is called Degree or arity of
the relation.
• Each attribute has a Domain (set of valid values and format).
Database Systems Slide 12
Data Models
3. Relational Data Model (RDM)
• Formally, given sets D1, D2, …. Dn a relation R (relation state) is a subset of
D1 x D2 x … x Dn. Thus, a relation is a set of n-tuples
<a1, a2, …, an> where each ai ε Di
• A relation consists of two parts:
a) Heading consisting of fixed set of attributes or columns.
b) Body consisting of time varying set of tuples.
• The schema or description of a relation is written as:
Relation_Name(Att-1,Att-2,..Att-n)
• A RDM consists of 3 components:
a) Data Structure: Data are organized in the form of tables or relations.
b) Data Manipulation: Powerful operations using SQL.
c) Data Integrity: To ensure the correctness of data.

Database Systems Slide 13


Relational Data Model (RDM)
Properties of a Relation
• Each relation in a database has a unique name.
• Each column has a distinct name.
• Each row is unique.
• Entries in columns are from the same domain.
• Each row/column intersection must have single or atomic value.
• The order of column (left to right) is insignificant.
• The order of rows (top to bottom) is insignificant.

Database Systems Slide 14


Entity set Column (attribute) value

Row
entity

15
Definition Summary

Informal Terms Formal Terms


Table Relation
Column or Field Attribute
All possible Column Values Domain
Row or Record Tuple
Table Definition Schema of a Relation
Populated Table State of the Relation

Database Systems Slide 16


Relational Keys
1. Super Key
2. Candidate Keys
3. Composite Key
4. Primary Key (PK)
5. Alternate Keys
6. Surrogate Key
7. Secondary Key, Sort Key
8. Foreign Key (FK)

Database Systems Slide 17


Relational Keys
Determination (A -> B)
• A determines B (A is called determinant)
• Using the value of A, value of B can be uniquely
determined OR attribute B is functionally dependent on
attribute A.
• STU_NUM → STU_NAME, STU_DOB, STU_CLASS
• Each value in column A determines one and only one
value in column B.
• All of the rows in a table that agree on value for attribute A
also agree in value for attribute B.

Database Systems Slide 18


Relational Keys
1. Super Key
• An attribute or a set of attributes that uniquely identifies a
tuple within a relation.
• A set of all the attributes of a relation is one of many
possible super keys of a relation.
• A super key may contain extra attributes that are not
necessary for achieving uniqueness.
2. Candidate Key is an attribute or minimal combination of
attributes that uniquely identifies each instance of an
entity type.
• A super key whose no proper subset is a super key.
For Ex: RollNo and RegNo of a student are candidate
keys for students entity types.

Database Systems Slide 19


Relational Keys
3. Composite Key:
• Sometimes a single attribute is not sufficient to uniquely
identify a tuple. In such situations, two or more attributes
are combined together to form a key and such a key is
called Composite Key.
• A Composite Key must satisfy the conditions of
uniqueness & Minimality i.e. Elimination of any part of key
should will destroy uniqueness.

Database Systems Slide 20


Relational Keys
4. Primary Key
• It is an attribute or combination of attributes that is
selected as an identifier for an entity type OR the
candidate key that is selected for unique identification of
tuples of a relation.
• Primary key values are not allowed to accept NULL value
• Primary key can uniquely determine the values of other
attributes of the relation.

Database Systems Slide 21


Relational Keys
5. Alternate Key or Keys are the keys left behind after the
selection of the primary key among the possible candidate
keys of an entity type.
For Ex: If RollNo and RegNo of a student are candidate
keys and RegNo is chosen as the Primary Key then
RollNo will be called Alternate Key.

6. Surrogate Key
• Sometimes row-ids or sequential numbers are assigned to
uniquely identify each tuple of the relation. Such key is
called artificial key or surrogate key.
7. Secondary Key, Sort Key is a key used in data retrieval
For Ex: List all students of BS program.

Database Systems Slide 22


Relational Keys
8. Foreign Key (FK)
• The RDM uses a set of relations liked together by a key
called foreign key.
• The FK is an attribute of one relation called referencing
relation whose values are required to match the primary
key of another relation called referenced or target relation.
• Foreign keys are the glue holding the database together.
Without foreign keys, the data is like leaves covering the
ground in fall – you know that each leave fell from one of
the trees, but it is impossible to say from which one.

Database Systems Slide 23


Relational Keys
8. Foreign Key (FK)
Consider the following two relations:
R1: EMP(EmpNo, EName,…,DeptNo)
R2: DEPT(DeptNo, DName)
The values of DeptNo in the EMP relation should either be one of
the values stored in DEPT relation or it may optionally be NULL.
Table Name: EMP Table Name: DEPT

EMPNO ENAME JOB DEPTNO DEPTNO DNAME LOC


7839 KING PRESIDENT 10 10 ACCOUNTING NEW YORK
7698 BLAKE MANAGER 30 20 RESEARCH DALLAS
7782 CLARK MANAGER 10 30 SALES CHICAGO
7566 JONES MANAGER 20 40 OPERATIONS BOSTON

Primary key Foreign key Primary key


Database Systems Slide 24
Integrity Constraints or Integrity Rules
• To maintain the accuracy & integrity of data in the database
• There are the following classes and Types of Integrity
Constraints:
a) Static or Schema Constraints
1. Domain Constraints
2. NOT NULL Constraint
3. Entity Integrity
4. Referential Integrity
b) Dynamic Constraints
1. Action Assertion / Business Rules

Database Systems Slide 25


Integrity Constraints or Integrity Rules
1. Domain Constraints
• Domain: Set of values that may be assigned to an
attribute
Attribute Description Domain
Empno Set of all possible Employee Numbers (PK) Character : size 5
Emp_Name Set of all possible Employee Names Character : size 25
(NOT NULL)
Job CLERK, MANAGER, SALESMAN etc. Character : size 15
Sal Sal is between 6000 and 40000 Numeric : size 5
Rank Rank can be between 1 and 5 Numeric : size 1

Database Systems Slide 26


Integrity Constraints or Integrity Rules
2. NOT NULL Constraint:
 It enforces that an attribute Ai of a relation R must have

some value.
 Example: if student tuple must have a valid, non-Null value

for the Name attribute, then Name of STUDENT is required


to be NOT NULL
3. Entity Integrity:
 All PK entries are unique and No Component of the PK of a

relation is allowed to have NULL values.


 Note: Some other attributes of a relation may also be

constrained to disallow null values, even though they are


not members of the primary key.
 Formally, t[PK]  null for any tuple t in r(R)

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 27
Integrity Constraints or Integrity Rules
3. Referential Integrity:
• It is a constraint involving two relations called referencing relation and
referenced or target relation.
• It specifies a relationship among tuples in two relations.
• A tuple t1 in R1 (referencing relation) is said to reference a tuple t2 in
R2 (referenced relation) if t1[FK] = t2[PK].
• The database must not contain any unmatched FK values.
• The value in the foreign key column (or columns) FK of the referencing
relation R1 can be either:
1. A value of an existing primary key value of a corresponding
primary key PK in the referenced relation R2, or
2. a null.
• In case (2), the FK in R1 should not be a part of PK of its containing
relation.

Database Systems Slide 28


Integrity Constraints or Integrity Rules
3. Referential Integrity:

CUSTOMER

Customer_ID Customer_Name Address City State Zip

ORDER

Order_ID Order_Date Customer_ID

ORDER_LINE

Order_ID Product_ID Quantity

PRODUCT

Product_ID Prduct_Desc Product_Finish Standard_Price On_Hand

Database Systems Slide 29


Integrity Constraints or Integrity Rules
3. Referential Integrity:
Q: What should happen if an attempt is made to delete a
record from a target table, while some matching records exist
in the referencing table?
• There are three possible options:
a) Not Allowed. This is the default option.
b) Cascade: All the matching records in the referencing table
will also be deleted.
c) Nullify: Null will be placed in the FK. This option is only
applicable if the FK is not a part of PK.

Database Systems Slide 30


Integrity Constraints or Integrity Rules

4. Action Assertion
• Control on the actions of the organization
• “An Employee can avail a certain facility only after a
certain period of service.

Database Systems Slide 31


Data Dictionary
Data Dictionary (D/D) contains metadata to provide
detailed information of all tables within the database.

Database Systems Slide 32


Data Structure Diagram or Bachman Diagram
• It is the graphical representation of the association or
relationship between the entities or relations of a relational
database.
• This diagram is very helpful for conceptual understanding.
• Example: -------

Database Systems Slide 33


Reading Material
• Book sections 2-5, 3-1 to 3-3, 3-5

Thank you for your attention.


Asif Sohail

Assistant Professor
University of the Punjab
Punjab University College of Information Technology (PUCIT)
Allama Iqbal (Old) Campus, Anarkali
Lahore, Pakistan
Tel: +92-(0)42-111-923-923 Ext. 154
E-mail: [email protected]

Database Systems Slide 34

You might also like