Lecture #3-1. Relational Model
Lecture #3-1. Relational Model
2
The Retrospective of Lecture #2: ER-
modelling
1. The structure: entity, attribute, relationship.
2. Cardinality: 1:1, 1:N, M:N. Optional & mandatory.
3. Attributes: unary, binary, n-ary.
4. ER should not be like exact “tables”.
5. EER: generalization, specialization, aggregation.
3
Relational model
1. Structure (a relation only).
2. Integrity constraints (the accuracy and consistency of data).
3. Relational Algebra: functional aspect (relational operations).
Not only is it framework for constructing SQL operators like SELECT but also
this is important for DBMS optimizer to find an optimal execution plan.
4
12 relational model rules by E.Codd (1)
0. Foundation Rule: For any system that is advertised as, or claimed to be, a
relational database management system, that system must be able to
manage databases entirely through its relational capabilities.
6. View Updating Rule: Different views created for various purposes should
be automatically updatable by the system. (*does not work in practice)
7. High level insert, update and delete rule: Relational Model should
support insert, delete, update etc. operations at each level of relations. Also,
set operations like Union, Intersection and Minus should be supported. 6
12 relational model rules by E.Codd (3)
8. Physical data independence: Any modification in the physical location of a table
should not enforce modification at application level.
10. Integrity Independence: Integrity constraints modified at database level should not
enforce modification at application level.
11. Distribution Independence: Distribution of data over various locations should not
be visible to end-users. (it is only 1 server for user view)
12. Non-Subversion Rule: Low level access (single-record-at-time) to data should not be
able to bypass integrity rule to change data (multiple-record-at-time). (modern RDBMS
violates by using bulk insertion and constraint disabling) 7
Structural aspect: Relation
The relation is defined as a subset of the Cartesian product.
In turn, the Cartesian product D1 × D2 ×,…, × Dn for given finite sets D1, D2,
…, Dn (optionally different) is called the set of products of the form
8
Cartesian product & relation example
Here is an example of a Cartesian product.
Suppose that two sets D1 = {1,2,3} and D2 = {a, b} are given, then the
Cartesian product D will be:
D = D1 × D2 = {1 × a, 1 × b, 2 × a, 2 × b, 3 × a, 3 × b} = cartesian product
10
Cartesian product example #2 Books
2 a2 3 Java 700
11
Relation: graphical representation
12
Properties of Relations
● Name of the relation is distinct from all other relations.
● Each attribute contains a distinct name.
● 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.
13
Integrity constraints
1. Entity Integrity Constraints (primary key)
2. Referential Integrity Constraints (foreign key)
3. Domain Constraints (not null, default, check, unique)
4. User-defined, operational Constraints (eg., “The begin date must be
before the end date”)
14
Keys
Super Key: The set of attributes which can uniquely identify a tuple is known
as Super Key.
Candidate Key: The minimal set of attributes which can uniquely identify a
tuple is known as candidate key.
Primary Key: There can be more than one candidate key in relation out of
which one can be chosen as the primary key.
Alternate Key: The candidate key other than the primary key is called an
alternate key
Foreign Key: If an attribute can only take the values which are present as
values of some other attribute (primary key), it will be a foreign key to the
15
attribute to which it refers.
Keys examples
Users
id name email birth_date gender boss
1 n1 [email protected] 01.01.2000 m 2
2 n2 [email protected] 01.02.2002 w
1 1
- “unique together”
- NULL is disallowed 1 2
1 2
Every relation must have PK
NULL 5
Try to use “natural” PK
17
NULL value
The special value in relational databases is NULL.
By the definition of the primary key, the attributes included in it cannot take
the value NULL.
18
NULL value
Logical operations NOT, AND and OR are generally performed in three-digit
logic, where: 1 = True, 0.5 = NULL, 0 = False.
In this case, for each value of the foreign key, which is defined in the
subordinate relation, there must be a tuple with the same value of the
primary key in the main relation (see lecture #2: ER to Relational examples).
The constraints on a domain include its domain name, description, data type,
size, and the set of allowable values. Other domain constraints may include
default value, number of decimal places, format, maximum and minimum
value. Each attribute is assigned to one domain constraint. Generally:
DEFAULT, UNIQUE, NOT NULL, CHECK.
Examples:
An library log item must have a begin date and an end date.
22
Relational algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output.
They accept relations as their input and yield relations as their output.
23