Lecture 3a - Logical DB Design Part 1
Lecture 3a - Logical DB Design Part 1
ICSE 6203:
Database Management Systems
Lecture 3a
Logical Database Design and the
Relational Model
(part 1)
1
2/21/2012
2
2/21/2012
3
2/21/2012
Login
4
2/21/2012
A Relational Database
• A relational database is a collection of relations with
distinct relation names.
• The relational database schema is the collection of
schemas for the relations in the database.
• For example, a university database with relations
called Students, Faculty, Courses, Rooms, Enrolled,
Teaches, and Meets_In.
• An instance of a relational database is a collection of
relation instances, one per relation schema in the
database schema; of course, each relation instance
must satisfy the domain constraints in its schema.
Relational Keys
5
2/21/2012
Foreign Keys
• Consider the following relations:
EMPLOYEE1(Emp_ID, Name, Dept_Name, Salary)
DEPARTMENT(Dept_Name, Location, Fax)
6
2/21/2012
Example of a Database
• The structure of the database is described by the
use of a conceptual schema, which is a description
of the overall logical structure of a database.
• There are two common methods for expressing a
conceptual schema:
a) Short text statements, in which each relation is
named and the names of its attributes follow in
parentheses
b) A graphical representation, in which each relation is
represented by a rectangle containing the attributes for
the relation.
7
2/21/2012
8
2/21/2012
Primary Key
Foreign Key (implements
1:N relationship between
customer and order)
Integrity constraints
• These help maintain the accuracy and integrity of the
data in the database
• Domain Constraints -
—a domain is the set of allowable values for an attribute.
• Domain definition usually consists of 4 components:
—domain name,
—meaning,
—data type,
—size (or length), allowable values/allowable range (if
applicable)
• Entity Integrity ensures that every relation has a
primary key, and that all the data values for that primary
key are valid. No primary key attribute may be null.
9
2/21/2012
Entity integrity
• In some cases a particular attribute cannot be
assigned a data value, e.g. when there is no
applicable data value or the value is not known
when other values are assigned
Integrity constraints
• A database is only as good as the information stored
in it, and a DBMS must therefore help prevent the
entry of incorrect information.
• An integrity constraint (IC) is a condition specified
on a database schema and restricts the data that can
be stored in an instance of the database.
• If a database instance satisfies all the integrity
constraints specified on the database schema, it is a
legal instance.
• A DBMS enforces integrity constraints, in that it
permits only legal instances to be stored in the
database.
10
2/21/2012
Integrity constraints
• A Referential Integrity constraint is a rule
that maintains consistency among the rows of
two relations – it states that any foreign key
value (on the relation of the many side) MUST
match a primary key value in the relation of the
one side. (Or the foreign key can be null)
• In the following Fig., an arrow has been drawn
from each foreign key to its associated primary
key. A referential integrity constraint must be
defined for each of these arrows in the schema
Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table
11
2/21/2012
Referential integrity
Referential integrity
12
2/21/2012
Referential integrity
2. Cascade
automatically delete “dependent” side rows that
correspond with the “parent” side row to be
deleted, i.e. delete the associated orders, in which
case we lose not only the customer but also the
sales history
3. Set-to-Null
set the foreign key in the dependent side to null if
deleting from the parent side - an exception that
says although an order must have a customer_ID
value when the order is created, Customer_ID can
become null later if the associated customer is
deleted [not allowed for weak entities]
Action assertions
13
2/21/2012
14
2/21/2012
15
2/21/2012
16
2/21/2012
Well-structured relations
Insertion anomaly
17
2/21/2012
Anomalies
18
2/21/2012
19
2/21/2012
(a) CUSTOMER
entity type with
simple
attributes
20
2/21/2012
Composite attributes
(a) CUSTOMER
entity type with
composite
attribute
21
2/21/2012
Multi-valued attributes
• Here two new relations (rather than one) are
created
Multi-valued attributes
22
2/21/2012
23
2/21/2012
24
2/21/2012
Foreign key
25
2/21/2012
26
2/21/2012
Foreign key
27
2/21/2012
New
Foreign key intersection
relation
Foreign key
28
2/21/2012
29
2/21/2012
30
2/21/2012
Resulting relations
31
2/21/2012
Identifier assigned
• Sometimes an identifier (called a surrogate identifier
or key) is assigned to the associative entity type on
the ER diagram. There are 2 possible reasons:
a) The associative identity type has a natural
identifier that is familiar to end users
b) The default identifier (consisting of identifiers for
each of the participating entity types) may not
uniquely identify instances of the associative
identity
• The process for mapping the associative entity is
now modified
32
2/21/2012
Identifier assigned
Identifier assigned
• Shipment_No has been chosen as the identifier for
two reasons:
1. Shipment_No is a natural identifier for this entity
that is very familiar to end users
2. The default identifier consisting of the combination
of Customer_ID and Vendor_ID does not uniquely
identify the instances of shipment. In fact, a given
vendor will make many shipments to a given
customer
• The new associative relation is named SHIPMENT,
with primary key Shipment_No. Customer_ID and
Vendor_ID are included as foreign keys in this
relation
33
2/21/2012
Associative entity
34