W3. The Relational Model
W3. The Relational Model
D1 ´ D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
• Alternative way is to find all combinations of elements with first from D1 and
second from D2.
Database Relations
Mathematical Definition of Relation
Any subset of Cartesian product is a relation.
e.g.
R = {(2, 1), (4, 1)}
•May specify which pairs are in relation using some condition for selection;
e.g.
– second element is 1:
R = {(x, y) | x ÎD1, y ÎD2, and y = 1}
– first element is always twice the second:
S = {(x, y) | x ÎD1, y ÎD2, and x = 2y}
Database Relations
• Consider three sets D1, D2, D3 with Cartesian Product D1 ´ D2 ´ D3;
e.g.
• Any set of n-tuples from this Cartesian product is a relation on the n sets.
Database Relations
Relation schema
Named relation defined by a set of attribute and domain name pairs.
• Let A1, A2, . . . , An be attributes with domains D1, D2, . . . , Dn. Then the
set {A1:D1, A2:D2,. . . , An:Dn} is a relation schema.
• A relation R defined by a relation schema S is a set of mappings from the
attribute names to their corresponding domains. Thus, relation R is a set
of n-tuples:
• A table is simply a physical representation of such a relation.
Database Relations
Relational database schema
Set of relation schemas, each with a distinct name.
• If R1, R2, . . . , Rn are a set of relation schemas, then we can write the
relational database schema, or simply relational schema, R, as:
Properties of Relations
• Relation name is distinct from all other relation names in relational schema.
• Each cell of relation contains exactly one atomic (single) value.
• Each attribute has a distinct name.
• Values of an attribute are all from the same domain.
• Each tuple is distinct; there are no duplicate tuples.
• Order of attributes has no significance.
• Order of tuples has no significance, theoretically.
Relational Keys
Superkey
• An attribute, or set of attributes, that uniquely identifies a tuple within a
relation.
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).
Relational Keys
Primary Key
• Candidate key selected to identify tuples uniquely within relation.
Alternate Keys
• Candidate keys that are not selected to be primary key.
Foreign Key
• Attribute, or set of attributes, within one relation that matches candidate
key of some relation.
Integrity Constraints
Null
• Represents value for an attribute that is currently unknown or not applicable
for tuple.
• Deals with incomplete or exceptional data.
• Represents the absence of a value and is not the same as zero or spaces,
which are values.
Integrity Constraints
Entity Integrity
• In a base relation, no attribute of a primary key can be null.
Referential Integrity
• If foreign key exists in a relation, either foreign key value must match a
candidate key value of some tuple in its home relation or foreign key value
must be wholly null.
General Constraints
• Additional rules specified by users or database administrators that define or
constrain some aspect of the enterprise.
Views
Base Relation
A named relation corresponding to an entity in the conceptual schema, whose
tuples are physically stored in the 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
Properties
•Views are dynamic
•A view is a virtual or derived relation
•Does not necessarily exist in storage
•The contents of a view are defined as a query
Purpose
•It provides a powerful and flexible security mechanism by hiding parts of the
database from certain users.
•It permits users to access data in a way that is customized to their needs
•It can simplify complex operations on the base relations.
Updating Views
•Updates are allowed through a view defined using a simple query involving a
single base relation and containing either the primary key or a candidate key of
the base relation.
• Updates are not allowed through views involving multiple base relations.
•Updates are not allowed through views involving aggregation or grouping
operations