UNIT-2 Introduction To The Relational Model

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

UNIT-2

Introduction to the Relational Model


Codd proposed the relational data model in 1970.At that time, most database
systems were based on one or two older data models (the hierarchical model and the network
model), the relational model revolutionized the database field and largely supplanted
(replaced with) these earlier models. Today, the relational model is by far the dominant data
model and the foundation for the leading DBMS products, including IBM’s DB2 family,
Informix, Oracle, Sybase, Microsoft’s Access, SQL server, Foxbase and Paradox. Relational
database systems are ubiquitous (very common) in the market place and represent a
multibillion dollar industry.
The relational model is very simple and elegant. A database is a collection of
one or more relations, where each relation is a table with rows and columns. This simple
tabular representation enables even novice users to understand the contents of database and it
permits the use of simple, high level language to query the data.
Relation Cardinality:
The relation cardinality is the number of tuples (rows or records) in the relation.
Relation Degree:
The relation degree is the number of fields (column or attributes) in the
relation.
Tuples/Records:
The rows of the table are also known as records or tuples.
Fields/Attributes:
The columns of the table are also known as fields or attributes.

Integrity Constraints over Relations:


Integrity Constraints is a condition that ensures the correct insertion of the data and
prevents unauthorized data access thereby preserving the consistency of the data.
For example, the roll number of a student cannot be a decimal value. The
database enforces the constraint that the instance of roll number can have only integer
values.
Integrity Constraints can be enforced in the following cases,
1) As soon as the database schema is defined, the associated integrity constraints
must be specified.
2) Validity of the changes made to the data must be checked while executing
theapplication. Hence, the time of ICs check, the changing statement and the
associated transaction must be necessarily specified.
Integrity Constraints

Key Foreign General


Constraints Constraints Constraints
Key constraints:
Key constraints are used in relations to uniquely identify some records of the relation. A
key constraint can be defined as a statement that consists of minimal subset of attribute that
uniquely determine a record in a table. This set of attributes that work in accordance with the
key constraint is called as candidate key. A candidate key is a collection of fields
/columns/attributes that uniquely identifies a tuple. For example, in “customer” relation the
attribute “cid” is a key, it uniquely defines a tuple in a relation. No two rows in a relation
“customer” can have the same “cid” value. The set of attributes that form a candidate key
need not be all keys. The attributes may be treated as candidates to be taken as key. For
example, the set (cid, cname) is a candidate key which means either cid or cname can be
taken as key but not both. Each of them independently and uniquely identifies a particular
row. The alternate keys are the candidate keys that are not taken as keys.
Composite key:
Composite key consists of more than one attribute that uniquely identifies a tuple
in a relation. All the attributes that form a set of keys and all of them taken together
determines a unique row in a table. For example, the each (cid, accno) is a composite key
which maintains the uniqueness of each row. Both cid, accno are taken as keys.
Super key:
A super key is a combination of both candidate key and composite key. That is a super
key is a set of attributes or a single attribute that uniquely identifies a tuple in a relation. For
example, consider the super key,
{cid, acnes, cane}
Here, all the three attributes taken together can identify a particular record or any
one of the attribute can attribute can identify a particular record.
Primary key:
Only a single attribute can uniquely identify a particular record. More specifically, it
can be defined as the candidate key which has been selected as key to identify unique
records. For example, “cid” attribute in “customer” relation can be treated as a primary key.
Summary of key (With Respect to “customers” Relation)
- Super key{cid, cname, ancon}
- Candidate key{cid, name}
- Composite key{cid, accno}
- Primary key{cid}

Querying Relational Data:


Query is a question. A query is formulated for a relation/table to retrieve some useful
information from the table. Different query languages are used to frame queries. The most
popular being SQL (Structured Query Language).
Example of SQL queries:
This section deals with different SQL queries which helps to retrieve data from the
table. Consider the employee relation discussed in table 3.2.1 and also department relation.
1) List the employee who are working for department number 7.
Select *
From employee
Where dno = 7;
This statement will list complete information about the employee who are working
for the department number 7. ‘*’ indicates all the fields.
“Select” can be read as “select all fields” from employee (table) where (condition). The
general format is,
Select field1, field2, field3,.......
From tablename1, tablename2,......
Where (condition);
Rows are selected based on the evaluation of condition, which is all the rows which
satisfies the given condition are selected. The result of this query is also a relation as shown
ename Eid Bdate address dno age Phone
number
John 12345261 10-8-1965 New 7 41 773213218
Jersey
Jack 12345262 12-5-1955 Chicago 7 51 773313218
Green 12345263 20-11- New York 7 34 773421317
1973

