DBMS_Unit-2 relational algebra
DBMS_Unit-2 relational algebra
Syntax − σ<condition>(relation_name)
Where condition is prepositional logic formula which uses
operators like V, ꓥ, !, =, ≠, ≥, < , >, ≤
• Consider a relation
Employee
Eno Ename Dno
1 Raj 10
2 Ravi 20
3 Ram 10
emp(Employee)
4) Union (U)
• This is one of the Set Operators.
• Union operator is denoted by symbol and it is used to select all
the rows (tuples) from two tables (relations).
Syntax: A U B
Ex: R1 X R2
A B X Y
Aa 100 Xx 99
Aa 100 Yy 11
Bb 200 Xx 99
Bb 300 Yy 11
Additional Operators/ Derived Operators
There are mainly three types of extended/additional
operators in Relational Algebra:
• Join
• Intersection
• Divide
JOINS
• Two types
1) Inner Join
a) Natural Join
b) Theta Join
c) Equi Join
2) Outer Join
a) Left Outer Join
b) Right Outer Join
c) Full Outer Join
1a) Natural Join
• Natural join does not utilize any of the comparison operators.
• Notation: R1 R2
• Where:
– The selection C checks equality of all common attributes
– The projection eliminates the duplicate common attributes
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
SSN Dname
999999999 Emily
777777777 Joe
Employee Dependents =
Name, SSN, Dname(SSN=SSN(Employee x Dependents))
• Syntax:
R1 A=B R2
• Meaning:
R1 A=B R2 = A=B (R1 R2)
• Consider two relations for doing these relational algebra operations
R1 R2
Cid Name Sid Sid sname Age
C101 Aditya 901 901 Aditya 19
C104 Aditya 901 911 Steve 18
C106 Steve 911 921 Paul 19
C109 Paul 921 931 Lucy 17
C115 Lucy 931 941 Carl 16
951 Rick 18
Ex:
Cid Name Sid Sid sname Age
C101 Aditya 901 901 Aditya 19
C104 Aditya 901 901 Aditya 19
C106 Steve 911 911 Steve 18
C109 Paul 921 921 Paul 19
C115 Lucy 931 931 Lucy 17
2. Outer Joins
• The result of a join (or inner join) consists of tuples formed by
combining matching tuples in the two operands or relations, an outer
join contains those tuples and additionally some tuples formed by
extending an unmatched tuple in one of the operands or relations by
"fill" values for each of the attributes of the other operand.
• An extension of the join operation(Outer Join) that avoids loss of
information
• Three outer join operators are defined:
a) Left outer join ( )
b) Right outer join ( )
c) Full outer join ( )
Left outer join (also known as left join): returns
all the rows from the table on the left even if no
matching rows have been found in the table on the
right. When no matching record found in the table
on the right, NULL is returned.
Right outer join (also known as right join):
returns all the rows from the table on the right even if no
matching rows have been found in the table on the left.
Where no matches have been found in the table on the
left, NULL is returned.
Full outer join (also known as full join):
It returns all tuples from both relations are
included in the result, irrespective of the matching
condition.
Intersection
• Intersection defines a relation consisting of a set of all
tuple that are in both A and B.
• It is a derived operator
A B = A – (A – B)
• Consider two relations for doing these relational algebra operations
Course Student
Cid Name Sid Sid sname Age
C101 Aditya S901 S901 Aditya 19
C104 Aditya S901 S911 Steve 18
C106 Steve S911 S921 Paul 19
C109 Paul S921 S931 Lucy 17
C115 Lucy S931 S941 Carl 16
S951 Rick 18
Q6) To Find the set of all courses taught in the Fall 2009 semester,
the Spring 2010 semester, or both.
Q7) To find all the courses taught in the Fall 2009 semester but not
in Spring 2010 semester.
Answers
A1) σdept_name = "Physics"(instructor)
A2) σsalary > 90000(instructor)
A3) σdept_name = "Physics" ∧ salary > 90000(instructor)
A4) ∏ ID, name, salary (instructor)
A5) ∏ name (σ dept_name =“Physics”(instructor))
A6) ∏ course_id (σ semester =“Fall” year = 2009 (section))
∏ course_id (σ semester =“Spring” year = 2010 (section))
A7) ∏ course_id (σ semester =“Fall” year = 2009 (section)) -
∏ course_id (σ semester =“Spring” year = 2010 (section))
Relational Data Model
Data types
• CHAR(SIZE):
It is used to store fixed length character strings.
For fixed length strings, a shorter string is padded with blank
characters to the right. For example, if the value ‘COMPUTERS’ is for
an attribute of type CHAR (10), it is padded with one blank character
to become ‘COMPUTERS ’ if needed.
CHAR takes a maximum size of 2000 bytes.
C O M P U T E R S
• VARCHAR2(SIZE):
It is used to store variable length character strings.
When we use VARCHAR2 Padded blank spaces are ignored and
only the total numbers of characters or memory needed for the string
are used(Example for the string COMPUTERS Only 9 characters are
needed).
VARCHAR2 takes a maximum size of 4000 bytes.
C O M P U T E R S
• NUMBER
NUMBER data types are used to store numbers of various sizes like
INTEGER and floating-point (real) numbers of various precision (FLOAT
or REAL).
A floating point value can be specified with Number data type in
the following way.
NUMBER (Precision, Scale).
Where precision is the total number of places or digits occupied by
the floating point value and scale is the number of digits after the
decimal point.
• Date:
It is used to store dates and time information.
• BLOB:
It is used to store images, audio files and video files
• CLOB:
It is used to store text files
• BFILE:
It is used to store binary files and executable files etc.,
Data Definition Language(DDL)
• Data definition language (DDL) describes the portion of SQL
that creates, alters, and deletes database objects.
• The main commands of this category are as follows:
i) CREATE
ii) ALTER
iii) DROP
i) CREATE
• It is used to create any Oracle database objects like Table, View, Trigger,
Index, User etc.
• Syntax:
SQL> CREATE TABLE <table_name>
(
<column_name1> DATATYPE(WIDTH),
<column_name2> DATATYPE(WIDTH),
…
<column_nameN> DATATYPE(WIDTH)
);
Example:
• Table name: Student
• Attributes: sid, sname, gender, branch
TABLE LEVEL
COLUMN LEVEL
Ex:
Ex:
CREATE TABLE STUDENT
CREATE TABLE STUDENT
(
(
sid VARCHAR2(20),
sid VARCHAR2(20) UNIQUE
CONSTRAINT uc1 UNIQUE(sid)
);
);
c) PRIMARY KEY
• A primary key is an attribute that is used to uniquely identify the
records or rows of a relation or table.
• It is a combination of NOT NULL and UNIQUE.
PRIMARY KEY=(UNIQUE + NOT NULL)
• Only one primary key is allowed per table.
TABLE LEVEL
COLUMN LEVEL
Ex:
Ex:
CREATE TABLE STUDENT
CREATE TABLE STUDENT
(
(
sid VARCHAR2(20),
sid VARCHAR2(20) PRIMARY KEY
CONSTRAINT pk1 PRIMARY KEY(sid)
);
);
d) DEFAULT
• DEFAULT keyword is used in SQL to assign a default value for a given
attribute(column) of a relation
• The default value will be added to all new records, if no other value is
specified.
COLUMN LEVEL
Ex: TABLE LEVEL
CREATE TABLE STUDENT It is not possible in table level.
(
age NUMBER(5) DEFAULT 18
);
e) CHECK
• Check constraints are the conditions that are applied on the
attribute(column) of a relation.
COLUMN LEVEL
Ex:
CREATE TABLE employee
(
sid VARCHAR2(10) PRIMARY KEY,
salary NUMBER(10,2) CHECK(salary>0),
age NUMBER(5) CHECK(age BETWEEN 18 AND 50)
);
TABLE LEVEL
Ex:
CREATE TABLE employee
(
sid VARCHAR2(10), salary NUMBER(10,2), age NUMBER(5),
CONSTRAINT pk1 PRIMARY KEY(sid),
CONSTRAINT cck1 CHECK(salary>0),
CONSTRAINT cck2 CHECK(age BETWEEN 18 AND 50)
);
f) FOREIGN KEY
• Foreign key is generally used to establish the link between two tables
• Foreign key is always used with REFERENCES Keyword
• A foreign key is an attribute or combination of attributes present in
one relation that refers to another attribute or combination of
attributes usually the primary key in another relation.
Referencing Table
Referenced Table
20 CSE VIZAG
30 ECE GUNTUR
COLUMN LEVEL
Ex:
CREATE TABLE employee
(
ssn NUMBER(4) PRIMARY KEY,
ename VARCHAR2(10) NOT NULL,
salary NUMBER(10,2) CHECK(salary>0),
deptno NUMBER(2) REFERENCES department(deptno),
age NUMBER(3) CHECK(age BETWEEN 18 AND 50)
);
TABLE LEVEL
Ex:
CREATE TABLE employee
(
ssn NUMBER(4), ename VARCHAR2(10), salary NUMBER(10,2),
deptno NUMBER(2), age NUMBER(3),
CONSTRAINT pk1 PRIMARY KEY(ssn),
CONSTRAINT cck1 CHECK(salary>0),
CONSTRAINT fc1 FOREIGN KEY(deptno) REFERENCES department(deptno),
CONSTRAINT cck2 CHECK(age BETWEEN 18 AND 50)
);
Maintaining referential integrity with ON
DELETE CASCADE option:
• If we use ON DELETE CASCADE option, ORACLE permits deletions of
referenced key values in the parent table and automatically deletes
dependent rows in the child table to maintain referential integrity.
TABLE LEVEL
Ex:
CREATE TABLE employee
(
ssn NUMBER(4),
deptno NUMBER(2), age NUMBER(3),
CONSTRAINT pk1 PRIMARY KEY(ssn),
CONSTRAINT fc1 FOREIGN KEY(deptno) REFERENCES department(deptno) ON DELETE CASCADE
);
Maintaining referential integrity with ON
UPDATE CASCADE option:
• If we use ON UPDATE CASCADE option, ORACLE permits updation of
referenced key values in the parent table and automatically updates
dependent rows in the child table to maintain referential integrity.
TABLE LEVEL
Ex:
CREATE TABLE employee
(
ssn NUMBER(4),
deptno NUMBER(2), age NUMBER(3),
CONSTRAINT pk1 PRIMARY KEY(ssn),
CONSTRAINT fc1 FOREIGN KEY(deptno) REFERENCES department(deptno) ON DELETE CASCADE
);
Basic Retrieval Queries in SQL, INSERT, DELETE, and
UPDATE Statements in SQL / DML
• DML commands are used for adding, retrieving, deleting and
modifying data in a database.
• The main commands of this category are as follows:
i. INSERT
ii. DELETE
iii. UPDATE
iv. SELECT
i) INSERT
• To insert rows of information into DB table.
• Syntax:
INSERT INTO <table_name>
[(<col1>,<col2>,..)] VALUES (<val1>,<val2>,..>)
• Ex:
1) INSERT INTO emp(empno,ename,sal,deptno, hiredate) VALUES(1, ’kiran’,10000, 123, ’12-
jun-20’);
(OR)
2) INSERT INTO emp VALUES(1, ’kiran’,10000, 123, ’12-jun-20’);
(OR)
3) INSERT INTO emp(empno,ename,sal,deptno, hiredate)
VALUES(&empno,’&ename’,&sal,&deptno, ‘&hiredate’);
ii) DELETE
• The DELETE statement is used to delete existing records in a table.
• DELETE Syntax:
DELETE FROM table_name WHERE condition;
• Ex: Delete student data whose sid is 102.
DELETE FROM student WHERE sid=102;
Note: During sorting process ORACLE will consider NULL values as the biggest
value.
It will place NULL at first in descending order.
It will place NULL at last in ascending order.
12) Display empno, ename, sal, daily sal and annual sal of all employees in
descending order of annual salary.
SELECT empno, ename, sal, sal/30 as d_sal, sal*12 as a_sal
FROM emp
ORDER BY a_sal desc;
13) Display empno, ename, sal, sysdate, hiredate and experience of all the
employees in descending order of experience.
SELECT empno, ename, sal, SYSDATE Today, hiredate doj, SYSDATE-hiredate exp
FROM emp
ORDER BY exp DESC;
Note: Here SYSDATE-hiredate gives number of days.
14) Display all the unique salaries of all the clerks in ascending order.
SELECT DISTINCT sal FROM emp
WHERE job=‘clerk’
ORDER BY sal;
IN operator
• It is used to define a list. Always the elements of list are enclosed
within the parenthesis and separated by commas.
16) Display all the employees whose salaries lies between 10000 and 20000
SELECT *FROM emp
WHERE sal BETWEEN 10000 AND 20000;
Questions
17) Write a query to list all the employees joined in year 1981.
18) Display all the employees in descending order of their salaries having an
experience which is ranging from 25yrs up to 39yrs.
Solutions
Ans-17) SELECT *FROM emp
WHERE hiredate BETWEEN ‘01-JAN-1981’ AND ‘31-DEC-1981’;
OR
SELECT *FROM emp
WHERE hiredate>=‘01-JAN-1981’ AND hiredate<=‘31-DEC-1981’;
27) Display the details of all the employees working under some manager.
SELECT *FROM emp
WHERE mgr IS NOT NULL;
28) Display all the employees receiving no commission.
SELECT *FROM emp
WHERE comm IS NULL;
Exercise Problem
Q29) Display all the clerks working for department number 10 and
20 with salary more than 900.
Ans-29)
SELECT *FROM emp
WHERE job=‘CLERK’ AND deptno IN(10,20) AND sal>900
Nested queries
• A sub-query or inner query or a nested query is a query within another query and
embedded within the WHERE clause.
• A subquery is used to return data that will be used in the main query as a condition
to further restrict the data to be retrieved.
• Syntax: Subqueries with the SELECT Statement:
Query:
SELECT empname FROM employee INTERSECT SELECT sname FROM student;
4. MINUS (SET DIFFERENCE):
It displays the rows which are present in the first query but absent in the second
query with no duplicates.
Query:
select empname from employee minus select sname from student;
Aggregate Functions or Group Functions
1) MAX(<col_name>): This function is used to find the maximum value of the
column in the given set of rows.
2) MIN(<col_name>): This function is used to find the minimum value of the
column in the given set of rows.
3) SUM(<col_name>): This function is used to find the sum of the column values
in the given set of rows.
4) AVG(<col_name>): This function is used to find the average of the column
values in the given set of rows.
5) COUNT(<col_name>): This function is used to count the number of rows in the
given set.
GROUP BY and HAVING
• GROUP BY clause is used to split the data of tables (or) table into
multiple groups based on one (or) more column based value.
• GROUP BY is used to get the summarised information of multiple
groups based on one or more attributes.
• The GROUP BY statement is often used with aggregate functions
(COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or
more columns.
• HAVING is used to check the conditions of GROUP BY.
• The HAVING clause was added to SQL because the WHERE keyword
cannot be used with aggregate functions.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Rules:
1) Th columns those are specified in GROUP BY clause only allowed in SELECT list.
2) Any column with any group function can also be used though it is not specified
in GROUP BY.
3) GROUP BY will split the table into multiple groups based on column names.
Consider an EMP relation:
Q1) Find the sum of salaries of all departments.
Q3) Find maximum and minimum salary, total employees, sum and
average salaries of employees in each department individually.
Q4) Display the number of employees belong to each Job group.