0% found this document useful (0 votes)
18 views58 pages

JNTUH Dbms Unit4

This document provides an overview of basic SQL query forms, including table creation, data insertion, and various query examples using SELECT, WHERE, and JOIN clauses. It also covers operators like UNION, INTERSECT, and EXCEPT, as well as nested queries and aggregation functions. Additionally, it explains the use of ORDER BY and GROUP BY clauses for sorting and grouping data in SQL queries.

Uploaded by

sanjunasam04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views58 pages

JNTUH Dbms Unit4

This document provides an overview of basic SQL query forms, including table creation, data insertion, and various query examples using SELECT, WHERE, and JOIN clauses. It also covers operators like UNION, INTERSECT, and EXCEPT, as well as nested queries and aggregation functions. Additionally, it explains the use of ORDER BY and GROUP BY clauses for sorting and grouping data in SQL queries.

Uploaded by

sanjunasam04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

UNIT - III

Form of basic SQL query

We will present a number of sample queries using the following table definitions:

Sailors (sid: integer, sname: string, rating: integer, age: real)


Boats (bid: integer, bname: string, color: string)
Reserves (sid: integer, bid: integer, day: date)

Queries:
 CREATE TABLE sailors (sid int PRIMARY KEY, sname varchar (50),
rating integer ,age int)
 CREATE TABLE boats(bid int PRIMARY KEY,bname varchar(50),color
varchar(50));
 CREATE TABLE reserves (sid int,bid int,day date,PRIMARY KEY
(sid,bid,day) ,FOREIGN KEY(sid) REFERENCES sailors(sid), FOREIGN
KEY(bid) REFERENCES boats(bid));

 INSERT INTO sailors VALUES


(22,"Dustin",7,45),(29,"Brutus",1,33),(31,"Lubber",8,55),(32,"Andy",8,25),
(58,"Rusty", 10, 35),(64,"Horatio",7,35),(71,"Zorba",10,16),
(74,"Horatio", 9, 35),(85,"Art",3,25),(95,"Bob",3,63);
 INSERT INTO boats VALUES
(101,"Interlake","Blue"), (102,"Interlake","Red"),
(103,"Clipper","Green"), (104,"Marine","red");
 INSERT INTO reserves VALUES
(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);

111
This section presents the syntax of a simple SQL query and explains its meaning
through a conceptual Evaluation strategy. A conceptual evaluation strategy is a
way to evaluate the query that is intended to be easy to understand rather than
efficient. A DBMS would typically execute a query in a different and more
efficient way.
The basic form of an SQL query is as follows:

SELECT [DISTINCT] select-list


FROM from-list
WHERE qualification

Example:

SELECT DISTINCT S.sname, S.age FROM Sailors S

Output:

The DISTINCT keyword is optional. It indicates that the table computed as an


answer to this query should not contain duplicates.
If two or more sailors have the same name and age, the answer still contains just
one pair. This query is equivalent to the projection operator of relational algebra.

112
Example:
SELECT S.sname, S.age FROM Sailors S

Output:

Example:

Q: Find all sailors with a rating above 7.


A: SELECT S.sid, S.sname, S.rating, S.age FROM Sailors AS S
WHERE S.rating > 7
Or
SELECT sid, sname, rating, age from sailors where rating > 7

Output:

113
AS is an optional keyword used to assign temporary names to table or column
name or both. This is known as creating Alias in SQL.

SELECT clause is actually used to do projection, whereas selections in the


relational algebra sense are expressed using the WHERE clause.

Q: Find the names of sailors who have reserved boat number 103.

A: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND


R.bid=103

Output:

Q: Find the sids of sailors who have reserved a red boat.

A: SELECT R.sid FROM Boats B, Reserves R WHERE B.bid = R.bid AND


B.color = 'red';

Output:

114
Q: Find the names of sailors who have reserved a red boat.
A: SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid
AND R.bid = B.bid AND B.color = 'red';
Output:

Q: Find the colors of boats reserved by Lubber.


A: SELECT B.color FROM Sailors S, Reserves R, Boats B WHERE R.bid =
B.bid AND S.sid = R.sid AND S.sname = 'Lubber';
Output:

115
Like Operator:
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.
There are two wildcards often used in conjunction with the LIKE operator:
 The percent sign (%) represents zero, one, or multiple characters
 The underscore sign (_) represents one, single character

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
Q: Find the sailors details whose name starts with ‘D’.
A: SELECT * from sailors where sname like "D%";
Q: Find the sailors details whose name ends with ‘Y’.
A: SELECT * from sailors WHERE sname LIKE "%Y";
Q: Find the sailors details whose name contains “bb”.
A: SELECT * from sailors WHERE sname LIKE "%bb%";
Q: Find the sailors details whose name contain ‘o’ in second place.
A: SELECT * FROM sailors where sname LIKE "_o%";
Q: Find the sailors details whose name contain “s” in the third place and
name should contain total of five characters.
A: SELECT * FROM sailors WHERE sname like "__s__";
Q: Find the details of sailors whose names does not starts with “a”.
A: SELECT * FROM sailors WHERE sname NOT LIKE "a%";

116
SELECT * FROM sailors WHERE sname LIKE '%a%'
INTERSECT
SELECT * FROM sailors WHERE sname NOT LIKE '%u%'

117
UNION, INTERSECT AND EXCEPT

UNION:

Union is an operator that allows us to combine two or more results from multiple
SELECT queries into a single result set. It comes with a default feature that
removes the duplicate rows from the result set. MySQL always uses the name of
the column in the first SELECT statement will be the column names of the result
set (output).

Union must follow these basic rules:

 The number and order of the columns should be the same in all tables that
you are going to use.
 The data type must be compatible with the corresponding positions of each
select query. 
 The column name selected in the different SELECT queries must be in the
same order.

Syntax:
SELECT expression1, expression2, expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, expression_n
FROM tables
[WHERE conditions];

Q: Find the sid’s and names of sailors who have reserved a red or a green boat.

A: SELECT S.sid,S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
union
SELECT S2.sid,S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = 'green';

118
Output:

INTERSECT:
The INTERSECT operator is used to fetch the records that are in common between
two SELECT statements or data sets. If a record exists in one query and not in the
other, it will be omitted from the INTERSECT results.
Syntax:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Intersection must follow these basic rules:

 The number and order of the columns should be the same in all tables that
you are going to use.
 The data type must be compatible with the corresponding positions of each
select query. 
 The column name selected in the different SELECT queries must be in the
same order.

Q: Find the sid’s and names of sailors who have reserved a red and a green boat.

A: SELECT S.sid,S.sname
FROM Sailors S, Reserves R, Boats B

119
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
INTERSECT
SELECT S2.sid,S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = 'green';

Output:

EXCEPT:

The EXCEPT clause in SQL is widely used to filter records from more than one
table. This statement first combines the two SELECT statements and returns
records from the first SELECT query, which isn’t present in the second SELECT
query's result. In other words, it retrieves all rows from the first SELECT query
while deleting redundant rows from the second.

This statement behaves the same as the minus (set difference) operator does in
mathematics.

Rules for SQL EXCEPT


We should consider the following rules before using the EXCEPT statement in
SQL:
 In all SELECT statements, the number of columns and orders in the tables
must be the same.
 The corresponding column's data types should be either the same or
compatible. 
 The fields in the respective columns of two SELECT statements cannot be
the same.
Syntax:
SELECT column_lists from table_name1
EXCEPT
SELECT column_lists from table_name2;

120
Q: Find the sids of all sailors who have reserved red boats but not green boats.
A: SELECT S.sid
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
MINUS // minus in Oracle, Except in SQL server
SELECT S2.sid
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = 'green';
Output:

121
Nested Queries
A nested query is a query that has another query embedded within it; the embedded
query is called a sub query. A sub query is known as the inner query, and the query
that contains sub query is known as the outer query.

Syntax:

As per the execution process of sub query, it again classified in two ways.

1. Non- Correlated sub queries.


In this sub queries, First inner query is executed and later Outer query will
execute. i.e Outer query always depends on the result of inner query.

2. Correlated Queries.
In correlated queries, First Outer query is executed and later Inner query will
execute. i.e Inner query always depends on the result of outer query.

Example for Non – Correlated Queries:

Q: Find the names of sailors who have reserved boat 103.


A: SELECT S.sname from sailors S WHERE S.sid IN (SELECT R.sid FROM
reserves R WHERE R.bid=103);

Output:

122
SQL IN condition used to allow multiple value in a WHERE clause condition.
SQL IN condition you can use when you need to use multiple OR condition.

(Q) Find the names of sailors who have reserved a red boat.

A: SELECT S.sname from sailors S WHERE S.sid IN


(SELECT R.sid FROM reserves R where R.bid IN
(SELECT B.bid FROM boats B WHERE B.color="red"));
Output:

(Q) Find the names of sailors who have not reserved a red boat.

A: SELECT S.sname from sailors S WHERE S.sid NOT IN


(SELECT R.sid FROM reserves R where R.bid IN
(SELECT B.bid FROM boats B WHERE B.color="red"));

Output:

123
SQL NOT IN condition used to exclude the defined multiple value in a WHERE
clause condition.

Correlated Sub Queries:


Correlated sub queries are the one in which inner query or sub query reference
outer query. Outer query needs to be executed before inner query. One of the
most common examples of correlated sub query is using keywords exits and not
exits.

Example:
(Q) Find the names of sailors who have reserved boat number 103
SELECT S.sname FROM Sailors S WHERE
EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 AND R.sid = S.sid)

124
Aggregation Operators:

SQL aggregation function is used to perform the calculations on multiple rows of


a single column of a table. It returns a single value. It is also used to summarize
the data.
SQL supports five aggregate operations, which can be applied on any column, say
A, of a relation:

1. COUNT ([DISTINCT] A): The number of (unique) values in the A column.


2. SUM ([DISTINCT] A): The sum of all (unique) values in the A column.
3. AVG ([DISTINCT] A): The average of all (unique) values in the A column.
4. MAX (A): The maximum value in the A column.
5. MIN (A): The minimum value in the A column.
Example:
CREATE TABLE employees(id int,name char(50),dob date,gender char(5),sal
int,dept char(10),collegename char(15),age int);
DESC employees;
INSERT INTO employees VALUES
(1,"Srikanth",'1990-05-05','M',25000,'cse','kits',25),
(2,"Kamal",'1992-02-02','M',65000,'ece','kitsw',29),
(3,"kavitha",'1993-01-01','F',45000,'cse','kits',30),
(4,"Karun",'1988-06-04','M',25000,'cse','kits',31),
(5,"Gouthami",'1980-02-09','F',10000,'civil','kits',29),
(6,"Narendher",'1985-09-18','M',30000,'civil','kits',35),
(7,"Mahesh",'1990-03-06','M',8000,'cse','kitsw',45);
(8, NULL,"1992-06-09",'F',65000,"ece",'kitsw',46),
(9,"Sravanthi","1991-07-04",'F',46000,"CSE",'kitsw',29),
(1,"Neeraja","1991-07-04",'F',50000,"CSE",'kitsw',26);