2) List the name, id, department name and phone number of the employees who are the
managers of the respective deportments.

Select ename, eid, dname, phone number, dno


From employee E, deportment D
Where E.dno=D.dno and E.eid=D.manager_id;
This query combined two relations based on some conditions and displays the
results not including all the fields but only the one which are listed after “select”
keyword.
Database languages:
A DBMS provides two types of languages,
1) Data Definition Language-to define the data in the database.
2) Data Management Language-to manipulate the data in the database.
DDL (Data Definition Language)
The language used to define the data in the database is called as Data definition Language
(DDL). In simple words, data definition language is used to create the database, alter the
database and delete the database. The result of compilation of data definition languages
statements is a set of tables that is stored in a special file called data dictionary or data
directory.
A data dictionary is a file that contains metadata that is, data about data. This file is
consulted before actual data is read or modified in the database.
This DDL is used by the conceptual schema to store (define) or retrieve (query)
the records to/ from the database respectively, where these records describes everything i.e.
entities, attributes and relationships.
There is yet another data definition languages, the internal DDL are also known as data
storage and definition language used by internal schema which describes how data is actually
stored. The result of compilation of the statements of this language specifies the
implementation details of the data base schemas details are usually hidden from the users.
DDL commands are create, alter and drop only.
DML (Data manipulation language)
The language used to manipulate the data in database is called data manipulation
language (DML). In simple words, data manipulation language is used to retrieve the data
from the database, insertion of new data into the database and deletion or modification of
existing data.
The operation of retrieving the data from the database using DML is called as a query.
Simply we can say that, a query is a statement in the DML the requests the retrieval of data
from the database. The portion of DML used to pose a query is known as query language. We
can use the terms query language and data manipulation language synonymously. The query
language contains commands to select and retrieve the data from the database. Comments are
also provided to insert, update and delete the records.
Even, the predefined data manipulation functions are provided by the DBMS which can
be called into four programs directly by their names i.e. procedures calls.
A DML manipulates the data basically in two types,
1. Procedural DMLs
2. Non-procedural DMLs
 Procedural DMLs makes a user to specify two things , one is what data
is to be retrieved and the second is to tell the procedure how to get
those data.
 Non-procedural DMLs makes a user to specify only the thing , that is,
what data is to be retrieved i.e. without telling the procedure of how to
get that data.
 Non-procedural DMLs are easier to learn and use the procedural
DMLs, but procedural DMLs are more efficient than non-procedural
DMLs because it specifies the procedure to retrieve the data.
Introduction to Views
Views are derived or virtual tables, they does not exit and derive their data
from the original relation. Views allow the user to see only the part of data but not all data.
The user can think of views as any other normal relation and can perform all the operations
that can be performed on relations. Views are dynamic in nature i.e., the
changes/modifications made to the views are reflected back to the original table and vice
versa.
Creation of Views: The following SQL statements are used to create a view,
Create view (view name) (field1, field2 ... field n)
as select from (attribute1, attribute2 ... attribute n) from table1, table2,...table n
where <condition>;
This is the original format for defining a view.
Example: Let us suppose, we want to create a view for employee table. This view gives the
information about the project located at New Jersy.
Create view project_info (name, id, dno, pnumber, pnme) as select E.ename, e.eid,
e.dno, p.pno, P.pname from employee E, project P where E.eid = P.pid and P.location =
‘New Jersy’;
The view project _ info contains the following fields, name (of employee who are
working on project), id, dno, pnumber, pname.
View project _ info of employees
Id Name Dno Pnumber Pname
12345263 Green 7 51 Soft drinks
12345262 Jack 7 71 Product B

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.

Introduction to Query Languages


A query language is a language in which user requests to retrieve some information
from the database. The query languages are considered as higher-level languages than
programming languages.
Query languages are of two types,
1) Procedural Language
2) Non-procedural Languages

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.

