UNIT-2 Introduction To The Relational Model
UNIT-2 Introduction To The Relational Model
UNIT-2 Introduction To The Relational Model
2) List the name, id, department name and phone number of the employees who are the
managers of the respective deportments.
Advantages of views:
Views provide data security as views contains only a part of data, the data of the
base table is protected from the unauthorized users as they can access only the part
of data not all the data.
Different users can view same data from different perspective in different ways at
the same time.
Views can be used to hide complex queries such as the queries which involve
“joins” operation. The user can issue the simple queries for the views and RDBMS
will take care of the complicated process.
Views can also be used to include extra/additional information.
Updation on Views
Whenever updations are performed on views, the changes must reflect back to the
base relations. Similarly, when changes are made to base relation, then these changes must
reflect in all the views which are derived from this base relation.
But all the updations cannot be applied on views. The updations can be carried out
only on the update table views. An update table view is one which is derived from single base
relations and if the following are true for the update table view,
No column should be derived by using as aggregate function.
No column of the view should be derived from the expression if the expression
involves scalar operator or scalar functions. On such views, insert and update
operations are not allowed.
Definition of view should not involve distinct keyword.
Definition of view should not include group by or having clause.
Definition of view should not include a sub query that involves the base relations.
Check Option
We can insert a row in employee, we can insert null values for the fields that are not
present in emp. Similarly, we can delete a row from employee by deleting it from employee
relation. With check option at the end of view definition SQL provides a way to user to insert
correct data so that this inserted or updated data does we can insert a row in emp by inserting
it in employee but what if the row does not satisfy the given condition, the check option is
used to check these conditions.
Destroying / Altering Tables and Views
Destroying or deleting a table can be done with the help of drop command. The
syntax for drop command is,
Drop table table_name restrict / cascade
The keywords restrict or cascade must always be used with drop command.
Drop table department restrict
This command deletes the department table if some view or IC (Integrity Constraints)
is referring to it otherwise, this command fails.
Drop table department cascade
In this case all views and ICs referring the relation ‘department’ are deleted along with the
table.
Similarly, a view can also be deleted with the help of drop command,
Drop view view _ name
- Alter table command is used to modify the structure of the table or view.
In procedural language, the user has to describe (show) the specific procedure (the
way) to retrieve the information from the database.
Example: The relational algebra is a procedural language.
In non-procedural languages, the user retrieves the information from the
database without describing the specific procedure to retrieve it.
Example: The Tuple Relation Calculus and the Domain Relation Calculus are non-
procedural languages.
Relational Algebra
The relational algebra is a procedural query language. It consists of a set of operations
that take one or two relations (tables) as input and produce a new relation, on the request of
the user to retrieve the specific information, as the output (result).
The relational algebra contains the following operations,
1. Selection
2. Projection
3. Union
4. Rename
5. Difference
6. Cartesian product
7. Intersection
8. Join
9. Divide
10. Assignment
The selection, projection and rename operations are called unary operations because
only on one relation.
The other operations operate on pairs of relation and are therefore called binary
operations.
Selection and Projection
The Selection (ϭ) Operation
The relation is a relational algebra operation that uses a condition to select rows from a
relation. A new relation (as a result) is created from another existing relation by selecting
only requested by the user that satisfies a specified condition.
We use the lower case Greek letter (ϭ) to denote selection.
General syntax of Selection Operation
Selection condition (relation_name)
Example Query: Find the customer details who are living in Hyderabad city from
customer relation.
Ϭ city= ‘Hyderabad’ (customer)
The selection operation uses the column names in specifying the selection condition.
Actually, selection conditions are same as the conditions used in if statements of
programming languages, selection condition uses the relational operations <, >, <=, >=
and ≠. Furthermore, we can combine several conditions into a large condition using the
logical connectives ‘and’ represented by ^, ‘or’ represented by ᵛ.
Example Query: Find the customer details who are living in Hyderabad city and whose
customer_id is greater than 1000 in customer relation.
Ϭ city= ‘Hyderabad’ ^ customer_id >1000 (customer)
The Projection (π) Operation
The projection is a relational algebra operation that creates a new relation by
deleting columns from an existing relation i.e., a new relation (as a result) is created from
another existing relation by selecting only those columns requested by the user from
projection and is denoted by letter pi (π).
The selection operation eliminates unwanted rows whereas the projection
operation eliminates unwanted columns. In simple words, the projection operation
extracts specified columns from a table.
Example Query: Find the customer names (not customer details) who are living in
Hyderabad city from customer relation.
π customer_name (ϭcity= ‘Hyderabad’ (customer))
In the above example, the selection operation is performed first. Next, the projection
of the resulting relation on the customer_name column is carried out. Thus, instead of all
customer details of customers living in Hyderabad city, we can display only the
customer’s names of customers living in Hyderabad city.
The above example is also known as relational algebra expression because we
are composing (combining) two or more relational algebra operations (i.e., selection and
projection) into one at the same time.
Example Query: Find all customer names of the customer relation,
π customer_name (customer)
Thus, this example query lists all customer names in the customer relation and
this is not called as relational algebra expression because it is performing only one
relation algebra operation.
SET OPERATIONS
The Union (ᴗ) Operation
The union denoted by ᴗ, is a relational algebra operation that creates a union
or combination of two relations. The result of this operation, denoted by d ᴗ b is a relation
that includes all tuples that all are either in d or in b or in both d and b, where duplicate
tuples are eliminated.
Example Query: Find the customer_id of all customers in the bank who have either an
account (depositor) or a loan (borrower) or both.
πcustomer_id (depositor) ᴗ π customer_id (borrower)
Explanation: To solve the above query, first, we have to find all customers with an
account in the bank.
πcustomer_id (depositor)
Then, we have to find all customers with a loan in the bank,
π customer_id (borrower)
Now, to answer the above query, we need the union of these two sets, that is,
we need all customer names that appear in either or both of the two relations by,
πcustomer_id (depositor) ᴗ π customer_id (borrower)
If some customers A, B and C are both depositors as well as borrowers, then
in the resulting relation, their customer ids will occur only once because duplicate values
are eliminated.
Therefore, for a union operator d ᴗ b to be valid, we require that two
conditions to be satisfied,
1) The relation depositor and borrower must have same number of attributes (columns).
2) The domains of i th attribute of depositor relation and i th attribute of borrower
relation must be the same, for all i.
Notice that we have lost street and city information about Smith, since tuples
describing Smith is absent in employee_words similarly, we have lost branch_name and
salary information about Gates, since the tuple describing Gates is absent from the employee
relation.
Now, we can easily select are project query on new join relation.
Example query: Find the employee names and city who have salary details.
π employee_name, salary, city ( employee employee_works)
The join operation selects all employees with salary details, from where we can easily project
the employee names, cities and salaries.
Natural join operation results in some loss of information.
Outer-join
The drawback of natural join operation is some loss of information, to overcome this;
we use outer-join operation.
The outer-join operations are of three types,
Left Outer-join
Right outer-join
Full outer-join
1. Left Outer-join: The left outer-join takes all tuples in left relation that did not match
with any tuples in right relation, adds the tuples with null values for all other columns
from right relation and adds them to the result of natural join in the table as follows,
Smith, Revolver, Valley, Null, null
2. Right Outer-join: The right outer-join operation takes all the tuples in the right
relation that did not match with any tuple in left relation, adds the tuples with null
values for all other columns from left relation and adds to the result of natural join in
the table as follows,
Gates, null, null, Redmond, 5300
3. Full Outer-join: The full outer-join operation does both of these operations, by adding
tuples from left relation that did not match any tuples from the right relation, as well
as add tuples from the right relation that did not match any tuple from the left relation
and adding them to the result of natural join in the table as follows,
There is only tuple selected because his salary less than 1400 (salary<1400).
Division (÷)
The division operation, denoted by ÷, is a relational algebra operation that creates a
new relation by selecting the rows in one relation that does not match rows in another
relation.
Let relation A is (X1, X2... Xm, Y1, Y2... Ym) and
Relation B is (Y1, Y2...Yn),
Where Y1, Y2...Yn, tuples are common to the both relations A and B with same domain
compulsory………..
Then A ÷ B = new relation with (X1, X2...Xm) tuples.
Relation A and B represent the dividend and divisor respectively.
A tuple ‘t’ is in a ÷ b if and only if two conditions are satisfied,
1) T is in πA-B(r)
2) For every tuple tb in B, there is a tuple ta in A satisfying the following two things,
i. Ta[B] = tb [B]
ii. Ta [A-B] = t
Examples
Ex. 1) Find the name of sailors who have reserved boat 103.
Ans: This query can be written as follows,
π sname ((ϭ bid=103 reserves) sailors)
We first compute the set of tuples in reserves with bid = 103 and then take the natural
join of this set with sailors. This expression can be evaluated on instances of reserves and
sailors. Evaluated on the instances R2 and S3, it yields a relation that contains just one yield,
called sname and three tuples <Dustin>, <Horatio> and <Lubber>. (Observe that there are
two sailors called Horatio, and only one of them has reserved a red boat).
We can break this query into smaller pieces using the remaining operator ρ,
ρ (temp, sbid= 103 reserves) ρ (temp2, temp1 sailors)
ρsname (temp2)
Because we are only using ρ to give names to intermediate relations, the remaining
list is optional and is omitted. Temp1 denotes an intermediate relation that identifies
reservations of boat 103. Temp2 is another intermediate relation and it denotes sailors and
who have made a reservation in the set temp1. The instances of these relations when
evaluating this query on the instances R2 and S3 are illustrated in tables .
Finally, we extract the same column from temp2.
Instance of temp1
Instance of temp2
Sid Sname rating age Bid day
22 Dustin 7 45.0 103 10/8/98
31 Lubber 8 55.5 103 11/6/98
74 Horatio 9 35.0 103 9/8/98
Ex. 2) Find the names of sailors who have reserved a red boat.
Ans: π sname ((ϭ colour=’red’ boats) reserves sailors)
This query involves a series of two joins. First we choose (tuple describing) red boats. Then,
we join this set with reserves (natural join, with equality specified on the bid column) to
identify reservations of red boats. Next, we join the resulting intermediate relation with
sailors (natural join, with equality specified on the Sid column) to retrieve the names of
sailors who have made reservations of red boats. Finally, we project the sailor’s names. The
answer, when evaluated on the instances B1, R2 and S3, contains the sane Dustin, Horatio
and Lubber.
An equivalent expression is,
πsname (ρ Sid((ρbid s colour =’red’ ‘boats’) reserves) sailors)