2 Relational Algebra SQL
2 Relational Algebra SQL
Definition of Relation :
The mathematical concept underlying the relational model is the set-theoretic relation,
which is a subset of the Cartesian Product (CP) of a list of domains. A domain is simply
a set of values.
A relation is any subset of the CP of one or more domains. In the context of DBMS, it is
pointless to discuss '‘infinite relation" rather the assumption is “all relations are finite”.
Terminology :
Tuple : The members of a relation are called tuples.
Arity : Each relation that is a subset of D1 X D2 X, ….XDk is said to have
arity k; another term is degree.
A tuple (v1, v2, ….,vk) has k components i.e. the ith component is vi.
We can view a relation as a table, where each row is a tuple and each
column corresponds to one component. The columns are often given names,
called attributes. The set of attribute names for a relation is called the
relation scheme.
Example : A relation whose attributes are CITY, STATE and POP. The arity
of the relation is 3. Here (Calcutta, W.B., 100160).
The relation scheme for this relation may be CITYINFO (CITY, STATE,
POP).
Relational Algebra
Relational algebra(RA) is required to the design of languages for expressing
queries about relations. The aspects of data manipulation language are query
answering, insertion, deletion and modification of tuples.
Query languages for the relational model broadly classified into two classes.
R S
1. UNION
The union of relations R and S, denoted by R S , is the set of tuples those are in R or
S or both. We only apply the union operator to relations of the same arity, so all tuples in
the result have the same number of components.
2. DIFFERENCE
The difference of relations R and S denoted by R - S, is the set of tuples in R but not in S.
We again require that R and S have the same arity.
------------------------------------
a b c
c b d
-------------------------------------
R -S
3. CARTESIAN PRODUCT
Let R and S be relations of arity k1 and k2 respectively. The R X S, cartesian product of
R and S, is the set of (k1+k2)-tuples whose first k1 components come from a tuple in R
and whose last k2 components come from tuple in S.
A B C D E F
----------------------------------------
a b c b g a
a b c d a f
d a f b g a
d a f d a f
c b d b g a
c b d d a f
------------------------------------------
R X S
4. PROJECTION
If R is a relation of arity k, we let i1,i2,……im ( R) where the ij’s are distinct integers in
the range 1 to k, denote the projection of R onto components i1,i2,………,im.
A C
--------------------------
a c
d f
c d
-------------------------------
A,C ( R)
5. SELECTION
This operation is used to select a subset of the tuples in a relation that satisfy a selection
condition.
A B C
--------------------------------
a b c
c b d
--------------------------------
B=b ( R )
RXS
There are a number of useful operations that can be expressed in terms of the five
previously mentioned operations which are known as primitive operations.
1. INTERSECTION
Tuples present in R and S both termed as R S . This may be derived as R - ( R - S)
------------------------------
d a f
------------------------------
RS
2. QUOTIENT
4. NATURAL JOIN
The natural join written as R ⋈ S is applicable only when both R and S have columns that
are named by attributes.
i) Compute R X S
ii) For each attribute A that names both a column in R and a column in S, select those
tuples from R X S whose values agree in the columns for R.A and S.A.
iii) For each attribute A above, project out the column S.A.
Some common database requests cannot be performed with the standard RA operations
described above. Most commercial query languages for RDBMS include capabilities to
perform these requests. These operations enhance the expressive power of the RA.
1. AGGREGATE FUNCTIONS
The first type of request that can’t be expressed in RA is to specify mathematical
aggregate functions on collections of values from the database.
They are SUM, MAXIMUM, MINIMUM, AVERAGE, COUNT.
Let us consider a table named as EMPLOYEE as follows :
--------------------------------------------------------------------------------------
NAME DNO SSN SEX SALARY
---------------------------------------------------------------------------------------
J. Saha 5 123456789 M 30000
F. Wadekar 5 333445555 M 40000
A. Zardari 4 999887777 F 45000
J. Wong 4 987654321 F 43000
R. Nath 5 666884444 M 38000
J. Epal 5 453453453 F 50000
A. Josh 4 987987987 M 25000
J. Bose 1 888665555 M 50000
----------------------------------------------------------------------------------------
EMPLOYEE
Example :
The following are three different solution to retrieve each department number, the number
of employees in the department and their average salary.
---------------------------------------------------------------------------------
DNO NO_OF_EMP AV_SAL
---------------------------------------------------------------------------------
5 4 39500
4 3 37667
1 1 50000
----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
COUNT_SSN AVERAGE_SALARY
--------------------------------------------------------------------------------
8 40125
--------------------------------------------------------------------------------
Example :
There is a STS database as follows :
TEACHER (tname, emp-no, dept)
SUBJECT (sub-no, sub-title, credit)
STUDENT(sname, roll-no, hostel)
TAUGHTBY(emp-no, sub-no)
TAKENBY(sub-no, roll-no, status, marks)
Q1 :
Find the department to which Prof. “XYZ” belongs.
dept (σ tname = ‘XYZ’ ( TEACHER))
⨝
Q2 :
Find the name of the teacher who teaches the subject DBMS.
Q4 :
Find the name of students who study ‘DBMS ‘ but not ‘OPERATING SYSTEM’.
Q5 :
Find the name of students who study DBMS and SOFTWARE ENGG.
Q1 :
Find all customers who have a loan for an amount greater than Rs. 10,000/-.
Find all the customers having a loan or an account or both at ‘BEC’ branch.
Q3 :
Find all the customers who have an account at the ‘BEC’ branch but do not have a loan
from that branch.
Q4 :
Find all the customers who have an account at all branches located at Howrah.
SQL
Relational Algebra is rarely used as the query language in real world. SQL is most
commonly used commercial query language. It has been standarised many times. It is a
declarative, query language.
Simple SQL Query Syntax
SELECT COLUMNS
FROM TABLES
WHERE PREDICATE
|
SELECT A1, A2, ….., An
FROM r1, r2, … , rm
WHERE P
The above is nearly equivalent to
A1, A2, … , An ( s P ( r1 x r2 x … x rm))
Here duplicate will be eliminated but in the above SQL query, duplicate will not be
eliminated.
SQL Components
i) create
Q1:
Create a table having three attributes
create table course (
cno integer ,
cname varchar(20),
prereq integer );
Q2 :
Create a table having attributes with constraints.
create table course (
cno integer primary key,
cname varchar(20) not null,
prereq integer);
Q3:
Create a domain.
create domain name_of_course varchar(30);
Now create a table where an attribute takes value from the defined domain.
create table course (
cno integer default 0 not null,
cname name_of_course,
prereq integer);
Q2 :
List the course names with their prerequisite course names
select C.cname, P.cname
from COURSE C, COURSE P
where C.prereq = P.cno ;
SET Functions
i) COUNT
ii) MAX
iii) MIN
iv) AVG
v) SUM
Q3:
Display number of ‘CST’ department faculty.
Q5:
Give oldest faculty member’s age
select max (age)
from FACULTY;
Q6:
Display age of youngest and oldest faculty
select max (age), min (age)
from FACULTY;
Q7:
Display sum of the salaries of the faculty
select sum (sal)
from FACULTY;
Q8:
Find out average age of ‘IT’ dept faculty
select avg (age)
from FACULTY, DEPT
where DEPT. dno = FACULTY.dno
and DEPT.dname = ‘IT’;
Q9:
Print name and age of oldest faculty
select name, max (age)
from FACULTY;
The above query is syntactically incorrect
The correct syntax is as follows :
Step 2 : Divide the resulting table of Step 1 into minimum groups such that within a
group all rows have same value for column(s) on which grouping is specified.
Step 3 : Eliminate groups of Step 2 that do not satisfy the having clause.
Step 4 : The result table has one tuple per group and the attributes correspond to select
list.
Q1 :
Find out year and maximum grade obtained by a student of that year.
select year, max (grade)
from STUDENT
group by year;
Q2 :
Give average salary of faculty within a department provided the dept has at least 5
faculty.
select dno, avg ( sal)
from FACULTY
group by dno
having count > = 5;
The concept of ‘views’ provides a means for a user to design a ‘personalised’ model of
the database. A view can hide data that a user does not need to see. Security is provided
if there is a mechanism to restrict the user to his/her personal view (s). A combination of
table level security and view level security can be used to limit a user’s access precisely
to the data that user needs.
A user may have several forms of authorisation on parts of the database. Authorisation is
a means by which the database system can be protected against malicious or unauthorised
access. A user who has some form of authority may be allowed to pass this authority on
to other users. However, we need to be careful about how authorisation may be passed
among users in order to ensure that we can revoke authorisation at some future time.
SQL provides capability to protect or control access to i) table ii) view iii) columns of a
table.
Integrity constraints
An integrity constraint is a rule that restricts the values for one or more columns in
table/view. There are several types of constraints which fulfil our real life requirements.
The following are two examples of integrity constraints :
i) rate < 50 ii) quantity > 1000
Integrity constraints may be enforced either by application program or by DBMS. Here
we shall restrict ourselves to the second one. The integrity constraint can appear in either
‘create table’ and ‘alter table’.
column_constraint
not null
alter table vendor
modify ( vendor_add varchar(20)
constraint nn_add not null);
unique
create table item (
item_id varchar(6),
item_descrip varchar(15) unique,
.
.
. );
primary key
create table customer (
customer_code varchar(8) primary key,
customer_add varchar(20),
.
.
. );
A primary key constraint designates a column or combination of columns as table’s
primary key. A table can have only one primary key. To satisfy a primary key constraint,
both of the following conditions must be true :
i) No primary key value can appear in more than one row in the table.
ii) No column that is part of the primary key can contain a null.
iii) No column or combination of columns can be designated both as a primary key and a
unique key.
on delete cascade);
Check Constraint
The following are two examples of check constraint using two different syntaxes.
View Mechanism
View is virtual table which does not physically exist. It restricts access to data and hence
can be used as a security mechanism.
Create View
create view vendor_list
select vendor_name, vendor_add
from vendor;
------------------------------------------------------------------------------------------
Vendor Vendor Vendor Vendor
code name add tel
------------------------------------------------------------------------------------------
CP123 | Computer Point Camac street | 294954
-------------------|------------------------------------------------------|---------------
DM459 | Data Maintenance Park street | 602364
-------------------|------------------------------------------------------|---------------
BARC102 | Bharat Cmp Alipore | 312357
-------------------------------------------------------------------------|----------------
TELI65 | Telephone Cmp S.N.Ban. Rd | 471234
-------------------------------------------------------------------------------------------
Vendor_list
The objective of the view vendor_list may be to hide vendor_code and vendor_tel from
some users. Instead of the full table the view is available to the users.
select count(*)
from roller_vendor;
Updation of views are tricky, complicated and ambiguous. It is still in active area of
research. Any updation on a view need to be translated into an update on base tables. A
few of the problems with updation of views are described below :
Let us consider a table Quotation_2 in the purchase order system. The attributes are
quotation_no, item_id, price, qty. Now let us create a view rod_prices which will give a
list of quotations for the item ‘iron rod’ and correponding prices.
create view rod_prices
select quotation_no, price
from quotation_2
where item_id = ‘iron rod’;
Now the problem is, an insert into rod_prices gets converted insert on quotation_2
without value for item_id. Now the problem is ‘ should this be rejected or accepted with
item_id set to null ?’
Authorisation
A user may have several forms of authorisation on parts of the database. The ultimate
form of authority is that given to the database administrator. The database administrator
may authorise new users, restructures the database etc. This form of authorisation is
analogous to that provided to a “superuser” for an operating system.
A user who has been granted some form of authority may be allowed to pass this
authority onto other users. However, we need to be careful about how authorisation may
be passed among users in order to ensure that we can revoke authorisation at some future
time.
The purpose of privileges is to grant different types of authorisation for a particular object
to users. There is another types of privileges called system privileges. This is needed to
manage the system. In this context only grant on ‘object privileges’ is discussed.
There are various types of object privileges. A few of them are listed below :
1. ALTER
2. DELETE
3. INSERT
4. SELECT
5. UPDATE
6. REFERENCE
If you want to give someone to use all the privileges available on a particular object, it is
possible to give it by the keyword ALL PRIVILAGES provided you have been granted all
privileges on that particular object. The user who owns the schema containing an object
automatically has all the privileges on the object.
The object may be of various types say TABLE, VIEW etc. Privileges may be granted to
either selective user(s) by giving their user_id or to all users by giving the keyword
PUBLIC.
Among the privileges listed earlier ALTER and REFERENCE privileges must not be on
object VIEW.
Examples
Let us grant to see the table ITEM to the user with user_id storekeeper.
grant select
on item
to storekeeper;
Let us take another example. Here a user, the managing director, with user_id md is
considered.
grant delete, select
on purchase_order
to md
with grant option;
It allows managing director to cancel all purchase order. A WITH GRANT OPTION is
used, managing director can in turn pass these privileges onto others, say , deputy
director.
The following is an example of granting UPDATE privileges.
Cancellation of Authorisation
If REVOKE is used on designated object it means the withdrawl of the privileges from
the designated user. Question of revoking does arise only when the user either directly or
indirectly has been granted the privilege.
If you revoke a privilege from a user, the system removes the privilege from the user’s
privilege domain. The user cannot exercise the privilege as soon as it is removed.
If you revoke a privilege from public, the system removes the privilege from
Revoking Privileges
Examples
revoke select
on item
from storekeeper;
revoke all
on purchase_order
from md;