0% found this document useful (0 votes)
34 views55 pages

DBMS Unit 5 HWN

Uploaded by

bharathpatel612
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)
34 views55 pages

DBMS Unit 5 HWN

Uploaded by

bharathpatel612
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/ 55

Unit-III

SQL: QUERIES, CONSTRAINTS, TRIGGERS: form of basic SQL query, UNION,


INTERSECT, and EXCEPT, Nested Queries, aggregation operators, NULL values, complex
integrity constraints in SQL, triggers and active data bases.
Schema Refinement: Problems caused by redundancy, decompositions, problems related to
decomposition, reasoning about functional dependencies, FIRST, SECOND, THIRD normal
forms, BCNF, lossless join decomposition, multi-valued dependencies, FOURTH normal form,
FIFTH normal form.
SQL: SQL (structured query language) is most widely used commercial database language. It
was developed by IBM in 1970’s, it has several aspects:
DDL(Data Definition Language): this subset of SQL supports the creation, deletion,
modification of tables and views, allows to specify the integrity constraints on tables.
DML(Data Manipulation Language): it allows the user to pose the queries and to insert, delete
and modify the rows on a table.
Embedded and Dynamic SQL: Embedded SQL features allows user to call SQL code from a
host language such as C or COBOL. Dynamic SQL allows a query to construct at runtime.
Trigger: the new SQL1999 standard supports the triggers. Which are the actions executed by
DBMS whenever changes to the database meet the conditions specified in the trigger.
Security: SQL provides the mechanisms to control access to data objects such as tables and
views.
Transaction management: it allows a user to explicitly control how a transaction is to be
executed.
Form of a Basic SQL Query:

SQL is a programming language for Relational Databases. It is designed over relational algebra
and tuple relational calculus. SQL comprises both data definition and data manipulation
languages. Using the data definition properties of SQL, one can design and modify database
schema, whereas data manipulation properties allows SQL to store and retrieve data from
database.
The basic form of SQL query is
SELECT [Distinct] Select-list
FROM From-list
WHERE Condition;
Example: SQL Query for to get the names of students who got marks above 60.
SELECT sname
From student
Where marks>60
This query corresponds to a relational algebra expression involves selection, projection, and
cross product.
The SELECT clause specifies which columns to be retained in the result.
Select-list: it specifies the list of column names.
The FROM clause specifies the cross product of tables
From-list: list of table names.
WHERE clause specifies the selection condition on the tables mentioned in the FROM clause.
Conceptual evaluation strategy:
 Computes the cross product of tables in the from-list.
 Deletes the rows from the cross product that fails the condition.
 Delete the column that does not appear in the select-list.
 Eliminates the duplicate rows.
In this we wrote the queries using 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)
Id sname rating Age Sid bid Day
22 Dustin 7 45.0 22 101 10/10/98
29 Brutus 1 33.0 22 102 10/10/98
31 Lubber 8 55.5 22 103 10/8/98
32 Andy 8 25.5 22 104 10/7/98
58 Rusty 10 35.0 31 102 11/10/98
64 Horatio 7 35.0 31 103 11/6/98
71 Zorba 10 16.0 31 104 11/12/98
74 Horatio 9 35.0 64 101 9/5/98
85 Art 3 25.5 64 102 9/8/98
95 Bob 3 63.5 74 103 9/8/98
Fig instance of sailors s3
Figure: An Instance R2 of Reserves

bid bname color


101 Interlake blue
102 Interlake Red
103 Clipper green
104 Marine Red
Fig: Instance of boats b1
Examples of Basic SQL queries:
1. Find the names and ages of all sailors.
SELECT S.sname, S.age
FROM Sailors S
2. Find all sailors with rating above 7?
SELECT *
FROM Sailors S
WHERE S.rating>7;
3. Find the names of sailors who reserved boat number 103?
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid= R.sid AND R.bid=103;
4. Find Sid’s of sailors who reserved a red boat?
SELECT Sid
FROM Reserves, Boats
WHERE Rserves.bid= Boates.bid AND Boats. color=’red’;
Or
SELECT S.sid
FROM Reserves R, Boats B
WHERE R.bid= B.bid AND B. color=’red’;
5. Find the colors of boats reserved by sailor name Lubber?
SELECT B. color
FROM Reserves R, Boats B, Sailors S
WHERE S.sid=R.sid AND R.bid=B.bid AND S.sname= ‘Lubber’;
6. Find the Names of sailors who have reserved at least one boat?
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid= R.sid;
7. Find the names of sailors who have reserved at least two boats on same day?
SELECT S.sname
FROM Sailors S, Reserves R1, Reserves R2
WHERE (S.sid=R1.sid AND R1.sid=R2.sid AND R1.bid < > R2.bid AND R1.day=
R2.day);
8. Find the ages of sailors whose name begins and ends with B and has at least three characters.
SELECT S.age
FROM Sailors S
WHERE S.sname LIKE `B %B'.
9. Compute increments for the ratings of persons who have sailed two different boats on the
same day.
SELECT S.sname, S.rating+1 AS rating
FROM Sailors S, Reserves R1, Reserves R2
WHERE S.sid = R1.sid AND S.sid = R2.sid
AND R1.day = R2.day AND R1.bid <> R2.bid
Set operators:
SQL provides three set manipulation constructs (UNION, INTERSECTION, EXCEPT) that
extend the basic form of sql query, these constructs are used when answer to query is multiset of
rows. SQL also provides other set operations: IN, op ANY, op ALL, EXIST, NOT IN, NOT
EXIST. These operators are used to join the results of two (or more) SELECT statements
UNION: this UNION operator, it returns the combined result from all the compounded
SELECT queries, after removing all duplicates and in sorted order (ascending by default),
without ignoring the NULL values.
Example: Find the names of sailors who have reserved a red or a green boat.
SELECT 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.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = `green'.
Example: Find all Sid’s of sailors who have a rating of 10 or have reserved boat 104.
SELECT S.sid
FROM Sailors S
WHERE S.rating = 10
UNION
SELECT R.sid
FROM Reserves R
WHERE R.bid = 104
INTERSECT: INTERSECT operator, displays the common rows from both the SELECT
statements, with no duplicates and data arranged in sorted order.
Example: Find the names of sailors who have reserved a red and a green boat.
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = `red'
INTERSECT
SELECT S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = `green'
EXCEPT: It displays the rows which are present in the first query but not presented in the
second query, with no duplicates and data arranged in ascending order
Example: Find the Sid’s of all sailors who have reserved red boats but not green boats.
SELECT S.sid
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = `red'
EXCEPT
SELECT S2.sid
FROM Sailors S2, Reserves R2, Boats B2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = `green'
Nested Queries: A nested query is a query that has another query embedded within it; the
embedded query is called a sub query. In this inner query is executed first later the outer query
will be executed. A sub query typically appears within the WHERE clause of a query, sometimes
appear in the FROM clause or the HAVING clause.
Example: Find the names of sailors who have reserved boat 103.
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid = 103)