The Intersection ( ) operation


The intersection operation, denoted by ᴖ, is related algebra operation that finds that
are in both relations. The results of this operation, denoted by d ᴖ b, is a relation that includes
all tuples common in both depositor and borrower relations.
Example query: Find the customer_id of all customers in the bank who have an account but
not a loan.
πcustomer_id(depositor)ᴖπcustomer_id(borrower)
The resulting relation of this query, lists all common customer ids of customers who have
both an account and a loan.
Therefore a difference operation d – b to be valid we require that two conditions to be
satisfied as was with the case of union operation started above.
The Set-Difference (-) Operation
The set-difference operation, denoted by-, is a relational algebra operation that
finds tuples that are in one relation but are not in another. The result of this operation,
denoted by d – b, is a relation that includes all tuples in depositor relation but not in borrower
relation.
Example query: Find the customer_id of all customers in the bank who have an account but
not a loan.
πcustomer_id(depositor)-πcustomer_id(borrower)
The result relation for this query, lists the customers ids of all customers who have
an account but not a loan.
Therefore a difference operation d – b to be valid we require that two conditions to
be satisfied as was with the case of union operation stated above.
The Cross-Product (or) Cartesian product (×) Operation
The Cartesian-product operation, denoted by a cross (×) is a relational algebra
operation which allows us to combine information from two relations into one relation.
Assume that there are n1 tuples in borrower relation and n2 tuples in loan relation. Then, the
result of this operation, denoted by r = borrower × loan, is a relation ‘r’ that includes all the
tuples formed by each possible pair of tuples one from the borrower relation and one from the
loan relation. Thus, ‘r’ is a large relation containing n1 * n2 tuples.
But, the drawback is if we have same name attribute,
Example: loan_number in both relations i.e., borrower (customer_id, loan_number ) and loan
( loan_number, amount ) then in the resulting relation ‘r’ ( customer_id, loan_number,
loan_number, amount), we cannot distinguish between loan_number of borrower and loan.
That is, whether which loan_number is from where, whether which loan_number is from
where, whether borrower or loan.
The solution is we use a naming schema to distinguish between these attributes. We
do so here by attaching an attribute the name of the relation from which the attribute
originally came. We can then write the relation schema for r as (customer_id, borrower.
loan_number, loan_number, amount). Now, we can easily distinguish between loan numbers
of borrower and loan relations.
Example query: Find customer_id of all customers who have loan>10000.
πcustomer_id (ϭ borrower.loan_number = loan.loan_number ( ϭ amount>10000 ( borrower × loan)))
We can get customer_id from borrower relation and amount from loan relation.
So, first we find Cartesian-product of borrower × loan, so that the new relation
contains both customer_id, amount, with each combination.
Now, we select the amount, by ϭ amount>10000
So, if any customer have taken the loan, then their borrower.loan_number =
loan.loan_number should be selected as their entries of loan_number matches in both
relation.
Finally, since we want only customer_id, we do a projection,
πcustomer_id ( ϭ borrower.loan_number = loan.loan_number (ϭ amount>10000 (borrower × loan )))
Renaming (ρ)
The rename operation, denoted by rho (ρ), is a relational algebra operation, which is used to
give the new names to the relation algebra expressions. Thus, we can apply the rename
operation to a relation ‘borrower’ to get the same relation under a new name. Given a relation
‘customer’, then the expression, ρ ͓(customer)
returns the same relation ‘customer’ under a new name ‘x’.
Now there are two relations, one with customer name and second with ‘x’ name.
The rename operation is useful the largest when we want to compare the values
among same column attribute in a relation.
For example: If we want to find the largest account balance in the bank. Then we have to
compare the values among same column (balance) with each other in a same relation account,
which is not possible. So, we rename the relation with a new name‘d’. Now, we have two
relations of account, one with account name and second with‘d’ name. Now we can compare
the balance attribute values with each other in separate relations.
Example query: Find the largest account balance in the bank.
π account. Balance (ϭ account. balance<d. balance (account × ρԀ (account)))
Note: The rename operator ρ can also be represented as ρ(R(Ḟ),E) where E is an arbitrary
relational expression, R is an instance of a new relation Ḟ is the renaming list/rename fields
(one among R and Ḟ may be optional sometimes but not both).
Joins
The join operation, denoted by “join” “ “, is a relational algebra operation, which is used
to combine (join) two relations like Cartesian-product but finally removes duplicate attributes
(same column to only one-column) and makes the operations (selection, projection etc.,) very
simple. In simple words, we can say that join connects relations on columns containing
comparable information.
There are three types of joins,
1. Natural join
2. Outer join
3. Theta join (or) conditional join
Natural join (or) Equi-join
The natural join is a binary operation that allows us to combine two different relations
into one relation and makes the same column in two different relations into only one-column
in the resulting relation. Suppose we have relations with following schemas, which contain
data on full-time employees.
Employee (employee_name, street, city) and
Employee_works (employee_name, branch_name, salary)
The relations are,
Table : Employee Relation
Employee_name Street City
Coyote Town Hollywood
Rabbit Tunnel Carrotville
Smith Revolver Valley
Williams Seaview Seattle