125
Queries:
SELECT COUNT(*) from employees; // Output=10
SELECT COUNT(name) FROM employees; // Output=9
SELECT COUNT(id) FROM employees; // Output=10
SELECT COUNT(DISTINCT id) FROM employees; // Output=9
SELECT COUNT(*) FROM employees WHERE collegename ="kitsw";
// Output=5
SELECT MAX(sal) FROM employees; // Output=65000
SELECT MAX(sal) as Maximum_salary FROM employees; // Output=65000
SELECT MAX(sal) FROM employees WHERE collegename="kits";
// Output=65000
SELECT MIN(sal) FROM employees; // Output=8000
SELECT MIN(sal) FROM employees WHERE collegename='kits';
// Output=8000
SELECT SUM(sal) FROM employees; // Output=369000
SELECT sum(sal) as total_salary FROM employees; // Output=369000
SELECT AVG(sal) FROM employees; // Output=36900

126
Order by Clause:
Order by clause is used sort the rows either in ascending order or descending order
(default is ascending)
Example:
SELECT * from employees ORDER BY sal ASC;
Output:

SELECT * FROM employees ORDER BY sal DESC;


Output:

127
Group By clause:
The GROUP BY Statement in SQL is used to arrange identical data into groups
with the help of some functions. i.e if a particular column has same values in
different rows then it will arrange these rows in a group.

Important Points:
 GROUP BY clause is used with the SELECT statement.
 In the query, GROUP BY clause is placed after the WHERE clause.
 In the query, GROUP BY clause is placed before ORDER BY clause if used
any.

Syntax:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
ORDER BY column1, column2;

function_name: Name of the function used for example, SUM() , AVG().


table_name: Name of the table.
condition: Condition used.

Examples:

Q: Display the Number of employees in each department.


A: SELECT dept,COUNT(*) FROM employees GROUP BY dept;
Output:

128
SELECT dept,COUNT(*) AS No_of_employees FROM employees GROUP BY
dept;
Output:

Q: What is the highest salary in each department?


A: SELECT dept,MAX(sal) from employees GROUP BY dept;
Output:

Q: What is the total salary each department is paying?


A: SELECT dept,SUM(sal) AS total_amount_paying from employees GROUP BY
dept;
Output:

129
Q: Display the Number of male and female faculty.
A: SELECT gender,COUNT(*) FROM employees GROUP BY gender;
Output:

Q: What is the highest and lowest salary where each dept is paying.
A: SELECT dept,MAX(sal),MIN(sal) FROM employees GROUP BY dept;
Output:

Q: Display the number of male and female faculty from each Department.
A: SELECT dept,gender,COUNT(*) FROM employees GROUP BY dept, gender;
Output:

130
Q: Display the number of male and female’s faculty from each college.
A: SELECT collegename,gender,COUNT(*) from employees GROUP BY
collegename , gender;
Output:

Having Clause:

The HAVING clause is like WHERE but operates on grouped records returned by
a GROUP BY. HAVING applies to summarized group records, whereas WHERE
applies to individual records. Only the groups that meet the HAVING criteria will
be returned.

HAVING requires that a GROUP BY clause is present.


Both WHERE and HAVING can be used in the same query at the same time.
Syntax:
SELECT column-names
FROM table-name
WHERE condition
GROUP BY column-names
HAVING condition;
Example:
Query: SELECT dept,COUNT(*) FROM employees GROUP BY dept HAVING
COUNT(*)>2;
Output:

131
Query: SELECT dept,MAX(age) FROM employees GROUP BY dept HAVING
MAX(sal)>30;
Output:

Query: SELECT dept,sum(sal) FROM employees GROUP BY dept HAVING


SUM(sal) > 50000;
Output:

132
NULL
We have assumed that column values in a row are always known. In practice column
values can be unknown. For example, when a sailor, say Dan, joins a yacht club, he
may not yet have a rating assigned. Since the definition for the Sailors table has a
rating column, what row should we insert for Dan? What is needed here is a special
value that denotes unknown. Suppose the Sailor table definition was modified to
include a maiden-name column. However, only married women who take their
husband's last name have a maiden name. For women who do not take their
husband's name and for men, the maiden-name column is inapplicable. Again, what
value do we include in this column for the row representing Dan? SQL provides a
special column value called null to use in such situations. We use null when the
column value is either Unknown or inapplicable. Using our Sailor table definition,
we might enter the row (98. Dan, null, 39) to represent Dan. The presence of null
values complicates many issues, and we consider the impact of null values on SQL
in this section.

OUTER JOIN:
In an outer join, along with tuples that satisfy the matching criteria, we also include
some or all tuples that do not match the criteria.

Left Outer Join (A B)


In the left outer join, operation allows keeping all tuple in the left relation. However,
if there is no matching tuple is found in right relation, then the attributes of right
relation in the join result are filled with null values.

133
Example:
Student Marks Student Marks

id Name id marks id Name id marks