Example: Find the names of sailors who have reserved a red boat.
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' )
Example: Find the names of sailors who have not reserved a red boat.
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' )
Correlated Nested Queries:
Correlated sub queries are used for row-by-row processing. Each sub query is executed once for
every row of the outer query.
Example: 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)
Set-Comparison Operators:
SQL support the set operators EXIST, IN, and UNIQUE it also supports op ANY and op ALL,
where op is one of the arithmetic comparison operators (<;<=;=; <>;>=;>). SOME is also
available, but it is just a synonym for ANY.
The ANY and ALL operators are used with a WHERE or HAVING clause.
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.
SOME: This operator is used to compare a value with a single column set of values returned by
the subquery. The SOME operator in SQL must match at least one value in a subquery and that
value must be preceded by comparison operators.

Generally we will use this SOME operator in WHERE clause to check whether the required
column values are matching with the set of values returned by subquery or not.

Example: write a Query to find the sailors whose rating of is greater than 8.

SELECT * FROM Sailors WHERE rating> SOME


(SELECT Sid FROM sailors WHERE rating >8)
Example: Find sailors whose rating is better than some sailor called Horatio.
SELECT S.sid
FROM Sailors S
WHERE S.rating > ANY (SELECT S2.rating
FROM Sailors S2
WHERE S2.sname = `Horatio’)
The sub query computes all rating values in sailors. The outer query WHERE condition is
satisfied when S.rating is greater than or equals to each of these rating values.
Example: find the sailors with highest rating.
SELECT S.sid
FROM Sailors S
WHERE S.rating >= ALL (SELECT S2.rating
FROM Sailors S2)
Example: find the names of sailors who has reserved a red and green boat.
SELECT S3.sname
FROM Sailors S3
WHERE S3.sid IN ((SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid = B.bid AND B.color = `red’)
INTERSECT
(SELECT R2.sid
FROM Boats B2, Reserves R2
WHERE R2.bid = B2.bid AND B2.color = `green’))
Or
SELECT S3.sname
FROM Sailors S3
WHERE S3.sid IN (( SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid = B.bid AND B.color = `red' )
INTERSECT
(SELECT R2.sid
FROM Boats B2, Reserves R2
WHERE R2.bid = B2.bid AND B2.color = `green' ))
Example: Find the names of sailors who have reserved all boats.
SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS (( SELECT B.bid
FROM Boats B )
EXCEPT
(SELECT R.bid
FROM Reserves R
WHERE R.sid = S.sid ))
Aggregate Operators:
These operators are used to perform the calculation on the multiple rows of the column of a
table. SQL supports five aggregate operations
COUNT (A): it returns the number of (unique) values in column A.
Example: Count the number of sailors.
SELECT COUNT (*)
FROM Sailors S
Example: Count the number of different sailor names.
SELECT COUNT (DISTINCT S.snmae)
FROM Sailors S
SUM (A): it returns the sum of all the values in column A.
Example: Find the sum of ages of all sailors.
SELECT SUM (S.age)
FROM Sailors S
AVG (A): it returns the the average of all values in column A.
Example: Find the average age of sailors with a rating of 10.
SELECT AVG (S.age)
FROM Sailors S
WHERE S.rating = 10
MAX (A): it returns the maximum value in the column A.
Example: Find the name and age of the oldest sailor.
SELECT S.sname, MAX (S.age)
FROM Sailors S
Or
SELECT S.sname, S.age
FROM Sailors S
WHERE S.age = (SELECT MAX (S2.age)
FROM Sailors S2 )
MIN (A): it returns the minimum value in column A.
Ex: Find the name and age of the youngest sailor.
SELECT S.sname, MIN (S.age)
FROM Sailors S
Example: Find the names of sailors who are older than the oldest sailor with a rating of 10.
SELECT S.sname
FROM Sailors S
WHERE S.age > ( SELECT MAX ( S2.age )
FROM Sailors S2
WHERE S2.rating = 10)
Group by Having Clause:
The aggregate operators can be applied on all the rows of a relation. We want to apply aggregate
operations on groups of rows in relation then this having clause can be used.
The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.
ORDER BY:
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example: find the age of the youngest sailor for each rating level.
SELECT S.rating, MIN (S.age)
FROM Sailor S
Group By S.rating
HAVING SID>58
Example: find the age of the youngest sailor who is eligible to vote for each rating level with
at least two such categories in saaending order.
SELECT S.rating, MIN(S.age) AS minage
FROM Sailors S
WHERE S.age>18
GROUP BY S.rating
HAVING COUNT(*)>1
ORDER By Sid.
Example: for each red boat, find the number of reservations this boat.
SELECT B.bid, COUNT (*) AS reservation count
FROM Boats B, Reserves R
WHERE R.bid==B.bid
GROUP BY B.bid
Having B.color=’red’
Example: find those ratings for which the average age of sailors is the minimum over the
all ratings.
SELECT S.rating
FROM Sailors S
WHERE AVG (S.age) = (SELECT MIN(S2.age))
FROM Sailors S2
Group By S2.rating)
Null Values:
A field with a NULL value is a field with no value. If a field in a table is optional, it is possible
to insert a new record or update a record without adding a value to this field. Then, the field will
be saved with a NULL value.
We can disallow null values by specifying NOT NULL as part of field definition for example
Sname CHAR(20) NOT NULL. There is an implicit NOT NULL constraint for every field listed
in a PRIMARY KEY Constraint.
IS NULL and IS NOT NULL operators used to test the null values.
Syntax: IS NULL
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Syntax: IS NOT NULL
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
Example find the Sid, Sname of sailors whose name is NULL.
SELECT Sid, Sname
FROM Sailors
WHERE Sname IS NULL;
Joins:
A SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them.

Example

Consider the following two tables.


Table 1 − CUSTOMERS Table is as follows.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00
Table 2 − ORDERS Table is as follows.
+-----+---------------------+-------------+--------+
| OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060

Different types of Joins are:


 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 FULL JOIN

INNER JOIN: The INNER JOIN creates a new result table by combining column values of two
tables (table1 and table2) based upon the join-predicate.

SELECT ID, NAME, AMOUNT, DATE


FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

ID | NAME | AMOUNT | DATE |


+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+--------------------
LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
This means that if the ON clause matches 0 (zero) records in the right table; the join will still
return a row in the result, but with NULL in each column from the right table.

SELECT ID, NAME, AMOUNT, DATE


FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
This would produce the following result
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL

RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left
table. This means that if the ON clause matches 0 (zero) records in the left table; the join will
still return a row in the result, but with NULL in each column from the left table.
This means that a right join returns all the values from the right table, plus matched values from
the left table or NULL in case of no matching join predicate
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00
FULL JOIN: combines the results of both left and right outer joins.
The joined table will contain all records from both the tables and fill in NULLs for missing
matches on either side.
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
FULL JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
This would produce the following result
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00
Complex integrity constraints in SQL:
Integrity constraints over a single table:
We can specify complex integrity constraints over a single table using table constraints, which
have the form CHECK conditional-expression. When a row is inserted into table or an Existing
row is modified, the conditional expression in CHECK constraint is evaluated. If it evaluates to
false, the command is rejected.
Example:
Ensure that rating must be an integer in the range 1 to 10 only.
CREATE TABLE Sailors( sid INTEGER,
Sname CHAR(10),
Rating INTEGER,
Age REAL, PRIMARY KEY (Sid),
CHECK (rating >=1 AND rating <=10));
Restrict values:
Example: enforce the constraint that Interlake boats cannot be reserved
CREATE TABLE Reserves (Sid INTEGER,
Bid INTEGER (10),
Day DATE,
FOREIGN KEY (sid) REFERENCES Sailors,
FOREIGN KEY (bid) REFERENCES Boats,
CONSTRAINT noInterlakes
CHECK (‘Interlake’ < > (SELECT B.bname
FROM Boats B
WHERE B.bid=Reserves.bid)))
Domain Constraints and distinct types:
A user can define a new domain using the CREATE DOMAIN statement, which uses check
constraints.
CREATE DOMAIN ratingval INTEGER DEFAULT 1
CHECK (VALUE >=1 AND VALUE <=10)
Assertions:
Table constraints are associated with a single table, although the conditional expression in the
CHECK clause can refer to other tables. Table constraints are required to hold only if the
associated table is non empty. Thus when a constraint involves two or more tables, the table
constraint mechanism is not quite desired. To cover such situations, SQL supports the creation of
assertions. Assertions are constraints not associated with any one table.
For example, suppose that we wish to enforce the constraint that the number of
boats plus the number of sailors should be less than 100.
When a constraint involves two or more tables assertions are used.

Ex: CREATE TABLE Sailors (Sid INTEGER,


Sname CHAR (10),
Rating INTEGER,
Age REAL,
PRIMARY KEY (sid),
CHECK (rating>=1 AND rating <=10)
CHECK ((SELECT COUNT (s.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM
Boats B) < 100));
This solution is suffers from two draw backs, it iis associated with sailors, although it involves
boats in a completely symmetric way. More important is if the sailors table is empty, this
constraint is defined to hold, even if we have more than 100 rows in Boats! We could extend this
constraint specification to check that Sailors is nonempty but this approach becomes
cumbersome. The best solution is creating an assertion as follows:
CREATE ASSERTION SmallClub
CHECK ((SELECT COUNT (S.sid) FROM Sailors S)
+ (SELECT COUNT (B.bid) FROM Boats B) < 100)
Example2: the salary of employee must not be greater than the salary of the manager of the
department that the employee works for.
CREATE ASSERTION SALARY_CONSTRINT
CHECK (NOT EXISTS (SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT
D
WHERE (E.SALARY> M. SALARY AND E. DNO=D.NUMBER AND d. MGRSSN=M.SSN))
Triggers and active databases:
A trigger is a stored procedure in database which is automatically invoked in response to
specified changes to the database. The database that has a set of associated triggers is called an
active database.
Triggers are invoked when the database is modified, when a row is inserted into a specified table,
when column of certain table is updated.
A condition in a trigger is true or false statement. It equals to true then the action associated with
the trigger is executed.
Trigger description consists of three parts.
Event: A change to the database that activates the trigger. Trigger monitors’ the database and is
executed when a database is modified in way that matches the event specification.
Condition: It is a true or false statement, when the condition evaluates to true, the action
associated the trigger is activated.
Action: A procedure that is executed when the trigger is activated and condition is true.
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;
Example: Database contains the relation student (Sid, sname, Subject1, Subject2, Subject3,
total ), create trigger to compute total when insert the subject values into a student relation.
Create Trigger T1
BEFORE INSERT
ON Student
For each row
Set new.total = new.subject1 + new.subject2 + new.subject3;

Example: Database contains the CUSTOMERS table (id, name, age, address, salary),
creates a row-level trigger for the customers table to display the salary difference between
the old values and new values, that would fire for INSERT or UPDATE or DELETE
operations performed on the CUSTOMERS table.

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/

Drop Trigger: we can remove the trigger on database using drop command.
Syntax: DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name
Example: DROP TRIGGER [IF EXISTS] [customers] display_salary_changes
Active Databases:
Active Database is a database consisting of set of triggers. These databases are very difficult to be
maintained because of the complexity that arises in understanding the effect of these triggers. In
such database, DBMS initially verifies whether the particular trigger specified in the statement that
modifies the database is activated or not, prior to executing the statement.
If the trigger is active then DBMS executes the condition part and then executes the action part
only if the specified condition is evaluated to true. It is possible to activate more than one trigger
within a single statement.
In such situation, DBMS processes each of the trigger randomly. The execution of an action part of
a trigger may either activate other triggers or the same trigger that Initialized this action. Such
types of trigger that activates itself is called as ‘recursive trigger’. The DBMS executes such chains
of trigger.

Features of Active Database:


 It possess all the concepts of a conventional database i.e. data modeling facilities, query
language etc.
 It supports all the functions of a traditional database like data definition, data
manipulation, storage management etc.
 It detects event occurrence.
 It must be able to evaluate conditions and to execute actions.
 It means that it has to implement rule execution.
 Enhances traditional database functionalities with powerful rule processing capabilities.
 Enable a uniform and centralized description of the business rules relevant to the
information system.
 Suitable platform for building large and efficient knowledge base and expert systems.
Schema Refinement: It is the last step before considering physical design; it checks the tables
for redundancies and addresses the problems caused by redundancy based on decomposition.
Redundancy means storing the multiple copies of same data in the database, it leads to several
problems.

For example consider Hourly_Emps relation, ssn is the key for this relation, and hourly wages
attribute is determined by rating attribute. That is, for a given rating value, there is only one
permissible hourly_wages value. This integrity constraint is an example of functional
dependency. It leads to possible redundancy in the relation Hourly_Emps.

Redundant storage: the rating value 8 corresponds to the hourly wage 10, this association
repeated three times, the rating value 5 corresponds to the hourly wage 7, this association
repeated two times.

Fig: instance of Hourly_Emps relation

Problems caused due to redundancy are:


Insertion anomalies: it may not possible to store certain information unless some other,
unrelated, information is stored as well.
For example we cannot insert a tuple for an employee unless we know the hourly wages for the
employee’s rating value.
Deletion anomalies: it may not possible to delete certain information without losing some other,
unrelated, information as well.
For example if we delete all tuples with a given rating value we lose the association between that
rating value and its hourly_wage value.
Update anomalies: if one copy of such repeated data is updated, then database will be in
inconsistent state, unless all copies are similarly updated.
For example the hourly_wages in the first tuple (row) could be updated without making a similar
change in the second tuple.

Decompositions: Many problems arising from redundancy can be addressed by replacing a


relation with collection of smaller relations. This decomposition is used to eliminate the
redundancy from relation and to achieve heir normal form.

A decomposition of a relational schema R consists of replacing the relation schema by two(or


more) relation schemas that each contains a subset of the attribute of R and together include all
attributes in R.

Example: we can decompose Hourly_Emps into two relations:

hourly_emps2 (ssn,name,lot,rating,hpurs_worked) and wages( rating, hourly_wages). Where ssn


number acts as primary key for relation hourly_emps2, rating acts as primary key for wages
relation.

Fig: instance of Hourly_Emps relation


Fig: instance of Hourly_Emps2 and Wages relations

Problems related to decomposition: decomposition relation schema can create more problems
than it solve. So we have to understand when we normally decompose a table into n number of
sub tables we have to realize that what the importance of doing decomposition is and problems
that might be we may if we do decomposition.

Two questions must be asked to do decomposition


1. Do we need to decompose a relation?
Several normal forms have been proposed for the relations, the normal form of given relation
schema help us to decide whether or not to decompose it further. If we decide that a relation
schema must be decomposed further, we must choose a particular decomposition.
2. What problems does a given decomposition cause?
Decomposition of a relation is done when a relation in relational model is not in appropriate
normal form. Relation R is decomposed into two or more relations if decomposition is lossless join
as well as dependency preserving.
Two properties of decomposition are important.

Lossless-join decomposition: A decomposition {R1,R2,……..Rn} of relation R is called loss


less decomposition if the natural join of {R1,R2,……..Rn} produces exactly the relation R.
Decomposition is lossless if R1 ⋈ R2 = R

A decomposition is to be lossless it must hold the following conditions.

1. Union of Attributes of R1 and R2 must be equal to attribute of R. Each attribute of R


must be either in R1 or in R2.
Att(R1) U Att(R2) = Att(R)
2. Intersection of Attributes of R1 and R2 must not be NULL.
Att(R1) ∩ Att(R2) ≠ Φ
3. Common attribute must be a key must be super key or candidate key of either of the
relation R1 or relation R2 or both R1 and R2
Att(R1) ∩ Att(R2) -> Att(R1) or Att(R1) ∩ Att(R2) -> Att(R2)

Example: Relation R(ABC) is decomposed into R1(AB) and R2(BC) check whether the
decomposition is lossy or loss less decomposition.

R(ABC)=

A B C
1 2 1
2 2 2
3 3 2
R1(AB)=

A B
1 2
2 2
3 3

R2(BC)=

B C
2 1
2 2
3 2

R1 X R2=

A B B C
1 2 2 1
1 2 2 2
1 2 3 2
2 2 2 1
2 2 2 2
2 2 3 2
3 3 2 1
3 3 2 2
3 3 3 2
R1 ⋈ R2 =

A B C
1 2 1
1 2 2
2 2 1
2 2 2
3 3 2
R1 ⋈ R2 != R, This decomposition is lossy decomposition.

Example: Relation R(ABC) is decomposed into R1(AB) and R2(AC) check whether the
decomposition is lossy or loss less decomposition.

R(ABC)=

A B C
1 1 1
2 1 2
3 2 1
4 3 2
R(ABC) is decomposed into R1(AB) and R2(AC)

R 1=

A B
1 1
2 1
3 2
4 3
R2=

A C
1 1
2 2
3 1
4 2
R1 ⋈ R2 = R

A B C
1 1 1
2 1 2
3 2 1
4 3 2
This decomposition is loss less.

Dependency preserving decomposition:

The decomposition of relation schema R with set of FD’s, F into R 1 an R2 with FD’s F1 and F2
then this decomposition is said to be dependency preserving, the closure of set of functional
dependency set (F) is equals to the closure of the functional dependency sets F1 and F2.

The decomposition is said to be dependency preserving if F+=(F1 U F2)+ .


Example: A relation R (A, B, C, D) with FD set{AB, BC, CD, D B} is decomposed
into R1(AB) and R2(BC) ,R3(BD) check this decomposition dependency preserved or not.
The R (A, B, C, D)
Set of FD’s F= {AB, BC, CD, D B}
F+= {AB, BC, CD, DB, BD,CB}
R is decomposed into R1(AB) , R2(BC), R3(BD).
From R1(AB) we can derive functional dependency set F1={ AB}
From R2(BC) we can derive functional dependency set F 2={ BC , CB(from CD and D
B)}
From R3(BD) we can derive functional dependency set F3={ DB, B D(from BC and CD
),}
(F1 U F2 U F3)+ = {AB, BC , CB, B D, DB} = F+
This decomposition is dependency preserved decomposition.
Example: A relation R (A, B, C, D) with FD set{AB, CD } is decomposed into R1(AC)
and R2(BD) check this decomposition dependency preserved or not.
Relation R (A, B, C, D) and
The set of Functional dependencies of relation R is F= {AB, CD}
R is decomposed into R1 (A,C) and R2(BD)
From R1(AC) we can derive functional dependency set F1={ }
From R2(BD) we can derive functional dependency set F2 ={}
(F1 U F2)+= { } != F+
This decomposition is not dependency preserved.
Functional Dependency:
The functional dependency is a relationship that exists between two attributes. It typically exists
between the primary key and non-key attribute within a table. Functional Dependency is
represented by  (arrow sign).
If column X of a table uniquely identifies the column Y of same table then it can represented as
XY (Attribute Y is functionally dependent on attribute X, here attribute X determinant, Y is
dependent)
A functional dependency XY in a relation holds if two tuples having same value of attribute X
also have same value for attribute Y.
For Example, in relation STUDENT shown in table 1, Functional Dependencies
STUD_NO->STUD_NAME, STUD_NO->STUD_PHONE hold
But
STUD_NAME->STUD_ADDR do not hold

Types of Functional dependency

Trivial functional dependency

A functional dependency A → B has trivial functional dependency if B is a subset of A.

Example:
Consider a table with two columns Employee_Id and Employee_Name.
{Employee_id, Employee_Name} → Employee_Id is a trivial functional dependency as
Employee_Id is a subset of {Employee_Id, Employee_Name}.
Also, Employee_Id → Employee_Id and Employee_Name → Employee_Name are trivial de
pendencies too.
2. Non-trivial functional dependency
A → B has a non-trivial functional dependency if B is not a subset of A.

When A intersection B is NULL, then A → B is called as complete non-trivial.

Example:

ID → Name,
Name → DOB

Functional Dependency Set: Functional Dependency set or FD set of a relation is the set of all
FDs present in the relation.

For Example, FD set for relation STUDENT shown in table 1 is:


{STUD_NOSTUD_NAME, STUD_NOSTUD_PHONE, STUD_NOSTUD_STATE,
STUD_NOSTUD_COUNTRY,
STUD_NO  STUD_AGE, STUD_STATESTUD_COUNTRY}
Attribute Closure: Attribute closure of an attribute set can be defined as set of attributes which
can be functionally determined from it.
To find attribute closure of an attribute set:
 Add elements of attribute set to the result set.
 Recursively add elements to the result set which can be functionally determined from the
elements of the result set.
Using FD set of table 1, attribute closure can be determined as:
(STUD_NO)+ = {STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE, STUD_COUNTRY,
STUD_AGE}
(STUD_STATE)+ = {STUD_STATE, STUD_COUNTRY}
Candidate Keys and Super Keys using Attribute Closure
Super key: If attribute closure of an attribute set contains all attributes of relation, the attribute
set will be super key of the relation.
Candidate Key: If no subset of this attribute set can functionally determine all attributes of the
relation, the set will be candidate key as well.
For Example, using FD set of table 1,

(STUD_NO, STUD_NAME)+ = {STUD_NO, STUD_NAME, STUD_PHONE,


STUD_STATE, STUD_COUNTRY, STUD_AGE}

(STUD_NO)+ = {STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,


STUD_COUNTRY, STUD_AGE}
(STUD_NO, STUD_NAME) will be super key but not candidate key because its subset
(STUD_NO)+ is equal to all attributes of the relation. So, STUD_NO will be a candidate key.
Prime and non-prime attributes
Attributes which are parts of any candidate key of relation are called as prime attribute, others are
non-prime attributes.
For Example, STUD_NO in STUDENT relation is prime attribute, others are non-prime attribute.
Example: relation of attributes R(ABCD) , set of functional dependencies F={AB, BC,
CD} ,then find the candidate key of relation.
Given set of FD’s are F={AB, BC, CD}
To find the candidate key we should find the closure of all the attributes of relation based on
functional dependies. Which attribute of relation R functionally determined all the attributes of the
relation is candidate key for the Relation R.
Attribute closure of A is A+= {A,B,C,D} ( AB, BC then AC, AC and CD then AD)
Attribute closure of B is B+= {BCD}
Attribute closure of C is C+= {C,D}
Attribute closure of D is D+= {D}
So Attribute A is candidate key for the relation R.
Prime attribute is A, Non prime attributes are BCD.
Example: Relation R(ABCDEFGH) of set of functional dependencies F={ABC, BCDE,
CFG, DF} find the
Given set of FD’s are
F= {ABC, BCDE, CFG, DF}
Attribute closure of A (A+) = { A,B,C,D,E,F,G}
Attribute closure of D (D+) = {D,F }
Attribute closure of BC (BC+) = {B,C,D,E,F,G }
Example: Relation R(ABCD) of set of functional dependencies F={AB, BC, CD,
DA} find the candidate key’s and prime attributes for the relation R.
Given set of FD’s are
F= {AB, BC, CD, DA}
To find the candidate key’s we should find attribute closure based on given functional
dependencies.
Attribute closure of A (A+) = {A,B,C,D }, Attribute closure of B (B+) = { B, C, D,A}
Attribute closure of C (C+) = {C,D,A,B}, Attribute closure of D (D+) = {D,A, B, C}
Then candidate key of Relation R = {A, B, C, D}, Prime attributes of relation R= {A, B, C, D}
Example: Relation R(ABCDE) of set of functional dependencies F={AB, BCD, EC,
DA} find the candidate key’s and prime attributes for the relation R.
Given set of FD’s are
F= {AB, BCD, EC, DA}
To find the candidate key’s we should find attribute closure based on given functional
dependencies.
Note: Here attribute E is not in the part of R.H.S of the functional dependencies, so each candidate
key a relation must contain that attribute E.
Attribute closure (ABCDE+) = { A, B, C, D, E}
Attribute closure (ABC+)={A,B,C,D}
Attribute closure (CDE+)={C,D,E,A,B}
Attribute closure (BCE+)={B,C,E,D,A}
Attribute closure (BDE+)={B,D,E,C,A}
Attribute closure (ABE+)={A,B,E,C,D}
Attribute closure (AB+)={A,B}
Attribute closure (BC+)={B,C,D,A}
Attribute closure (CD+)={C,D,A,B}
Attribute closure (DE+)={D,E,A,B,C}
Attribute closure (BE+)={B,E,C,D,A}
Attribute closure (AE+)={A,E,C,B,D}
Attribute closure (A+)={A,B}
Attribute closure (B+)={B}
Attribute closure (C+)={C}
Attribute closure (D+)={D,A,B}
Attribute closure (E+)={E,C}
Super keys={(ABCDE), (CDE),( BCE), (BDE.),( ABE),(AE),(BE),(DE)}
Candidate keys={(AE),(BE),(DE)}
Prime attributes={A,E,B,D}
Non Prime attributes= {C}
Attribute closure of AE (AE+) = {A, B, C, D, E}
Attribute closure of BE (BE+) = {A, B, C, D, E}
Attribute closure of CE (CE+) = {A, B, C, D, E}
AE,BE,CE are the candidate keys.
Example: Relation R(ABCDE) of set of functional dependencies F={AB, DE} find the
candidate key’s and prime attributes for the relation R.
Given set of FD’s are
F= {AB, DE}

Example: Consider the relation scheme R = {E, F, G, H, I, J, K, L, M, N} and the set of


functional dependencies {{E, F} {G}, {F}  {I, J}, {E, H}  {K, L}, K  {M}, L  {N}?
Finding attribute closure of all given options, we get:
{E,F}+ = {EFGIJ}
{E,F,H}+ = {EFHGIJKLMN}
{E,F,H,K,L}+ = {{EFHGIJKLMN}
{E}+ = {E}
{EFH}+ and {EFHKL}+ results in set of all attributes, but EFH is minimal. So it will be candidate
key.

To check whether an FD AB can be derived from an FD set F,


Find (A)+ using FD set F.
If B is subset of (A)+, then AB is true else not true.
Closure of set of functional dependencies (F+): closure of set of functional dependencies F+, is
set of all FD’s that include F as well as all dependencies that can be inferred from F

Armstrong Axioms: The Armstrong axioms refer the set of inference rules. These are used to find
the closure of set of functional dependencies (F+), from the given set of functional dependencies
(F).

The Functional dependency has 6 types of inference rules or axioms:


1. Reflexivity: if Y is a subset of X, then X determines Y. If X ⊇ Y then X → Y
Example:
X = {a, b, c, d, e}
Y = {a, b, c}
2. Augmentation: 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
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
4. Union: 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
5. Decomposition
Decomposition rule is also known as project rule. It is the reverse of union rule.
This Rule says, if X determines Y and Z, then X determines Y and X determines Z separately.
If X → YZ then X → Y and X → Z
6. Pseudo transitivity
In Pseudo transitive Rule, if X determines Y and YZ determines W, then XZ determines W.
If X → Y and YZ → W then XZ → W
Example: Relation has attributes R(ABCGHI) , set of functional dependencies of the relation
are F={AB, AC, CGH,CGI, BH} find the closure of set of functional
dependencies for relation R.

Given set of functional dependencies (F) = {AB, AC, CGH, CGI, BH}

Here we have
AB, and we also have BH then we can write AH (Transitivity)
CGH, and CGI, then we can write CG HI (Union)
AC, CGH, then we can write AGH (Pseudo transitivity)
AC, CGI, then we can write AGI (Pseudo transitivity)

Closure of set of functional dependencies (F+)= {AB, AC, CGH, CGI, BH, AH ,
CG HI, AGH, AGI }

Example: Relation has attributes R(ABCDEF) , set of functional dependencies of the relation
are F={AB, AC, CDE,CDF, BE} find the closure of set of functional
dependencies for relation R.
Given set of functional dependencies (F) = F={AB, AC, CDE,CDF, BE}
Here we have
AB and AC then we can write ABC(union)
CDE and CDF then we can write CDEF(union)
ABand BE then we can write AE(Transitivity)
AC, and CDE then we can write ADE (Pseudo transitivity)
AC, and CDF then we can write ADF (Pseudo transitivity)

Then closure of set of functional dependencies (F+)= { AB, AC, CDE,CDF, BE,
ABC, CDEF, AE, ADF, ADE }
Minimal cover or canonical cover or irreducible set of functional dependies:

To find the minimal set of functional dependencies we should follow these three rules

1. Single attribute in right hand side (R. H. S).


Ex: ABC ( it can be written as AB and AC )
2. Remove the extraneous attribute in left hand side (L.H.S)
Ex: ABC, AC (here B is extraneous attribute)
3. Remove the redundant functional dependencies.
Ex: AB, BC, AC (here AC is redundant because we can get A C from AB, BC
using transitivity)
Example: The relation R has attributes R (A, B, C), set of FD’s {ABC, BC, AB,
ABC}, find the canonical cover of functional dependencies.

Given set of functional dependencies


F= {ABC, BC, AB, ABC}
Step1: it should have the single attribute in RHS.
According rule one the functional dependencies can be written as
AB,
AC,
BC,
ABC
Step2: it should not have the extraneous attribute in left hand side (L.H.S).
ABC
To find the extraneous attribute we should find the closure of A and Closure of B
Closure of A is (A+) = {ABC}
Closure of B is (B+) = {BC}

Here attribute B is extraneous, because from attribute A we can get attribute B, but from
attribute B we can’t get attribute A.
To removing extraneous attribute B, the functional dependency AB C can be written as AC
After removing the extraneous attribute B the FD’s are
AB,
BC,
AC
Step3: we should remove the redundant FD’s
AB,
BC,
AC

Here AC is redundant because, we can get AC from {AB,


BC} (transitivity)
After removing this redundant functional dependency AC,
We have the functional dependencies
AB,
BC

The canonical cover of FD’s of relation R is

AB,
BC
Example2: find the canonical cover for the given set of functional dependencies
G = {AC, ABC,CDI, CDI, ECAB, EIC,AE}.

Given set of functional dependencies:


G = {AC, ABC, CDI, CDI, ECAB, EIC}

Step1: it should have the single attribute in RHS.


According rule one the functional dependencies can be written as
AC
ABC
CD
CI
CDI
ECA
ECB
EIC
AE
Step2: it should not have the extraneous attribute in left hand side (L.H.S).
ABC
CDI
ECA
EIC
ECB
ABC
To find the extraneous attribute we should find the closure of A and Closure of B

CLOSURE OF A {A+}={A,C,D,I,E,B}
CLOSURE OF B {B+}={B}
Here Attribute B Is Extranious.
AC
CDI

To find the extraneous attribute we should find the closure of C and Closure of D

CLOSURE OF C {C+} ={C,D,I}


CLOSURE OF B {D+}={ D}
Here Attribute D Is Extraneous
ECA

To find the extraneous attribute we should find the closure of C and Closure of D

CLOSURE OF E {E+} = { E}
CLOSURE OF C {C+}= {C,D,I }
HERE THERE IS N EXTRANIOUS ATTRIBUTE
EIC
To find the extraneous attribute we should find the closure of E and Closure of I

CLOSURE OF E {E+} = { E}
CLOSURE OF C {I+}= {I}
Here there is noextranious attribute

ECB
To find the extraneous attribute we should find the closure of E and Closure of I

CLOSURE OF E {E+} = { E}
CLOSURE OF C {C+}= {C,D,I}
HERE THERE IS N EXTRANIOUS ATTRIBUTE

STEP:3 we should remove the redundant FD’s


AC
AC
CI
EIC
ECB
ECA
After Applying Step3 We Got These Functional Dependencies
A-->I
EIC
ECAB

Example2: find the canonical cover for the given set of functional dependencies
F= {AD,EAD,BCAD,CB}

Example3: Find the canonical cover of F = { A  BC, B CE, A E, AC H, D B}

Normalization
Normalization is the process of organizing the data in the database. It is used to minimize the
redundancy from a relation or set of relations. Normalization divides the larger table into the
smaller table and links them using relationship.
Types of Normal Forms
First Normal Form (1NF):
A relation will be 1NF if it contains an atomic value. It states that an attribute of a table cannot
hold multiple values. It must hold only single-valued attribute, it doesn’t allows the multi-valued
attribute, composite attribute, and their combinations.

Example: Relation STUDENT is not in 1NF because of multi-valued attribute STUD_PHONE.

STUD_ID STUD_ NAME STUD_PHONE STUD _STATE STUD_COUNTRY


9716271721,
1 RAM HAYANA INDIA
9871717178
2 RAM 98982972881 PUNJAB INDIA

3 SURESH PUNJAB INDIA

The decomposition of the STUDENTT table into 1NF has been shown below:

STUD_ID STUD_ NAME STUD_PHONE STUD _STATE STUD_COUNTRY


1 RAM 9716271721 HAYANA INDIA
1 RAM 9871717178 HAYANA INDIA
2 RAM 98982972881 PUNJAB INDIA

3 SURESH PUNJAB INDIA


Second Normal Form (2NF):

To be in second normal form,


1. A relation must be in first normal form.
2. A relation must not contain any partial dependency.
A relation is in 2NF if it has No Partial Dependency, i.e., no non-prime attribute (attributes which
are not part of any candidate key) is dependent on any proper subset of any candidate key of the
table.
Partial Dependency – If the proper subset of candidate key determines non-prime attribute, it is
called partial dependency

Example 1 – Consider table-3 as following below.

STUD_NO COURSE_NO COURSE_FEE


1 C1 1000
2 C2 1500
1 C4 2000
4 C3 1000

4 C1 1000

2 C5 2000

{Note that, there are many courses having the same course fee.
Here,
COURSE_FEE cannot alone decide the value of COURSE_NO or STUD_NO;
COURSE_FEE together with STUD_NO cannot decide the value of COURSE_NO;
COURSE_FEE together with COURSE_NO cannot decide the value of STUD_NO;
Hence,
COURSE_FEE would be a non-prime attribute, as it does not belong to the one only candidate key
{STUD_NO, COURSE_NO} ;
But, COURSE_NO -> COURSE_FEE , i.e., COURSE_FEE is dependent on COURSE_NO,
which is a proper subset of the candidate key. Non-prime attribute COURSE_FEE is dependent on
a proper subset of the candidate key, which is a partial dependency and so this relation is not in
2NF.
To convert the above relation to 2NF, we need to split the table into two tables such as :
Table 1: STUD_NO, COURSE_NO and Table 2: COURSE_NO, COURSE_FEE

STUD_NO COURSE_NO COURSE_NO COURSE_FEE

1 C1 C1 1000
2 C2 C2 1500
1 C4 C4 2000
4 C3 C3 1000

4 C1 C1 1000

2 C5 C5 2000
Table 1: STUD_NO, COURSE_NO Table 2: COURSE_NO, COURSE_FEE
NOTE: 2NF tries to reduce the redundant data getting stored in memory. For instance, if there are
100 students taking C1 course, we dont need to store its Fee as 1000 for all the 100 records, instead
once we can store it in the second table as the course fee for C1 is 1000.

Example 2: Let's assume, a school can store the data of teachers and the subjects they teach. In a
school, a teacher can teach more than one subject.

TEACHER table

Teacher ID Subject Teacher Age


25 Chemistry 30
25 Biol0gy 30
47 English 35
83 Math 38
83 Computer 38

In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID which


is a proper subset of a candidate key. That's why it violates the rule for 2NF.
To convert the given table into 2NF, we decompose it into two tables:

TEACHER_DETAIL table:

Teacher ID Teacher Age


25 30
47 35
83 38

TEACHER_SUBJECT table:

Teacher ID Subject
25 Chemistry
25 Biol0gy
47 English
83 Math
83 Computer

3. Third Normal Form (3NF):


A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency. 3NF
is used to reduce the data duplication, to achieve the data integrity.

A relation is in 3NF if at least one of the following condition holds in every non-trivial function
dependency X –> Y
1. X is a super key.
2. Y is a prime attribute i.e., each element of Y is part of some candidate key.
Transitive dependency – If A->B and B->C are two FDs then A->C is called transitive
dependency.
Example 1 – In relation STUDENT

STUD_
STUD_ID STUD _ STATE STUD_ COUNTRY STUD_ AGE
NAME
1 RAM HAYANA INDIA 20
2 RAM PUNJAB INDIA 19
3 SURESH PUNJAB INDIA 21
FD set: {STUD_ID -> STUD_NAME, STUD_ID -> STUD_STATE, STUD_STATE ->
STUD_COUNTRY, STUD_ID -> STUD_AGE}
Candidate Key: {STUD_ID}
For this relation, STUD_ID -> STUD_STATE and STUD_STATE -> STUD_COUNTRY are
true. So STUD_COUNTRY is transitively dependent on STUD_ID. It violates the third normal
form.
To convert it in third normal form, we will decompose the relation STUDENT into two relations.
STUDENT (STUD_ID, STUD_NAME, STUD_STATE, STUD_AGE)
STUD_
STUD_ID STUD _ STATE STUD_ AGE
NAME
1 RAM HAYANA 20
2 RAM PUNJAB 19
3 SURESH PUNJAB 21

STATE_COUNTRY (Stud_id, COUNTRY)


STUD _Id STUD_ COUNTRY
1 INDIA
2 INDIA
3 INDIA

Boyce- Codd Normal Form (BCNF):


A relation R is in BCNF, if it is in 3NF, for every non-trivial functional dependency X –> Y, X is
a candidate key or super key of relation R.
Example: Let's assume there is a company where employees work in more than one department.
Employee table:
EMP_ID EMP_COUNTRY EMP_DEPT DEPT_TYPE EMP_DEPT_NO
264 INDIA Designing D394 283
264 INDIA Testing D394 300
364 UK Stores D283 232
364 UK Developing D283 549
In the above table Functional dependencies are as follows:
EMP_ID → EMP_COUNTRY
EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

Candidate key: {EMP-ID, EMP-DEPT}


The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.

To convert the given table into BCNF, we decompose it into three tables:

EMP_COUNTRY table:

EMP_ID EMP_COUNTRY
264 INDIA
364 UK

EMP_DEPT table:

EMP_DEPT DEPT_TYPE EMP_DEPT_NO


Designing D394 283
Testing D394 300
Stores D283 232
Developing D283 549

EMP_DEPT_MAPPING table:

EMP_ID EMP_DEPT
264 Designing
264 Testing
364 Stores
364 Developing

Functional dependencies:

EMP_ID → EMP_COUNTRY
EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

Candidate keys:

For the first table: EMP_ID


For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}

Now, this is in BCNF because left side part of both the functional dependencies is a key.
Fourth normal form (4NF):

 A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
 For a dependency A → B, if for a single value of A, multiple values of B exists, then the
relation will be a multi-valued dependency.

Example

STUDENT RELATION

STU_ID COURSE HOBBY


21 COMPUTER DANCING
21 MATH SINGING
34 CHEMESTRY DANCING
74 BIALOGY CRICKET
59 PHYSICS HOCKEY

The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent
entity. Hence, there is no relationship between COURSE and HOBBY.

In the STUDENT relation, a student with STU_ID, 21 contains two


courses, Computer and Math and two hobbies, Dancing and Singing. So there is a Multi-
valued dependency on STU_ID, which leads to unnecessary repetition of data.

So to make the above table into 4NF, we can decompose it into two tables:

STUDENT_COURSE, STUDENT_HOBBY.

Student_Course

STU_ID COURSE
21 COMPUTER
21 MATH
34 CHEMESTRY
74 BIALOGY
59 PHYSICS
Student_Hobby

STU_ID HOBBY
21 DANCING
21 SINGING
34 DANCING
74 CRICKET
59 HOCKEY
Fifth normal form (5NF):

 A relation is in 5NF if it is in 4NF and not contains any join dependency and joining
should be lossless.
 5NF is satisfied when all the tables are broken into as many tables as possible in
order to avoid redundancy.
 5NF is also known as Project-join normal form (PJ/NF)

SUBJECT LECTURER SEMESTER


COMPUTER Ansika Semester 1
COMPUTER John Semester 1
MATH John Semester 1
MATH Akash Semester 2
CHEMESTRY Praveen Semester 1

In the above table, John takes both Computer and Math class for Semester 1 but he doesn't take
Math class for Semester 2. In this case, combination of all these fields required to identify a valid
data.Suppose we add a new Semester as Semester 3 but do not know about the subject and who
will be taking that subject so we leave Lecturer and Subject as NULL. But all three columns
together acts as a primary key, so we can't leave other two columns blank.

So to make the above table into 5NF, we can decompose it into three relations P1, P2 & P3:
Table P1
SEMESTER SUBJECT
Semester 1 COMPUTER
Semester 1 MATH
Semester 2 MATH
Semester 1 CHEMESTRY

Table P2
SUBJECT LECTURER

COMPUTER Ansika

COMPUTER John

MATH John

MATH Akash

CHEMESTRY Praveen

Table P3
SEMESTER LECTURER
Semester 1 Ansika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
Example: Relation R has attributes R(ABCDEF) , set of FD’s, {ABC, CD, CE, EF,
FA} check the highest normal for relation R.

The given set of FD’s F = {ABC, CD, CE, EF, FA}

STEP:1 Find the all candidate keys of a relation


Attribute closure of AB is (AB+) = {ABCDEF}
Attribute closure of A is (A+) = {A}
Attribute closure of B is (B+) = {B}
AB is the candidate key
Here A is in RHS of FD (FA), so F can determine A ,
FB+={FBACDE}
FB also becomes the candidate key
Similarly EB is also the candidate key (EF),
EB+= {EBFACD}
CB is also the candidate key (CE)
CB+= {CBDEFA}
Candidate keys of relation R = {AB, FB, EB, CB}
STEP2: write all prime attributes
Prime attributes = {A,B, C,E,F}
STEP3: write all non prime attributes
Non prime attribute = {D}
Given set of FD’s
F = {ABC,
CDE,
EF,
FA}
Check for Highest normal form or BCNF
BCNF: L.H.S. of all FD’s should be candidate key.
It is not in BCNF because C, E and F are not candidate keys.
Check for next highest normal form 3NF
3NF: it should not contain any transitive dependency
It is not in 3NF because there is a transitive dependency ABC and C-DE then ABDE,
EF and FA, then EA. it is not in 3NF.

2NF: L.H.S. of all FD’s should be candidate key or RHS is Prime attribute it should not contain
any partial dependency L.H.S. is proper subset of any candidate key and R.H.S is non prime
attribute)
It is not in 2NF because is C is proper subset of candidate key and D is Non prime attribute.
(CDE)
The highest normal for of given relation R is first normal form (1NF).

Example: check the highest normal form of the Student relation R, set of functional
dependencies F is {RollnoName, RollnoVoterid, Voteridage, Voter Rollno }.

Roll Name Voter id Age


no
1 Ravi K0123 20
2 Varun M034 21
3 Ravi K786 23
4 Rahul D286 21

Given student relation R is

Roll Name Voter id Age


no
1 Ravi K0123 20
2 Varun M034 21
3 Ravi K786 23
4 Rahul D286 21
Functional dependencies set
F={RollnoName,
RollnoVoterid,
Voteridage,
Voterid Rollno }.

To check the highest normal form, we can check from highest to lowest.

BCNF: L.H. S of every functional dependency is the candidate key or super key of a relation.
Candidate keys of a relation are {Roll no, Voter id}

The above table is in BCNF in L.H. S of every functional dependency is the candidate key.

Example: R (A, B, C) and set of FD’s F={ABC, CA} show that R is in 3NF, not in

BCNF? And convert this relation R in 3NF into BCNF.

Given set of FD’s F= {ABC, CA}


Attribute closure of AB is(AB+)={ABC}
Attribute closure of A is (A+)={A}
Attribute closure of B is (B+)={B}

Attribute closure of C is(C+) = {AC}

So Candidate key is {AB} , prime attributes are {A, B}

The above relation is in 3NF because AB is candidate key in FD (AB C) and A is the prime
attribute in FD (CA) of Relation R.

The above relation is not in BCNF because attribute C is not candidate key in FD (CA) of
relation R

The above Relation in 3NF to BCNF


Decompose a relation R (ABC) into R1 (AB) and R2 (AC)
Example : Find the highest normal form of a relation R(A,B,C,D,E) with FD set as {BC->D,
AC->BE, B->E}.

Step 1. As we can see, (AC)+ ={A,C,B,E,D} but none of its subset can determine all attribute of
relation, So AC will be candidate key. A or C can’t be derived from any other attribute of the
relation, so there will be only 1 candidate key {AC}.
Step 2. Prime attributes are those attribute which are part of candidate key {A, C} in this
example and others will be non-prime {B, D, E} in this example.
Step 3. The relation R is in 1st normal form as a relational DBMS does not allow multi-valued or
composite attribute.
The relation is in 2nd normal form because BC->D is in 2nd normal form (BC is not a proper
subset of candidate key AC) and AC->BE is in 2nd normal form (AC is candidate key) and B->E
is in 2nd normal form (B is not a proper subset of candidate key AC).
The relation is not in 3rd normal form because in BC->D (neither BC is a super key nor D is a
prime attribute) and in B->E (neither B is a super key nor E is a prime attribute) but to satisfy 3rd
normal for, either LHS of an FD should be super key or RHS should be prime attribute.
So the highest normal form of relation will be 2nd Normal form.

Example: Find the highest normal form in R (A, B, C, D, E) under following functional
dependencies.
ABC  D, CD  AE
1) It is always a good idea to start checking from BCNF, then 3 NF and so on.
2) If any functional dependency satisfied a normal form then there is no need to check for
lower normal form.
For example, ABC –> D is in BCNF (Note that ABC is a super key), so no need to check this
dependency for lower normal forms.
Candidate keys in the given relation are {ABC, BCD}
BCNF: ABC -> D is in BCNF. Let us check CD -> AE, CD is not a super key so this dependency
is not in BCNF. So, R is not in BCNF.

3NF: ABC -> D we don’t need to check for this dependency as it already satisfied BCNF. Let us
consider CD -> AE. Since E is not a prime attribute, so the relation is not in 3NF.

2NF: In 2NF, we need to check for partial dependency. CD which is a proper subset of a candidate
key and it determine E, which is non-prime attribute. So, given relation is also not in 2 NF. So, the
highest normal form is 1 NF.

You might also like