02 Relational Model
02 Relational Model
02 Relational Model
Relational Model
[PM Jat, DAIICT, Gandhinagar]
Content:
What is a Relation
Relation Characteristics
Relation instance and relation schema
Relation constraints
Schema Examples
Languages for performing operations on relations
What is a relation?
Origin of relational model lies in “relation” in mathematics. The model was first proposed by Dr.
E.F. Codd of IBM Research through paper "A Relational Model for Large Shared Data Banks" in
Communications of the ACM, June 1970.
To understand what the relation in relational model is, let us begin with relation in math.
Now let us try using this concept in some real situation. Assume that
(101,Rahul,BCS,7.5)
(102,Vikash,BIT,8.6)
(103,Shally,BEE,5.4)
(104,Alka,BIT,6.8)
(105,Ravi,BCS,6.5)
Another example:
Consider four sets C, T, S, G; where
C is set of course numbers (offered in an institute)
T is set of term-ids
S set of student IDs
G is set of grades
Following is a relation drawn from these sets -
(IT214, Autumn-2017, 201501123, BB)
(IT214, Autumn-2016, 201501123, CD)
…
Each tuple, in this case represents fact of an event of “student registering in a course offered in a
term”. Tuples of this relation can be interpreted as following – first tuple captures a fact that Student
ID 201501123 registered in course IT214 in Autumn-2017 semester, and scored BB grade.
We name to this relations as Registers. Below is depiction of some snapshot of relation instance.
CourseNo Term StudentID Grade
IT214 Autumn-2017 201501123 DE
IT214 Autumn-2016 201501123 BC
…
Notations:
Schema: R(A1, A2, An), where R is name of relation and A1 through An are name of
attribute in relation R
Domain for attribute Ai is defined as dom(Ai)
r(R) for relation [instance] of schema R
Tuple: Two ways to represent a tuple
As ordered list of n-values; t = <v1, v2, ..., vn> ; or
As set of n (attrib-name, attrib-value) pairs: t = {(A2,v2), (A1,v1), ..., (An,vn)}
Notationally, value of attribute Ai in tuple t, can be expressed as t[Ai] or t.Ai
Similarly, t[Au, Av, ..., Aw] refers to the sub-tuple of t containing the values of attributes
Au, Av, ..., Aw, respectively in t
Relation constraints
Constraints are data existential rules, and are also called “Database Integrity Constraints”.
Recall: constraints are part of schema, and DBMS is to ensure compliance of constraints on every
valid state of database.
Domain constraints
Attribute values are drawn from their corresponding domain.
Key Constraints
Primarily this constraint requires that every tuple in a relation is distinct.
In other words, we say this as following. Suppose, r is a relation; and R is its schema. Key
constraints requires that there are no two tuples t1 and t2 in relation r, where t1=t2. It can also be
expressed as t1[R] != t2[R], where R is basically set of all attributes of the relation r.
However, for checking uniqueness of a tuple, we may not check values for all attributes; checking
for some subset often suffices the purpose.
Super-key
Some subset SK of R such that no tuple t1 and t2 can be same in a relation r; i.e.
t1[SK] != t2[SK]. Super Key is subset of attributes that are enough to check uniqueness of tuples in
a relation.
Key
Some minimal subset K of R such that no tuple t1 and t2 can be same in a relation r; i.e.
t1[K] != t2[K]. Key is minimum subset of attributes that are enough to check uniqueness of tuples
in a relation.
Super key can be defined in terms of Key: any superset of K (and subset of R) is super key.
Key can also be defined in terms of Super Key; minimal subset K of super-key such that
t1[K] != t2[K].
Motivation of define Key is that we do not have to check for all attributes of relation for ensuring
uniqueness of tuples in a relation. Checking attribute in Key are enough.
Remember Keys here are sets of attributes; even though in most cases it will singleton set.
A relation might have multiple keys. Also key might be “composite”, that is having more than one
attribute.
Foreign Key
In relations, we use foreign key to capture associations between entities.
Figure below depicts student entities associated with program entities. This association is captured
by referring corresponding tuple.
In this diagram, we have two student entities represented by two tuples in student relation. Refer
encircled values; they are values for ProgID attribute. Values BEE, and BME are basically
reference to program tuples. The attribute having reference value is called foreign key. The relation
having reference is called referencing relation. Relation in which referenced tuple is, called
referencing relation.
Foreign key is an attribute (or attribute set) in a relation, that is used for referencing purpose. It is
used to refer to a tuple in some relation (can be in same relation). Referencing happens by storing
value of key attributes of target relation. Recall that key values are enough to identify a tuple.
In the schema, conceptually we define foreign keys as following:
Suppose we have a relation schema R that has attributes A,B,C,D, E. Let us say there is another
relation schema S with attributes P,X,Y,Z. Suppose value of attribute D in relation r (instance of R)
is to refer attribute p in relation s (instance of S); then we express Foreign Key as following- D is a
foreign key in R that refers to attribute P of relation S. It should be obvious that attribute P should
be key of relation schema S. Why? That is when we will be able to unambiguously refer to a tuple
in s.
Formally we can explain it as following: Suppose relation R has a foreign key FK, that refers to
attribute K in relation S. Suppose t1 is a tuple in r; foreign key constraints requires that there should
be a tuple t2 in s such that t1[FK] = t2[K] or t1[FK] is null.
Summary:
1. We have seen how mathematical relation has been adapted to represent databases.
2. A relation in relational model is set of tuples. A tuple is ordered list of values for
respective attributes. A tuple can also be represented as set of attribute value pairs.
3. Defined: Key (candidate key), Super key, and Foreign Key.
4. Learned various types of Constraints: Domain, Key, Not Null, Entity Integrity, and
Referential Integrity.
5. Foreign keys are means of recording “associations”. Value in a foreign key refers to
another tuple. FK specification tells following: attribute that is FK; attribute (and in
which relation) to which the foreign key refers.
6. Referential integrity constraint tells us that value in a foreign key must refer to some
existing tuple in referred relation.
Examples
1. ProgID in student referring refering to ProgID in program relation [fig below]
2. DID in program referring to DID in Department [fig below]
3. Relation: Offers(CouseNo, TermID, InstructorID), all three attributes referring to key of
respective relation. Key of this relation is?
4. Relation: Register(CouseNo, TermID,StudentID,Grade), {CourseNo, TermIID} compositely
referring to Offers, and StudentID referring to StudentID in Student relation.
Relational Algebra
Relational algebra allows expressing queries as algebraic expression, where operands are relations.
In example below, sigma (σ) is an operation, employee is operand relation, and logical expression is
parameter to the operation.
σ DNO = 4 AND SALARY > 50000 (employee)
This expression outputs tuples that member of employee relation, and value of salary attribute is >
50000 and value of dno attribute is equals to 4.
Relational algebra expressions have closure property that is result of an expression is a valid
relation.
Relational Calculus
A query in relational calculus is expressed as a logic expression; above relational algebra query is
expressed in relational following is a query, expressed in tuple relational calculus, as following.
{t| EMPLOYEE(t) AND t.salary > 50000 AND t.dno=4) }
Below is another query that outputs specific attributes of tuple tuple t, where t is such that member
of employee, and there t.dno and d.dno matches where d is tuple of department relation, and d.name
is „Research‟
It may be noted that in calculus expressions are more declarative (rather than procedural) in nature;
no concept of operation and their order.