Lecture 6
Lecture 6
Lecture # 6
Summary: Previous Lecture
Relational model terminology
Mathematical relations
Database relations
Properties of a relation
Relational Keys
Superkey
Candidate key
Primary key
Alternate key
Foreign key
Superkey
An attribute, or set of attributes, that
uniquely identifies a tuple within a relation
A super key may contain additional attributes
that are not necessary for unique identification
Consider Branch relation:
{branchNo, street, city, postcode}
{branchNo, street, city}
{branchNo, city} or {branchNo, postcode}
Candidate Key
Superkey (K) such that no proper subset is
a superkey within the relation
In each tuple of R, values of K uniquely identify that tuple
(uniqueness)
No proper subset of K has the uniqueness property
(irreducibility)
In simple words, superkey with minimal attributes
Instance of a relation cannot be used to prove uniqueness
Consider Branch relation
branchNo or postcode
Primary Key
Candidate key selected to identify tuples
uniquely within relation
Consider Branch relation
Primary key: branchNo
Alternate Keys
Candidatekeys that are not selected to be
primary key
Consider Branch relation
Alternate key: postcode
Foreign Key
Attribute, or set of attributes, within one
relation that matches candidate key of some
(possibly same) relation
Used to represent relationship
In the Branch relation, branchNo is the primary key
However, in the Staff relation the branchNo attribute
exists to match staff to the branch office they work in
In the Staff relation, branchNo is a foreign key
branchNo in the Staff relation targets the primary key
attribute branchNo in the home relation, Branch
Representing Relational Database Schemas
B025
General Constraints
Additional rules specified by users or
database administrators that define or
constrain some aspect of the enterprise
For example, If upper limit of 20 exists for number of
Staff that may work at a branch office
DBMS should enforce it
Level of support for general constraints varies from
system to system
Views in Relational Model
Base Relation
Named relation corresponding to an entity in conceptual
schema, whose tuples are physically stored in database
View
The dynamic result of one or more relational operations
operating on the base relations to produce another
relation
A view is a virtual relation that does not necessarily exist
in the database but can be produced upon request by a
particular user, at the time of request
Views
A virtual relation that does not necessarily
actually exist in the database but is
produced upon request, at time of request
Contents of a view are defined as a query on
one or more base relations
Views are dynamic, meaning that changes
made to base relations that affect view
attributes are immediately reflected in the
view
Purpose of Views
Provides powerful and flexible security
mechanism by hiding parts of database
from certain users
Permits users to access data in a customized
way, so that same data can be seen by
different users in different ways, at same
time
Can simplify complex operations on base
relations
Views Supporting External Model
For example
A user might need Branch tuples along with names of
managers
Some members of staff should see Staff tuples without the
salary attribute
Attributes may be renamed or the order of attributes
changed e.g. branchNo attribute of branches is
represented by Branch Number
Views & Logical Data Independence
Views allow a more significant type of
logical data independence that supports the
reorganization of the conceptual schema
For example, if a new attribute is added to a relation,
existing users can be unaware of its existence if their views
are defined to exclude it
If an existing relation is rearranged or split up, a view
may be defined so that users can continue to see their
original views
Updating Views
All updates to a base relation should be
immediately reflected in all views that
reference that base relation
If view is updated, underlying base relation
should reflect change
Updating Views..
There are restrictions on types of
modifications that can be made through
views:
Updates are allowed if query involves a single base
relation and contains a candidate key of base relation
Updates are not allowed involving multiple base relations
Updates are not allowed involving aggregation or
grouping operations
Summary
Relational keys
Integrity constraints
Views