Table : Employee_works Relation


Employee_name Branch_name Salary
Coyote Mesa 1500
Rabbit Mesa 1300
Gates Redmond 5300
Williams Redmond 1500

Now, if we want to generate a single relation with all the information


(employee_name, street, city, branch_name and salary) about full-time employees. Then, a
possible approach would be to use the natural-join operation as follows,
Employee employee_works
The result of this expression is the relation,
Table: Results of Natural_Join
Employee_name Street City Branch_name Salary
Coyote Town Hollywood Mesa 1500
Rabbit Tunnel Carrotville Mesa 1300
Williams Seaview Seattle Redmond 1500

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,

Smith, Revolver, Valley, null, null


Gates, null, null, Redmond, 5300
Theta Join (or) Condition Join
The theta join operation, denoted by symbol, θ is an extension to the natural join
operation that combines two relations into one relation with a selection condition (θ).
The theta join operation is expressed as employee salary<1400 employee_works
and the resulting relation is as follows,
Employee salary<1400 employee_works
Table : Result of Theta Join
Employee_name Street City Branch_name Salary
Rabbit Tunnel Carrotville Mesa 1300

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

The Assignment (←) Operation


The assignment operation, denoted by ← works in a manner similar to assignment in
a programming language. For example, we could write a ÷ b as,
temp1 ← πA-B (a)
Temp2 ← πA-B ((temp1×b) – πA-B,B(a))
Result = temp1 – temp2
The evaluation of an assignment does not result in any relation. Rather, the result of
expression to the right of ← is assigned to the relation variable on the left of ←. This relation
variable may be used in subsequent expressions.
With the assignment operation, a query can be written as sequential program
consisting of series of assignments followed by an expression whose value is displayed as the
result.
Examples of Relational Algebra Queries
For the following questions consider the instance S3 of sailors, R2 of reserves and B1
of boats, shown in tables respectively.

Table: An instance S3 of sailors

Sid Sname rating age


22 Dustin 7 45.0
29 Brutus 1 33.0
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
64 Horatio 7 35.0
71 Zorba 10 16.0
74 Horatio 9 35.0
85 Art 3 25.5
95 Bob 3 63.5

Table: An instance R2 of reserves

Sid Bid Day


22 101 10/10/98
22 102 10/10/98
22 103 10/8/98
22 104 10/7/98
31 102 11/10/98
31 103 11/6/98
31 104 11/12/98
64 101 9/5/98
64 102 9/8/98
74 103 9/8/98

Table: An Instance B1 of Boats

Bid bname Color


101 Interlake Blue
102 Interlake red
103 Clipper Green
104 Marine red

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

Sid Bid Day


22 103 10/08/98
31 103 11/06/98
74 103 09/08/98

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)

Ex.3) Find the colours of boats reserved by lubber.


