De Module2 RelationalModel
De Module2 RelationalModel
The relational model uses a collection of tables to represent both data and the relationships among those
data. Each table has multiple columns, and each column has a unique name.
A model basically defines the structure or organization of data and a set of operations on that data.
Relational model is a simple model in which the database is represented as a collection of relations,
where each relation is represented by a two dimensional table.
Tables are referred to as relations, the rows as tuples or records and the columns as attributes or fields.
Domain is the pool of values from which the actual values of a column are drawn.
The table, the customer table, shows, for example, that the customer identified by customer-id 192-83-
7465 is named Johnson and lives at 12 Alma St. in Palo Alto.
The relational model is an example of a record-based model.
Each table contains records of a particular type.
Each record type defines a fixed number of fields, or attributes.
The columns of the table correspond to the attributes of the record type.
The relational model is at a lower level of abstraction than the E-R model.
Database designs are often carried out in the E-R model, and then translated to the relational model.
Features of Relational Model
1. It was formulated by Dr. E F Codd of IBM in 1970.
2. It is based on relations , which is a two dimensional table.
3. Rows represent tuples or records, and columns represent attributes or fields.
4. It doesn’t require the user to understand its physical representation.
5. It provides information about the contents and the structures in the table.
6. Tables show the logical but not the physical relationships.
1
Advantages
1. Ease of use.
Simple tabular representation helps the user define and query conveniently.
2. Flexibility
New data can be added, deleted, and even manipulated easily by using various basic operations.
3. Accuracy
Relational algeric operations are used to manipulate database, which are mathematical operations and
ensure accuracy.
Disadvantages
1. Reusability of structures is not possible.
2. At times, maintenance of structures becomes difficult.
CONCEPTS
Relation (or tables)
It is a two dimensional array of rows and columns containing single valued entries (entities) and no
duplicate rows. Each cell can have only one value and no two rows may be identical. It is represented by
combining attributes of the entities.
Domain
It is a set of permissible values that can be assigned to an attributes.
Relation schema
It specifies the relation’s name, its attributes and the domain of each attribute. If R is the relation name
and A1, A2, A3,……………. An is the list of attributes then R(A1, A2, A3……………. An) is the relation schema.
Each attribute in the relation schema takes a value from some specific domain i.e Domain(Ai ). The
relation schema is also called as intension.
2
Ex: student (Rollno: integer, Name :string, Address: string)
Cardinality of a relation
It is the total number of tuples in a relation(i.e number of rows ina table).
3
Tabular Representation of Weak Entity Sets
The weak Entity Set(W) is converted into a relation(R) ie table, containing all simple attributes ie
columns. If a strong entity set (E) is owner of a weak entity set then primary key of that strong entity set is also
included(which acts as a foreign key). Now the combination of primary key of the strong entity and key of
weak entity acts as primary key for the weak entity.
4
Tabular Representation of Relationship Sets
i. One-to-one relationship
Given a relationship R (one-to-one) involving two entities E1 and E2. One of the entity is
chosen (preferably with total participation) and primary key of the other entity is added to it as a
foreign key in the table. If any attributes are present for the relationship R, then those are also
included.
Ex : College ER diagram, head of relationship.
Department
Dept_no Deptname Facultyid
(primary key) (Foeign key)
Faculty
Facultyid Name Address Dept_no
(primary key) (Foreign key)
5
iii. Many-to-many relationship
Given a relationship R (one-to-one) involving two entities E1 and E2. Create a new table
for the relationship R, which includes the primary keys of both entities, as foreign keys. The
attributes of the relationship(if any) are also included in the new table.
Taughtby(new table)
Facuiltyid Courseid
(Foreign key) (Foreign key)
(Primary key)
Rollno Phoneno
(Foreign key)
(Primary key)
Checking-account
account-number balance Overdraft-amount
(Primary key)
7
Overlapping Generalization:
If the generalization is overlapping then create a table for the higher-level entity set.
For each lower-level entity set,create a table that includes a column for each of the attributes of that
entity set plus a column for each attribute of the primary key of the higher-level entity set.
Thus, for the E-R diagram with overlapping generalization, we have three tables:
i. account, with attributes account-number and balance
ii. savings-account, with attributes account-number and interest-rate
iii. checking-account, with attributes account-number and overdraft-amount
saving-account
account-number interest-rate
(primary key)
checking-account
account-number Overdraft-amount
(primary key)
Relational Constraints
Constraints refer to the various restrictions imposed on the data , which can be specified on a
relational database.
Constraints are basically of three types
i. Domain Constraints:
They specify that each attribute in a relation (ie table) must contain an atomic value, only
from the corresponding domain. They are mainly created for defining the logically correct values
for an attribute of a relation, which allows attributes to be confined to a range of values.
8
ii. Key Constraints
It states that the key attribute value in each tuple (ie record) must be unique (ie no
duplicates). It ensures that the primary key can be used to identify the tuples uniquely.
iii. Integrity Constraints
Integrity refers to maintaining the consistency of data. Thus integrity constraints ensures
that changes made to the database doesn’t hamper consistency.
There are two types of integrity constraints:
a. Entity Integrity Constraints
It states that no primary key value can be null. NULL represents missing value.
b. Referential Integrity Constraints
It states that the database must not contain any unmatched foreign key values. The
foreign key must exactly match with the values of primary key in referenced relation( ie
table) or it must be null.
Fundamental Operations
The select, project, and rename operations are called unary operations, because they operate on one
relation.
The other three operations operate on pairs of relations and are, therefore, called binary operations.
9
A. Basic Set Operations
These are the binary operations ie operate on two sets or relations.
The two relations should be union compatible except in the case of Cartesian product.
Two relations r and s are said to be union compatible if :
1. The relations r and s must be of the same arity/degree. That is, they must have the same
number of columns.
2. The domains of the ith attribute of r and the ith attribute of s must be the same, for all i.
i. UNION (U)
If R1 and R2 are two union compatible relations then R3 = R1 U R2 is the relation containing
tuples(ie rows) that are either in R1 or in R2 or in both(but duplicates are eliminated)
ie R3 = {t / t R1 V t R2}
- It is commutative ( ie R1 U R2 = R2 U R1)
- It is associative ( ie R1 U(R2UR3 = (R1 U R2) U R3)
Ex :
R1 R2
Empid Name Deptno Empid Name Deptno
100 Smith 10 200 Jack 30
200 Jack 30 400 Elina 40
300 Henry 30 500 Bob 10
R1 U R2
Empid Name Deptno
100 Smith 10
200 Jack 30
300 Henry 30
400 Elina 40
500 Bob 10
10
ii. INTERSECTION(∩)
If R1 and R2 are two union compatible relations then R3 = R1 ∩ R2 is the
relation containing tuples(ie rows) that are both in R1 and R2.
ie R3 = {t / t R1 Λ t R2}
- It is commutative ( ie R1 ∩ R2 = R2 ∩ R1)
- It is associative ( ie R1 ∩(R2 ∩ R3 = (R1 ∩ R2) ∩ R3)
Ex : R1 ∩ R2
Empid Name Deptno
200 Jack 30
Ex: R1 − R2
Empid Name Deptno
100 Smith 10
300 Henry 30
11
Ex :
R1 R2
Empid Name Deptno
100 Smith 10
200 Jack 20
300 Henry
R1 X R2
Empid Name Deptno
100 Smith 10
100 Smith 20
200 Jack 10
200 Jack 20
300 Henry 10
300 Henry 20
B. Relational operations
These operations were developed for relational databases. They operate on one or more relations
to form another relation.
i. SELECT ( )
It is used to extract some specific tuples (ie rows or records) from one relation based on some
condition or criteria. It is denoted by .
ie conditions (Table)
where condition can use the logical operators (and), (or) and (negation) and usually of the
form
attribute operator value or attribute operator attribute
- It is commutative (ie condition 1 ( condition2 (R) ) = conditions 2 ( condition1 (R) ))
ii. PROJECT ( )
It is used to extract some specific attributes( ie columns or fields) from one relation based on
names of the attributes. It is denoted by the symbol pi( ).
ie Attribute list (Table)
Duplicate tuple values are eliminated.
Ex :
Name (R1) Deptno (R1)
Deptno
Smith 10
Jack 30
Henry 30
Π name ( Dept-no=10(Employee))
Π Deptid ( Deptname='Finance'(Department))
iii. JOIN ( ⋈ )
It allows combing of two relations to form a single new relation. It is used to select related
tuples(ie rows from two relations). It requires that both the joined relations must have at least one
domain compatible attribute, which is known as the join attribute .
13
ie R1 ⋈ joincondition R2
where joincondition can use the logical operators (and), (or) and (negation) and usually of the
form
R1 attribute operator R2 attribute
Degree(R1 ⋈ joincondition R2) <= Degree(R1) + Degree(R2)
The main difference between Cartesian product(ie X) and join(ie ⋈) is that , in join only
those tuples that satisfy the join condition are in the result, but whereas in Cartesian product all
combinations of the tuples are in the result. If no join condition is specified then all combinations of the
tuples qualify and it becomes a cartesian product. Thus join is a cartesian product with an added
condition.
There are various types of joins such as
a. Inner Join
- Equi Join
- Natural Join
- Non Equi Join
- Theta Join
- Self Join
b. Outer Join
- Left Outer join
- Right Outer Join
- Full Outer Join
INNER JOIN
It retrieves all the combined tuples from the two relations, which satisfy the join conditions. Extraneous
tuples from the relations are not retrieved. It can be further divided into various types
- Equi Join
It is a join in which equlity operator (ie. =) is only used in each of the join conditions. Thus two
relations are joined together using the equality of values in one or more columns, which results in
having one or more pairs of attributes having identical values in every tuple of the new result.
14
Ex:
Empinfo : Deptinfo :
Empid Ename Address Deptno Deptid Dname Mgrid
10 Smith BBSR 100 100 Accounts 10
20 Jack CTC 100 200 Production 30
30 Henry RKL 200
Thus the above join retrieves all tuples from the Cartesian product where Mgrid =Empid. But
the attribute pairs Deptid and Deptno, Mgrid and Empid have the same values in every tuple.
Employee Salgrade
Empid Empname Salary Grade Losal Hisal
101 Smith 12500 A 10000 15000
203 Jack 11380 B 15001 20000
308 Henry 34000 C 20001 40000
-
15
- Theta Join
It is a join in which any operator can be used, including the equality operator, in each of the join
conditions (ie <=, >=, < , >, or ≠ ). It is denoted as join condition Ɵ.
R1 Ɵ R2
Example:
- Natural Join( * )
It is a special case of equi join, which uses the equality operator (ie. =) for the join condition, but
it requires that the attribute name must be the same in both the relations(ie. the join attribute
name)
Instead of returning the pairs of identical attributes(ie same values in every tuples) which is
useless, it retains only one attribute from the pairs.
It is denoted by * and the condition need not be shown as it is assumed by default.
If the attribute names differ then the attribute name of one relation must be renamed before
applying natural join.
Rho (ie ρ ) can be used for renaming the fields of a relation.
ρ x (E): returns the result of expression E under the name x.
ρ x (A1, A2, A3… An) (E): returns the result of expression E under the name x with attributes
renamed to A1, A2, A3… An.
RENAME OPERATOR
ρemployee(Emp) Changes the name of Emp table to employee
16
Employee ρ EmployeeName / Name (Employee)
Ex :
Projinfo :
Projno Projname Plocation Deptid
1000 A CTC 100
2000 B BBSR 200
3000 C BBSR 200
Projinfo * Deptinfo
17
Degree of resultant relation = (Degree of relation1 +Degree of relation2) -1
- Self Join
It is a join in which the relation is joined with itself(ie as if two different relational).
Example:
18
OUTER JOIN
In join operations the tuples without a match and those having NULL value in the join attribute are eliminated.
Thus outer joins are used to retrieve additional tuples from one or both tables, even though they don’t satisfy
the join condition.
Ex:
Empinfo Empsal
Ename Address CityCode Ename Branchno Salary
Smith BBSR 10 Smith 100 10,000
Jack CTC 20 Henry 200 20,000
Henry RKL 30 Bob 300 30,000
Empinfo Empinfo.Ename = Empsal.Ename Empsal (Assuming natural join ie. drop one Ename column)
19
- Right Outer Join ( )
It retrieves all additional extra tuples from the right table (ie. second table of the join
operation)
Empinfo Empinfo.Ename = Empsal.Ename Empsal (Assuming natural join ie. drop one Ename column)
Empinfo Empinfo.Ename = Empsal.Ename Empsal (Assuming natural join ie. drop one Ename column)
iv. DIVISION ( ÷)
It retrieves all values of the attributes in the first table(ie attributes not present in second table) but
those values should be related to all values of the common attributes of the second table. If R1÷ R2 then it
retrieves the values of R1 , related to every value of R2, but displays only the attribute of R2.
T perform division operation, second table must be a proper subset of the first table.
20
Examples of Queries in Relational Algebra
Example 1:
Given the tables:
Sailor(Sid, Sname, Rating, Age)
Boat (Bid, Bname, Bcolor)
Reserve(Sid, Bid,Date)
1. Display the name of the sailors who have reserved boat with id number 105.
- ∏ Sname (Sailor * (бBid=105 (Reserve)))
2. Display the name of the sailors who have reserved a red color boat.
- ∏ Sname (Sailor *(Reserve * (бBcolor = ‘red’ (Boat))))
4. Find the name of the sailors who have reserved any boat.
- ∏ Sname (Sailor * Reserve )
5. Find the name of the sailors who have reserved both a red and a green color boat.
21
Example : 3
person (driver-id, name, address)
car (license, year, model)
accident (report-number, location, date)
owns (driver-id, license)
participated (report-number, driver-id, license, damage-amount)
employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)
manages (person-name, manager-name)
a. Find the names of all employees who work for First Bank Corporation.
- Π person-name (σcompany-name = “First Bank Corporation” (works))
b. Find the names and cities of residence of all employees who work for First Bank Corporation.
- Π person-name, city (employee ⋈ (σ company-name = “First Bank Corporation” (works)))
c. Find the names, street address, and cities of residence of all employees who work for First
Bank Corporation and earn more than $10,000 per annum.
- Π person-name, street, city (σ(company-name = “First Bank Corporation” ∧ salary > 10000) works⋈ employee)
d. Find the names of all employees in this database who live in the same city as the company for
which they work.
- Π person-name (employee ⋈ work ⋈ company)
e. Find the names of all employees in this database who do not work for First Bank Corporation.
- Π person-name (σ company-name ≠ “First Bank Corporation”(works))
22
EXTENDED RELATIONAL ALGEBRA OPERATIONS
The basic relational-algebra operations have been extended in several ways.
A simple extension is to allow arithmetic operations as part of projection.
An important extension is to allow aggregate operations such as computing the sum of the elements of a
set, or their average.
Another important extension is the outer-join operation, which allows relational-algebra expressions to
deal with null values, which model missing information.
Various Extended relational algebra operations are as follows:
Generalized Projection
Aggregate Functions
Outer Join
Null Values∗∗
Generalized Projection
The generalized-projection operation extends the projection operation by allowing arithmetic functions
to be used in the projection list.
The generalized projection operation has the form
Example
Query:For every customer of the bank, display the id ,name and how much more the person can spend.
Answer: Π CustomerID, Name,(Credit-limit – Credit-spent) as Credit-available (Credit-info)
Aggregate functions
Aggregate functions take a collection of values and return a single value as a result.
Example:
23
Avg
Count
Min
Max etc.
The collections on which aggregate functions operate can have multiple occurrences of a value; the
order in which the values appear is not relevant.
Such collections are called multisets.
Queries:
Display total credit spent by all customers of the bank.
The relational-algebra operation signifies that aggregation is to be applied, and its subscript specifies
the aggregate operation to be applied.
Display total credit spent by customers for every group of credit limit.
Display the total credit spent and maximum credit spent for every group of credit limit.
Outer Join
In join operations the tuples without a match and those having NULL value in the join attribute are eliminated.
Thus outer joins are used to retrieve additional tuples from one or both tables, even though they don’t satisfy
the join condition.
Since the special value null indicates “value unknown or nonexistent,” any arithmetic operations(such
as +,−,∗, /) involving null values must return a null result.
Any comparisons(such as <,<=,>,>=,=) involving a null value evaluate to special value unknown.
or : (true or unknown)=true;
(false or unknown)=unknown;
(unknown or unknown)=unknown.
select:
Otherwise, if the predicate returns unknown or false, t is not added to the result.
Join:
25
If two tuples from the participating tables have null value in common attribute, then the tuples
don’t match.
Project:
The projection operation treats nulls just like any other value when eliminating duplicates.
If two tuples in the projection result are exactly the same, and both have nulls in the same fields,
they are treated as duplicates.
union,intersection,difference:
These operations treat tuples that have the same values on all fields as duplicates even if some of
the fields have null values in both tuples.
Aggregate:
When nulls occur in aggregated attributes, the operation deletes null values at the outset, before
applying aggregation.
Outer join:
For tuples that do not occur in the join result may be added to the result padded with nulls.
26