101 Raju 102 92 101 Raju NULL NULL
102 Rishi 103 99 102 Rishi 102 92
103 Karthik 103 Karthik 103 99

 CREATE TABLE student (id int, name char(50));


 INSERT INTO student VALUES (101,"raju"),(102,"rishi"),(103,"karthik");
 CREATE TABLE marks (id int,marks int);
 INSERT INTO marks values(102,92),(103,99);
 SELECT * from student LEFT OUTER JOIN marks on student.id=marks.id;

Output:

Right Outer Join: (A B)

In the right outer join, operation allows keeping all tuple in the right relation.
However, if there is no matching tuple is found in the left relation, then the attributes
of the left relation in the join result are filled with null values.

134
Example:
Student Marks Student Marks

Id Name id marks id Name id marks


101 Raju 102 92 102 Rishi 102 92
102 Rishi 103 99 103 Karthik 103 99
103 Karthik

 SELECT * from student RIGHT OUTER JOIN marks on


student.id=marks.id
Output:

Full Outer Join / Full Join: ( A B)

In a full outer join, all tuples from both relations are included in the result,
irrespective of the matching condition.

Student Marks Student Marks

Id Name id marks id Name id marks


101 Raju 102 92 101 Raju NULL NULL
102 Rishi 103 99 102 Rishi 102 92
103 Karthik 103 Karthik 103 99
102 Rishi 102 92

135
103 Karthik 103 99
 SELECT * from student LEFT OUTER JOIN marks on student.id=marks.id
UNION ALL
SELECT * from student RIGHT OUTER JOIN marks on student.id=marks.id
Output:

SELECT * from student FULL JOIN marks on student.id=marks.id //sql


server

136
Triggers (MySQL)
Triggers are the SQL codes that are automatically executed in response to certain
events on a particular table. These are used to maintain the integrity of the data.

It is name PL (procedural language) / SQL block.

To understand this let’s consider a hypothetical situation.

John is the marketing officer in a company. When a new customer data is entered
into the company’s database he has to send the welcome message to each new
customer. If it is one or two customers John can do it manually, but what if the
count is more than a thousand? Well in such scenario triggers come in handy.

Thus, now John can easily create a trigger which will automatically send a
welcome email to the new customers once their data is entered into the database.

Important points:

 To maintain Business data in Uniform case.


 To maintain audit information of any table data.

Triggers can be made to insert, update and delete statements in SQL. We have two
types of triggers:

1. Row Level Triggers


In a row-level trigger, the changes are put on all the rows on which the insert,
delete, or update transaction is performed.
If we have 1000 rows in a database and a delete operation is being run on them,
then the trigger would also run 1000 times automatically.
Executed once for each row affected.

2. Statement Level Triggers

In the statement-level triggers, the operation is under execution only once no


matter how many rows are involved in the operation.
Executed only once for the statement.

137
Parts of Triggers:
Trigger Event: Insert / Update / Delete
Trigger action: Before / after
Trigger body: Logic / functionality (What Trigger is doing)
Triggers allow access to values from the table for comparison purposes using
NEW and OLD. The availability of the modifiers depends on the trigger event you
use:
New: get new value from column (Insert & after update).
Old: get old value from the column (Delete & before update).

Syntax:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;

138
Here,

 CREATE [OR REPLACE] TRIGGER trigger_name: It creates or replaces