Ans: π colour ((S sname=’lubber’ sailors) reserves boats)
This query is very similar to the query we used to compute sailors who reserved red boats. On
instance B1, R2 and S3, Lubber has reserved a green boat and a red boat.
Ex.4) Find the names of sailors who have reserved at least one boat.
Ans: π sname (sailors reserves)
The join of sailors and reserves creates an intermediate relation in which tuples
consists of a tuple attached to a reserves tuple. A sailor tuple appears in (some tuple of) this
intermediate relation only if at least one reserves tuple has the same Sid value, that is, the
sailor has made some reservation. The answer, when evaluated on the instances B1, R2 and
S3, contains the three tuples <Dustin>, <Horatio>, and <Lubber>. Even though there are two
sailors called Horatio who have reserved a boat, the answer contains only one copy of the
tuple <Horatio>, because the answer is a relation, i.e., a set of tuples, without any duplicates.
Ex. 5) Find the names of sailors who have reserved a red or a green boat.
Ans: ρ (tempboats, (S colour=’red’ boats) ᴗ (S colour=’green’ boats))
πsname (tempboats reverse sailors)
We identify the set of all boats that are either red or green (tempboats, which contains
boats with the bids 102,103 and 104 on instances B1, B2 and S3). Then we join with reserves
to identify sids of sailors who have reserved one of these boats, this gives us sids 22, 31, 64
and74 over our examples instances. Finally, we join (an intermediate relation containing this
set of sids) with sailors to find the names of sailors with these sids. This gives us the names
Dustin, Horatio and Lubber on the instances B1, R2 and S3. Another equivalent definition is
the following.

ρ (tempboats, (scolor=’red’ v color=’green’ boats)


ρsname(tempboats reserved sailors)
Ex. 6) Find the names of sailors who have reserved atleast two boats.
Ans. ρ (reservations, ρsid,sname,bid(sailors reserves))
ρ ( reservationpairs(1sid1,2sname1,3bid1,4sid2,5sname2,6bid2)reservations*
reservations),
πsname(s (sid1=sid2)^(bid1≠bid2)(reservationpairs)
First, we compute tuples of the form (sid,sname,bid), where sailor sid has made a
reservation for boat bid. This set of tuples is the temporary relation reservations. Next we find
all pairs of reservations of tuples where the same sailor has made both reservations and the
boats involved are distinct. Here is the central idea, in order to show that a sailor has reserved
two boats, we must find two reservations tuples involving the sailor but distinct boats. Over
instances B1,R2 and S3. Each of the sailors with sids 23, 31 and 64 have reserved atleast two
boats. Finally, we project the names of such sailors to obtain the answer, containing the
names Dustin, Horotio and Lubber.
Ex: 7).Find the sids of sailors with age over 20 who have not reserved a red boat.
Ans.Π sid (ϭage >20 sailors)-
Π sid((ϭcolor=red boats)reservessailors)
This query illustrates the use of the set-difference operator.we use the fact that
sid is the key for sailors. We first identify sailors aged over 20(over instances B1, R2 and S3,
sids 22,29,31,32,58,64,74,85 and 95)and then discard those who have reserved a red boat(sids
22,31and 64), to obtain the answer (sids 29,32,58,74,85 and 95 ).if we want to compute the
names of such sailors, we must first compute their sids and then join with sailors and project
the same values.
Ex: 8) Find the names of sailors who have reserved all boats.
The use of the word all (or every) is a good indication that the division operation might
be applicable,
ρ (tempsids, (πsid,bidreserves)/(πbidboats))
πsname(tempsids sailors)
The intermediate relation tempsids is defined using division and computes the set of sids of
sailors who have reserved every boat (over instances B1, R2 and S3, this is just sid 22). We
define the two relations that the division operator (/) is applied to the first relation has the
schema (sid/ bid) and the second has the schema (sid, bid) and the second has the schema
(bid). Division then returns all sids such that there is a tuple <sid, bid>, in the first relation for
each bid in the second. Joining tempsids with sailors is necessary to associate nanes with the
selected sids, for sailor 22, the name is Dustin.
Ex: 9) Find the names of sailors who have reserved all boats called Interlake.
ρ(tempsids, (ρsid,bidreserves)/(ρbid(sbname=’interlake’boats)))
πsname(tempsids sailors)
The only difference with respect to the previous query is that now we apply a selection of
boats, to ensure that we complete only bids of boats named Interlake in defining the second
argument to the division operator. Over instances B1, R2 and S3, tempsids evaluates to sids
22 and 64 and the answer contain their names, Dustin and Horatio.

You might also like