ADBMS - Lectures 1-5
ADBMS - Lectures 1-5
An Introduction to Relational
Databases
CHAPTER 3
The Relational Model-
Introduction
Proposed by Edgar. F. Codd(1923-2003) in the early
seventies.
Most of the modern DBMS are relational.
Simple and elegant model with a mathematical basis.
Led to the development of a theory of data
dependencies and database design.
Relational algebra operations –crucial role in query
optimization and execution.
Laid the foundation for the development of
Tuple relational calculus and then
Database standard SQL
Relational Model Basic
The relational model gives us a single way
to represent data: as a two-dimensional
table called a relation.
Attributes
Schemas
Tuples
Domains
Relational Databases: Terminology
The Relational Model
Relational system based on Relational Model of
data
Structural
Data is perceived by users as tables, ONLY tables
Integrity
Data subject to specific integrity requirements
Manipulation
Operators derive tables from other tables
• Select
• Project
• Join
Properties of Relational Model
No Duplicate Tuples – A relation cannot contain two or
more tuples which have the same values for all the
attributes. i.e., In any relation, every row is unique.
Tuples are unordered – The order of rows in a relation is
immaterial.
Attributes are unordered – The order of columns in a relation is
immaterial.
Attribute Values are Atomic – Each tuple contains exactly one
value for each attribute.
It may be noted that many of the properties of relations
follow the fact that the body of a relation is a
mathematical set.
Relation Scheme
Consists of relation name, and a set of attributes or field
names or column names. Each attribute has an
associated domain.
Example:
student ( studentName : string,
rollNumber :string,
phoneNumber : integer,
yearOfAdmission : integer,
branchOfStudy : string )
101 HW3 50
101 02-08-94 002
101 SW1 150
102 11-08-94 003
102 HW2 10
103 21-08-94 003
103 HW3 50
104 28-08-94 002 104 HW2 25
105 30-08-94 005 104 HW3 100
26
E-R Design (Revision)
Data modeling –
Technique for organizing and documenting a
system’s data. Sometimes called database
modeling.
27
Sample Entity Relationship Diagram
28
Data Modeling Concepts: Entity
Entity – a class of persons, places, objects, events, or
concepts about which we need to capture and store
data.
Entity instance – a single occurrence of an entity.
30
Data Modeling Concepts: Data Type
Data type – a property of an attribute that identifies what
type of data can be stored in that attribute.
34
Data Modeling Concepts: Relationships
35
Data Modeling Concepts: Cardinality
36
Cardinality Notations
37
Data Modeling Concepts: Degree
Relationships may exist between
more than two entities and are called
N-ary relationships.
The example ERD depicts a ternary
relationship.
Associative
Entity
38
How to make ER Diagram
1.) Use rectangles representing entity sets.
2.) Use ellipses representing attributes
3.) Use ellipses representing attributes.
4.) Use diamonds representing relationship sets.
5.) Use lines linking attributes to entity sets and
entity sets to relationship sets.
39
40
Logical database design
and the relational model
41
Objectives of logical design...
42
Relational database components
Data structure
Data organized into tables
Data manipulation
Add, delete, modify, and retrieve using SQL
Data integrity
Maintained using business rules
43
Some rules...
Every table has a unique name.
Attributes in tables have unique names.
Every row is unique.
The order of the columns is irrelevant.
The order of the rows is irrelevant.
44
The key...
45
Constraints
Domain constraints
Allowable values for an attribute as defined
in the domain
Entity integrity constraints
No primary key attribute may be null
Operational constraints
Business rules
Referential integrity constraints
46
Referential integrity constraint
47
Transforming ER diagrams
into relations
Mapping entities to relations
48
Looks like this using relational
schema notation
49
Transforming E-R diagrams into
relations
Mapping weak entities
Weak Entity is an entity that cannot be
uniquely identified by its attributes alone;
therefore, it must use a foreign key in
conjunction with its attributes to create a
primary key.
50
Example of mapping a weak entity
51
Looks like this using relational
schema notation
52
Transforming E-R diagrams into
relations
Mapping binary relationships
One-to-many - primary key on the one side
becomes a foreign key on the many side
Many-to-many - create a new relation with the
primary keys of the two entities as its primary key
One-to-one - primary key on the mandatory side
becomes a foreign key on the optional side
53
Example of mapping a 1:M
relationship
54
Looks like this using relational
schema notation
55
Example of mapping an M:M
relationship
56
Looks like this using relational
schema notation
57
Mapping a binary 1:1 relationship
58
Looks like this using relational
schema notation
59
Transforming E-R diagrams into
relations
Mapping associative entities
Identifier not assigned
• Default primary key for the association
relation is the primary keys of the two
entities
Identifier assigned
• It is natural and familiar to end-users
• Default identifier may not be unique
60
Mapping an associative entity
with an identifier
61
Looks like this using relational
schema notation
62
For example...
Emp_Num
Emp_Num
EMPLOYEE
EMPLOYEE
Emp-Name Supervises
Emp-Name Supervises
Emp_Address
Emp_Address
63
Would look like...
references
64
Mapping a ternary relationship
65
Looks like this using relational
schema notation
66
Transforming E-R diagrams into
relations
Mapping Supertype/subtype relationships
Create a separate relation for the
supertype and each of the subtypes
Assign common attributes to supertype
Assign primary key and unique
attributes to each subtype
Assign an attribute of the supertype to
act as subtype discriminator
67
Mapping Supertype/subtype
relationships
68
Would look like this...
69
Tutorial-1
ER Diagram
Exercise #1
Draw an ER diagram for the following application
from the manufacturing industry:
1. Each supplier has a unique name.
2. More than one supplier can be located in the same city.
3. Each part has a unique part number.
4. Each part has a colour.
5. A supplier can supply more than one part.
6. A part can be supplied by more than one supplier.
7. A supplier can supply a ¯xed quantity of each part.
Solution-1
Exercise #2
Construct an E-R diagram for a car-insurance
company whose customers own one or more cars
each. Each car has associated with it zero to any
number of recorded accidents.
Solution
Exercise #3
Construct an E-R diagram for a hospital with a set
of patients and a set of medical doctors. Associate
with each patient a log of the various tests and
examinations conducted.
Solution
Exercise 4
Create a E-R diagram of the Motor Vehicle Branch administers
driving tests and issues driver's licenses. Any person who wants a
driver's license must first take a learner's exam at any Motor Vehicle
Branch in the province. If he/she fails the exam, he can take the
exam again any time after a week of the failed exam date, at any
branch. If he passes the exam, he is issued a license (type =
learner's) with a unique license number. A learner's license may
contain a single restriction on it. The person may take his driver's
exam at any branch any time before the learner's license expiry
date (which is usually set at six months after the license issue date).
If he passes the exam, the branch issues him a driver's license. A
driver's license must also record if the driver has completed driver's
education, for insurance purposes.
Solution
Steps to follow:
1.Find out the entities in the spec.
2. Find out the relationships among the entities.
3. Figure out attributes of the entities and (if any) of the
relationships.
4. Figure out constraints between entities and
relationships.
5. check to see if you don't miss anything in spec.
Home Work
Imagine that you are a consultant called into the o±ce of a new Nepalese startup
called Movies4All. This company admires the success of the Internet Movie
Database (IMDb) in collecting and publishing movie-related information via the
web and thinks that it can set up its own database to rival the IMDb. The
payback to Movies4All, they hope, will be advertising revenue once thousands of
people-per-day start to visit their site looking for movie information/trivia/etc.
Putting aside your scepticism of this kind of business model, you ask them
precisely what content and functionality they want in their on-line database. They
suggest that looking at the IMDb (http://www.imdb.com/) would be a good start. If
you press them further, they'll tell you that Movies4All should maintain the
following kinds of information:
1. movies: title, cast, crew, story-line, genre, studio, year made/released
2. actors: biographical, photos, films they appeared in, ...
3. crew: occupation (director, cameraman,...), biographical, films, ...
4. studios: contact information, historical information (e.g. heads), ...
5. awards: given to movies or people who work in/on them
6. movie jobs: information about what people like "gaffers" actually do
7. session times: for movies being screened in cinemas all over the world
Contd…
All information in the system should be accessible (readable) to anyone on the
Internet. However, Movies4All staff are the only ones who are allowed to update
the database. They also want to provide for registered users of the system, who
can rate movies and post movie reviews. Registered users are also allowed to
provide new information to the Movies4All staff, who will check its validity and
then incorporate it into the site. Some ideas on the functionality that the system
should provide:
1. Movies4All staff and registered users have a username and password for
login authentication
2. Movies4All staff can add, update and (infrequently) delete any information in
the database
3. registered users can rate movies, post reviews, and post suggested changes
to Movies4All staff
4. anyone can search for movies, actors, crew, ... via keyword
5. an advanced search facility allows search by individual aspects of
movies/actors/crew/studios
6. every reference to a person/movie/studio is a hyperlink to a page giving the
details.
Well-structured relations
Well-structured relations contain minimal
redundancy and allow insertion, modification, and
deletion without errors or inconsistencies
Redundancies in a table (such as more than one
entry for each EMPLOYEE) may result in errors
and inconsistencies (anomalies) when the table is
updated
Anomalies are errors or inconsistencies resulting
from redundancy
Insertion anomaly
Deletion anomaly
Modification anomaly
82
Data normalization
Normalization is a formal process for deciding
which attributes should be grouped together in
a relation
Objective: to validate and improve a logical
design so that it satisfies certain constraints
that avoid unnecessary duplication of data
Definition: the process of decomposing
relations with anomalies to produce smaller,
well-structured relations
83
Normalization
Data normalization also may improve data consistency
and simplify future extension of the logical data model.
The formal classifications used for describing a
relational database's level of normalization are called
normal forms
Normal Forms is abbreviated as NF
What Happens without
Normalization
A non-normalized database can suffer from data
anomalies:
A non-normalized database may store data representing a
particular referent in multiple locations. An update to such
data in some but not all of those locations results in an
update anomaly, yielding inconsistent data. A normalized
database prevents such an anomaly by storing such data
(i.e. data other than primary keys) in only one location.
What Happens without
Normalization
A non-normalized database may have inappropriate
dependencies, i.e. relationships between data with no
functional dependencies. Adding data to such a database
may require first adding the unrelated dependency. A
normalized database prevents such insertion anomalies
by ensuring that database relations mirror functional
dependencies.
87
Steps in normalization
88
Functional dependencies and keys
Functional dependency: the value of one
attribute (the determinant) determines the value
of another attribute
A -> B, for every valid instance of A, that value
of A uniquely determines the value of B
Candidate key: an attribute or combination of
attributes that uniquely identifies an instance
Uniqueness: each non-key field is functionally
dependent on every candidate key
Non-redundancy
89
First normal form
No multi-valued attributes.
Every attribute value is atomic.
90
Table 1
Table 2
96
Subject Table
Subject_ID Subject
Author Table
1 MySQL
Author_ID Last Name First Name
2 Computers
1 Silberschatz Abraham
2 Korth Henry
Book Table
Book_Author Table
Book_Subject Table
ISBN Author_ID
0072958863 2 0072958863 1
0471694665 1 0072958863 2
0471694665 2 0471694665 2
2NF Table
Publisher Table
1 McGraw-Hill
Book Table
ISBN Title Pages Publisher_ID
105
Relation with Transitive Dependency
106
Transitive dependency in SALES
relation
107
Removing a transitive
dependency
108
Relations in 3NF
109