an existing trigger with the trigger_name.
 {BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger
would be executed. The INSTEAD OF clause is used for creating trigger on
a view.
 {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML
operation.
 [OF col_name]: This specifies the column name that would be updated.
 [ON table_name]: This specifies the name of the table associated with the
trigger.
 [REFERENCING OLD AS o NEW AS n]: This allows you to refer new and
old values for various DML statements, like INSERT, UPDATE, and
DELETE.
 [FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger
would be executed for each row being affected. Otherwise the trigger will
execute just once when the SQL statement is executed, which is called a
table level trigger.
 WHEN (condition): This provides a condition for rows for which the trigger
would fire. This clause is valid only for row level triggers.

139
Examples:

BEFORE INSERT:

 CREATE TABLE student(rollno int , name char(30));


 DELIMITER //
CREATE TRIGGER binsert BEFORE INSERT ON student
for EACH ROW
BEGIN
SET new.name=ucase(new.name);
END
//
 INSERT INTO student values(101,"deepak");
 SELECT * FROM student;

AFTER INSERT:
 DROP TABLE student;
 CREATE TABLE student(id int,name char(30),gmail char(20),pwd
char(20));
 INSERT INTO student
values(101,"Deepak","[email protected]","123456");
 SELECT * FROM student;

 CREATE TABLE student_copy(gmailcopy char(20),pwdcopy char(20));


 DELIMITER //
CREATE TRIGGER ainsert AFTER INSERT ON student
FOR EACH ROW
INSERT INTO student_copy(gmailcopy,pwdcopy)
VALUES(new.gmail,new.pwd);
//

140
 INSERT INTO student
VALUES(102,"Sagar","[email protected]","996655");
 SELECT * FROM student;

 SELECT * FROM student_copy;

BEFORE UPDATE:

 DROP TABLE emp;


 CREATE TABLE emp(empno int, name char(30));
 INSERT INTO emp VALUES(10, "Ravi")
 Delimiter //
 CREATE TRIGGER uexample BEFORE UPDATE ON emp
FOR EACH ROW
BEGIN
IF NEW.empno <= 0 THEN
SET NEW.empno= NULL;
END IF;
END
//
 INSERT INTO emp VALUES(40,"Raju");
 SELECT * FROM emp;

 update emp set empno=0 where empno=40;


 SELECT * FROM emp;

141
AFTER UPDATE:
 CREATE TABLE log(user char(30),status char(40));
 CREATE TRIGGER auexample AFTER UPDATE ON emp
FOR EACH ROW
INSERT INTO LOG VALUES(current_user(),concat('Updated by
',old.name," ",now()));
 SELECT * FROM emp;

 update emp set empno=50 where empno=10;


 SELECT * FROM emp;

 Select * from log;

142
BEFORE DELETE
 CREATE TABLE salaries(empno int,name varchar(20),salary int);
 INSERT INTO salaries VALUES
(501,"Raju",12000),
(502,"Rajesh",13000),
(503,"Raghu",14000),
(504,"Ranjith",15000);

 SELECT * FROM salaries;

 CREATE TABLE salary_deleted(empno int,name varchar(20),salary


int,deleted_time timestamp default now());
 DELIMITER //
 CREATE TRIGGER bdelete BEFORE DELETE ON salaries
FOR EACH ROW
BEGIN
INSERT INTO salary_deleted(empno,name,salary)
VALUES
(old.empno,old.name,old.salary);
END //
 SELECT * FROM salary_deleted;//Empty
 DELETE FROM salaries WHERE empno=501;
 SELECT * FROM salary_deleted;

143
AFTER DELETE:
 drop table salaries;
 CREATE TABLE salaries(empno int,salary int);
 INSERT INTO salaries values (501,3000),(502,2000),(503,1000);
 CREATE table salary_avg(sal int);
 INSERT INTO salary_avg (sal) SELECT SUM(salary) FROM salaries;
 SELECT * from salary_avg;
 DELIMITER //
 CREATE TRIGGER adelete
AFTER DELETE
ON salaries FOR EACH ROW
BEGIN
UPDATE salary_avg SET sal=sal-old.salary;
END //
 SELECT * FROM salaries;

 SELECT * FROM salary_avg;

 DELETE FROM salaries where empno=501;


 SELECT * FROM salaries;

 SELECT * FROM salary_avg;

144
SCHEMA REFINEMENT AND NORMALISATION

1. Schema Refinement:
The Schema Refinement is the process that re-defines the schema of a relation. The
best technique of schema refinement is decomposition.
Normalization or Schema Refinement is a technique of organizing the data in
the database. It is a systematic approach of decomposing tables to eliminate data
redundancy and undesirable characteristics like Insertion, Update and Deletion
Anomalies.
Redundancy refers to repetition of same data or duplicate copies of same data
stored in different locations.
Three types of redundancy:

1. File level
2. Entire record level
3. Few attribute have redundancy.

Anomalies: Anomalies refers to the problems occurred after poorly planned


databases where all the data is stored in one table which is sometimes called a flat
file database.

SID SNAME CID CNAME FEE


S1 A C1 C 5K
S2 A C1 C 5K
S1 A C2 C++ 10K
S3 B C2 C++ 10K
S3 B C3 JAVA 15K
PRIMARY(SID,CID)

Here all the data is stored in a single table which causes redundancy of data or say
anomalies as SID and Sname are repeated once for same CID. Let us discuss
anomalies one by one.
1. Insertion anomalies: It may not be possible to store some information unless
some otherinformation is stored as well.

145
2. redundant storage: some information is stored repeatedly
3. Update anomalies: If one copy of redundant data is updated, then
inconsistency is created unless all redundant copies of data are updated.
4. Deletion anomalies: It may not be possible to delete some information
without losing someother information as well.
Problem in updation / updation anomaly – If there is updation in the fee from
5000 to 7000, then we have to update FEE column in all the rows, else data will
become inconsistent.

Insertion Anomaly and Deletion Anomaly- These anomalies exist only due to
redundancy, otherwise they do not exist.
Insertion Anomalies: New course is introduced say c4 with course name DB, but no
student is there who is having DB subject.

Because of insertion of some data, It is forced to insert some other dummy data.

146
Deletion Anomaly:
Deletion of S3 student causes the deletion of course.

Because of deletion of some data, It is forced to delete some other useful data.

Solutions to Anomalies: Decomposition of Tables – Schema Refinement


To avoid redundancy and problems due to redundancy, we use refinement
technique called DECOMPOSITION.
Decomposition: Process of decomposing a larger relation into smaller relations.

Each of smaller relations contains subset of attributes of original relation.

147
Problems related to decomposition:
1. Do we need to decompose a relation?
2. That problems (if any) does a given decomposition cause?
Properties of Decomposition:

1. Lossless Join Decomposition:

 Consider there is a relation R which is decomposed into sub relations R1, R2 , …. , Rn.
 This decomposition is called lossless join decomposition when the join of the sub
relations results in the same relation R that was decomposed.
 For lossless join decomposition, we always have

R1 ⋈ R2 ⋈ R3 ……. ⋈ Rn = R
where ⋈ is a natural join operator

If a relation R is decomposed into two relations R1 & R2 then, it will be lossless iff

1. attribute (R1) U attribute (R2) = attribute(R)


2. attribute (R1) ∩ attribute (R2) ≠ ∅
3. attribute (R1) ∩ attribute (R2) → attribute (R1) or
attribute (R1) ∩ attribute (R2) → attribute (R2)

Example:
Consider the following relation R ( A , B , C ):

A B C
1 2 1
2 5 3

148
3 3 3
R(A, B, C)

Consider this relation is decomposed into two sub relations R 1 (A, B) and R2 (B, C)

The two sub relations are:

A B B C
1 2 2 1
2 5 5 3
3 3 3 3
R1(A,B) R2(B, C)

Now, let us check whether this decomposition is lossless or not.


For lossless decomposition, we must have-
R1 ⋈ R2 = R

Now, if we perform the natural join (⋈) of the sub relations R1 and R2 , we get

A B C
1 2 1
2 5 3
3 3 3
R(A, B, C)

This relation is same as the original relation R.


Thus, we conclude that the above decomposition is lossless join decomposition.

Example - 2:

R (A, B, C)

A B C

149
1 1 1
2 1 2
3 2 1
4 3 2

The two sub relations are:

R1 (A, R2 (B, C)
B)
A B B C
1 1 1 1
2 1 1 2
3 2 2 1
4 3 3 2

Note: Above example is not lossless join decomposition.

Dependency preserving decomposition:

Let a relation R(A,B,C,D) and set a FDs F = { A -> B , A -> C , C -> D} are given.
A relation R is decomposed into -

R1 = (A, B, C) with FDs F1 = {A -> B, A -> C}, and


R2 = (C, D) with FDs F2 = {C -> D}.
F' = F1 𝖴 F2 = {A -> B, A -> C, C -> D}
so, F' = F.
And so, F'+ = F+.

150
Functional dependencies
Functional dependency is a relationship that exists when one attribute uniquely
determines another attribute.
A functional dependency XY in a relation holds true if two tuples having
the same value ofattribute X also have the same value of attribute Y.
i. e if X and Y are the two sets of attributes in a relation R where
X ⊆ R, Y ⊆ R
Then, for a functional dependency to exist from X to Y,
if t1.X=t2.X then t1.Y=t2.Y where t1,t2 are tuples and X,Y are attributes.

A functional dependency is denoted by an arrow "→".


XY can be called as
X determines Y
Y is determined by X

Example:

eno ename sal City


1 Raju 30000 Knr
2 Ravi 20000 Hyd
3 Rakesh 50000 Hzb

In this example, if we know the value of Employee number, we can obtain


Employee Name, city, salary, etc. By this, we can say that the city, Employee
Name, and salary are functionally depended on Employee number.

151
Example 2:

r.no name marks dept Course


1 A 78 CS C1
` B 60 EE C1
3 A 78 CS C2
4 B 60 EE C3
5 C 80 IT C3
6 D 80 EC C2

Which of the following are functional dependencies


r.no →name
name →r.no ---- no functional dependency
r.no →marks --- functional dependency
dept →course
course →dept
marks →dept
name →marks
[name, marks] →dept
[name, marks] →[dept, course]

Example:

A B C D E
a 2 3 4 5
2 a 3 4 5
a 2 3 6 5
a 2 3 6 6

Which of the following are functional dependencies


1. A → BC
2. DE → C
3. C → DE
4. BC → A

152
Types of Functional Dependencies
There are two types of functional dependencies

1. Trivial Functional Dependencies


2. Non – Trivial Functional Dependencies

Trivial Functional Dependencies:

A functional dependency X → Y is said to be trivial if and only if Y ⊆ X.


Thus, if RHS of a functional dependency is a subset of LHS, then it is called as a
trivial functional dependency.
Example:
1. AB → A
2. AB → B
3. AB → AB
Non - Trivial Functional Dependencies:

A functional dependency X → Y is said to be non-trivial if and only if Y ⊄ X.


Example:
AB → C

153
Axioms / Inference rules: It assumes that axioms are true without being able to
prove them.
Developed by Armstrong in 1974, there are six rules (axioms) that all possible
functional dependencies may be derived from them
1. Reflexivity Rule
In the reflexive rule, if Y is a subset of X, then X determines Y.
If X ⊇ Y then X → Y
Example:
X= {A, B, C, D}
Y= {B, C}
ABC→BC

2. Augmentation Rule
The augmentation is also called as a partial dependency. In augmentation, if X
determines Y, then XZ determines YZ for any Z
If X → Y then XZ → YZ
Example:
For R (ABCD), if A → B then AC → BC

3. Transitivity Rule
In the transitive rule, if X determines Y and Y determine Z, then X must also
determine Z.
If X → Y and Y → Z then X → Z

Additional rules:
1. Union Rule
Union rule says, if X determines Y and X determines Z, then X must also
determine Y and Z.
If X→ Y and X→Z then X→YZ

2. Composition
If W → Z, X → Y, then WX → ZY.

3. Decomposition
If X→YZ then X→Y and X→Z

154
Closure of a Set of Attributes(X+) / Attribute closure:
X+ is the set of all attributes that can be determined using the given set X
(attributes).

Algorithm: Determining X+, the closure of X under F.

Input: Let F be a set of FDs for relation R.


Steps:
1. X+ = X //initialize X+ to X
2. For each FD: Y -> Z in F Do
If Y ⊆ X+ Then //If Y is contained in X+
X+ = X + 𝖴 Z //add Z to X+
End If
End For
3. Return X+ //Return closure of X
+
Output: Closure X of X under F

Example – 1:
We are given the relation R(A, B, C, D, E). This means that the table R has five
columns: A, B, C, D, and E. We are also given the set of functional dependencies:
{A->B, B->C, C->D, D->E}.

What is {A} +?
 First, we add A to {A}+.
 What columns can be determined given A? We have A -> B, so we can
determine B. Therefore, {A}+ is now {A, B}.
 What columns can be determined given A and B? We have B -> C in the
functional dependencies, so we can determine C. Therefore, {A}+ is now
{A, B, C}.
 Now, we have A, B, and C. What other columns can we determine? Well,
we have C -> D, so we can add D to {A}+.
 Now, we have A, B, C, and D. Can we add anything else to it? Yes, since D
-> E, we can add E to {A}+.
 We have used all of the columns in R and we have all used all functional
dependencies. {A}+ = {A, B, C, D, E}.

155
Example – 2:
Consider a relation R ( A , B , C , D , E , F , G ) with the functional dependencies-
A → BC
BC → DE
D→F
CF → G
Now, let us find the closure of some attributes and attribute sets
Closure of attribute A
A+ = { A }
= { A , B , C } ( Using A → BC )
= { A , B , C , D , E } ( Using BC → DE )
= { A , B , C , D , E , F } ( Using D → F )
= { A , B , C , D , E , F , G } ( Using CF → G )
Thus,

A+ = { A , B , C , D , E , F , G }

R(ABCDEFG) R(ABCDE)
A→B A→ BC
BC→ DE CD→ E
AEG →G B→ D
(AC)+={AC} (Using Reflexivity) E→ A
={ABC} (Using A→B) B+={B} (Using Reflexivity)
={ABCDE} (Using BC→ DE) ={BD} (Using B→ D)

R(ABCDEF)
AB→C
BC→AD
D→E
CF→B
(AB)+={AB} (Using Reflexivity)
={ABC} (Using AB→C)
={ABCD} (Using BC→AD)
={ABCDE} (Using D→E)

156
Super key: attribute / set of attributes whose closure contains all attributes of
given relation.

How to find candidate key(s) in a Relation:


Procedure:

 Determine all essential attributes of the given relation.


 Essential attributes are those attributes which are not present on RHS of any
functional dependency.
 Find closure of attribute / set of attributes for the essential attributes (Not
present R.H.S attributes)
If closure contains all the attributes, then it will be the only key.
Else find the combination of remaining attributes with the attributes
not present in R.H.S
Example:
Let R(A, B, C, D, E, F) be a relation scheme with the
following functional dependencies
C→F
E→A
EC→D
A→B

Step - 1

 Determine all essential attributes of the given relation (attributes which are
not present in R.H.S).
 Essential attributes of the relation are C and E.
 So, attributes C and E will definitely be a part of every candidate key.

Step - 2
Now,
 We will check if the essential attributes together can determine all remaining
non-essential attributes.
 To check, we find the closure of CE.

157
So, we have

{ CE }+ ={C,E}
= { C , E , F } ( Using C → F )
= { A , C , E , F } ( Using E → A )
= { A , C , D , E , F } ( Using EC → D )
= { A , B , C , D , E , F } ( Using A → B )

We conclude that CE can determine all the attributes of the given relation.
So, CE is the only possible candidate key of the relation.

More Examples:

R(ABCD)
AB→CD
D→A
Candidate keys are AB,BD

R(ABCD)
AB→CD
C→A
D→B
Candidate keys are AB, AD, BC,DC

R(WXYZ)
Z→W
Y→XZ
WX→Y
Candidate keys are Y, WX,ZX

R(ABCD)
A→B
B→C
C→A
Candidate keys are AD, BD, CD

158
R(ABCDE)
AB→CD
D→A
BC→DE
Candidate keys are AB, BC, BD

R(ABCDE)
AB→C
C→D
D→E
A→B
C→A
Candidate keys are A,C

R(ABCDE)
A→D
AB→C
B→E
D→C
E→A
Candidate key is B

R(ABCDEF)
AB→C
DC→AE
E→F
Candidate key is ABD,BDC

159
Normalization
Normalization is a database design technique that reduces data redundancy and
eliminates undesirable characteristics like Insertion, Update and Deletion
Anomalies. Normalization rules divide larger tables into smaller tables and links
them using relationships. The purpose of Normalization in SQL is to eliminate
redundant (repetitive) data and ensure data is stored logically.

Normalization rules are divided into the following normal forms:


1. First Normal Form
2. Second Normal Form
3. Third Normal Form
4. BCNF
5. Fourth Normal Form

1. First Normal Form:


A given relation is called in First Normal Form (1NF) if each cell of the table
contains only an atomic value (it states that an attribute of a table cannot hold
multiple values). i.e it should not contain multivalued attribute and no composite
attribute.
Example:
sid sname cname
1 Raju C,C++
2 Ravi C,Java
3 Rakesh Java

Relation is not in 1NF


After Decomposition
sid sname sid cname
1 Raju 1 C
2 Ravi 1 C++
3 Rakesh 2 C
2 JAVA
2 Java

Relation is in 1NF

160
Note:
 By default, every relation is in 1NF.
 This is because formal definition of a relation states that value of all the
attributes must be atomic.

2. Second Normal form:


A table is said to be in the second Normal Form if

 It should be in the First Normal form.


 And, it should not have Partial Dependency.
Or
A relation is in 2NF, if it is in 1NF and every non – prime attribute is fully
functional dependent on candidate of the relation.

Partial Dependency
A partial dependency is a dependency where few attributes of the candidate key
determines non-prime attribute(s).
Or
A partial dependency is a dependency where a portion of the candidate key or
incomplete candidate key determines non-prime attribute(s).

In other words,
X → a is called a partial dependency if and only if-
1. X is a proper subset of some candidate key and
2. a is a non-prime attribute or non – key attribute
If any one condition fails, then it will not be a partial dependency.

Prime or Key Attributes:


Attributes of the relation which exist in at least one of the possible candidate keys,
are called prime or key attributes.
Non Prime or Non Key Attributes:
Attributes of the relation which does not exist in any of the possible candidate keys
of the relation, such attributes are called non prime or non key attributes.

161
NOTE
To avoid partial dependency, incomplete candidate key must not determine any
non-prime attribute.
 However, incomplete candidate key can determine prime attributes.
Example - 1:
Consider a relation- R (V, W, X, Y, Z) with functional dependencies-
VW → XY
Y→V
WX → YZ
The possible candidate keys for this relation are
VW, WX, WY
From here,
 Prime attributes = { V , W , X , Y }
 Non-prime attributes = { Z }
Now, if we observe the given dependencies-
 There is no partial dependency. 
 This is because there exists no dependency where incomplete candidate key
determines any non-prime attribute. 
Thus, we conclude that the given relation is in 2NF.

Example - 2:
Consider a relation- R (A, B, C, D, E, F) with functional dependencies-
A→B
B→C
C→D
D→E
The possible candidate key for this relation is
AF
From here,
 Prime attributes = {A, F}
 Non-prime attributes = { B, C, D, E}
Now, if we observe the given dependencies-
 There is partial dependency.
 This is because there is an incomplete candidate key (attribute A) determines
non-prime attribute (B).
Thus, we conclude that the given relation is not in 2NF.

162
Example – 3:
Consider a relation- R (A, B, C, D) with functional dependencies-
AB → CD
C→A
D→B
The possible candidate key for this relation is
AB, AD, BC, CD

If there is no Non – prime attributes, then we can conclude that it is in 2NF

Example - 4:
Consider a relation- R (A, B, C, D) with functional dependencies-
AB → D
B→C
Possible candidate key is AB

Now, if we observe the given dependencies-


 There is partial dependency.
 This is because there is an incomplete candidate key (attribute B) determines
non-prime attribute (C).
Thus, we conclude that the given relation is not in 2NF.

163
3. Third Normal Form(3 NF):
A table is in 3NF
If
1. It is in 2NF
2. For every non trivial dependency
X → a, X is either super key or a is a prime attribute.
Or
A table is said to be in 3NF
If
1. It should be in 2NF
2. It should not have any transitive dependency
Or
A table is said to be 3NF form when it don’t have partial dependency and transitive
dependency.

Transitive dependency: When a non prime attribute finds another non prime
attribute, it is called transitive dependency.

Example-
Consider a relation- R ( A , B , C , D , E ) with functional dependencies-

A → BC
CD → E
B→D
E→A
The possible candidate keys for this relation are-
A, E, CD, BC

From here,

 Prime attributes = { A , B , C , D , E }
 There are no non-prime attributes 

Now,
 It is clear that there are no non-prime attributes in the relation. 
 In other words, all the attributes of relation are prime attributes.

164
 Thus, all the attributes on RHS of each functional dependency are prime
attributes. 
Thus, we conclude that the given relation is in 3NF.

4. Boyce-Codd Normal Form(BCNF)

A given relation is called in BCNF if and only if-


1. Relation should be in 3NF.
2. For each non-trivial functional dependency A → B, A is a super key of the
relation.

Example:
Consider a relation- R ( A , B , C ) with the functional dependencies-
A→B
B→C
C→A
The possible candidate keys for this relation are- A, B, C

Here A, B, C is super keys, so we can conclude that given relation is in BCNF

165
Fourth Normal Form:
A table is said to be in 4NF
If
1. It should be in BCNF
2. It should not have any Multi – valued dependency.
Multi – valued dependency:
1. For a dependency A →→ B, if for a single value of A, multiple values of B
exists.
2. Table should be at least 3 columns.
3. For a relation (A, B, C), If there is a Multi – valued dependency between
A →→ B, then B and C should be Independent of each other.

Example:

Sid cname hobby


1 DBMS Cricket
1 Java Tennis
2 C Dancing
2 C++ Cricket

sid cname sid hobby


1 DBMS 1 Cricket
1 Java 1 Tennis
2 C 2 Dancing
2 C++ 2 Cricket

166
Fifth normal form:
A database is said to be in 5NF, if and only if,
 It should be in 4NF
 If we can decompose table further to eliminate redundancy and anomaly,
and when we re-join the decomposed tables by means of candidate keys, we
should not be losing the original data or any new record set should not arise.
In simple words, joining two or more decomposed table should not lose
records nor create new records. Or
 It should not have join dependency
5NF is also known as Project-join normal form (PJ/NF).
Join dependency:
Let ‘R’ be a relation schema and R1, R2 ….Rn be the decompositions of R, R is said
to satisfy the join dependency *(R1, R2… Rn) if and only if
∏R1(R) ⋈∏R2(R) ⋈………∏RN(R) =R

Example:

167
Now, each of combinations is in three different tables. If we need to identify who
is teaching which subject to which semester, we need join the keys of each table
and get the result.
For example, who teaches Physics to Semester 1, we would be selecting Physics
and Semester1 from table 3 above, join with table1 using Subject to filter out the
lecturer names. Then join with table2 using Lecturer to get correct lecturer name.
That is we joined key columns of each table to get the correct data. Hence there is
no lose or new data – satisfying 5NF condition.

168

You might also like