0% found this document useful (0 votes)
36 views

UNIT-III Lecture Notes

Uploaded by

xodovip537
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)
36 views

UNIT-III Lecture Notes

Uploaded by

xodovip537
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/ 29

UNIT-III

SQL - Queries, Constraints, Triggers: Overview, the form of a basic SQL query, UNION,
INTERSECT, EXCEPT, nested queries, aggregate Operators, NULL values, complex integrity
constraints in SQL, Triggers and Active Databases.
Schema Refinement and normal Forms: Introduction to schema refinement, functional
dependencies, normal forms, Properties of Decompositions, Normalizations.

Overview:

The SQL Language has several features/aspects such as

1. The Data Manipulation Language (DML)


2. The Data Definition Language(DDL)
3. Triggers and Advanced Integrity Constraints
4. Embedded and Dynamic SQL
5. Client-Sever Execution and Remote Database Access
6. Transaction Management
7. Security
8. Advanced features like object-oriented features, recursive queries, decision support
queries.

The form of a basic SQL query:

The basic form of SQL Query is as follows:

SELECT [DISTINCT] select-list

FROM from-list

WHERE qualification;

 The from-list in the FROM clause is a list of table names. A table name can be followed by a
range variable; a range variable is particularly useful when the same table name appears more
than once in the from-list.
 The select-list is a list of (expressions involving) column names of tables named in the from-
list. Column names can be prefixed by a range variable.
 The qualification in the WHERE clause is a boolean combination (i.e., an expression using
the logical connectives AND, OR, and NOT) of conditions of the form expression op
expression, where op is one of the comparison operators {<, <=, =, <>, >=, >}.An expression
is a column name, a constant, or an (arithmetic or string) expression.
 The DISTINCT keyword is optional. It indicates that the table computed as an answer to this
query should not contain duplicates, that is, two copies of the same row. The default is that
duplicates are not eliminated.

Database Management Systems (R19) Dr. S Rao Chintalapudi


Relational Schemas used:

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

Boats( bid: integer, bname: string, color: string)

Reserves (sid: integer, bid: integer, day: date)

Instances of Sailors, Boats, Reserves tables:

Q: Find the names and ages of all sailors

Without DISTINCT:

SELECT s.sname, s.age

FROM sailors s;

With DISTINCT:

SELECT DISTINCT s.sname, s.age

FROM sailors s;

Q:Find all sailors with rating above 7

SELECT S.sid, S.sname, S.rating, S.age

FROM Sailors AS S

WHERE S.rating > 7;

Q: Find the names of sailors „Who have reserved boat number 103‟.

SELECT S.sname

Database Management Systems (R19) Dr. S Rao Chintalapudi


FROM Sailors S, Reserves R

WHERE S.sid = R.sid AND R.bid=103;


Q: Find the sids of sailors who have reserved a Red boat.
SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'red';

Q:Find the names of sailors 'Who have reserved a red 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';

Q: Find the colors of boats reserved by Lubber.

SELECT B.color

FROM Sailors S, Reserves R, Boats B

WHERE S.sid = R.sid AND R.bid = B.bid AND S.sname = 'Lubber‟;

Q: 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;

Q: 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';

Q: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

Database Management Systems (R19) Dr. S Rao Chintalapudi


AND (B.color = 'red' OR B.color = 'green');

UNION:

The SQL UNION clause/operator is used to combine the results of two or more SELECT
statements without returning any duplicate rows.

To use this UNION clause, each SELECT statement must have

 The same number of columns selected


 The same number of column expressions
 The same data type and
 Have them in the same order

But they need not have to be in the same length.

Q: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';

Q: Find all sids of sailors who have a rating of 10 or reserved boat 104.

SELECT S.sid

FROM Sailors S

WHERE S.rating = 10

UNION

SELECT R.sid

FROM Reserves R

Database Management Systems (R19) Dr. S Rao Chintalapudi


WHERE R.bid = 104 ;

INTERSECT:

The SQL INTERSECT clause/operator is used to combine two SELECT statements, but returns
rows only from the first SELECT statement that are identical to a row in the second SELECT
statement. This means INTERSECT returns only common rows returned by the two SELECT
statements.

Q: Find the names of sailors who have reserved both 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/Set Difference:

The SQL EXCEPT clause/operator is used to combine two SELECT statements and returns rows
from the first SELECT statement that are not returned by the second SELECT statement. This
means EXCEPT returns only rows, which are not available in the second SELECT statement.

Q:Find the sids 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;

Database Management Systems (R19) Dr. S Rao Chintalapudi


SELECT R. sid

FROM Boats B, Reserves R

WHERE R.bid = B.bid AND B.color = 'red'

EXCEPT

SELECT R2.sid

FROM Boats B2, Reserves R2

WHERE R2.bid = B2.bid AND B2.color = green';

Nested queries:

 A Subquery or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.
 A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
 Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

Q: 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 )

Q: 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

Database Management Systems (R19) Dr. S Rao Chintalapudi


WHERE R. bid

IN (SELECT B.bid

FROM Boats B

WHERE B.color = 'red')

Q: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' );

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 );

Q: Find sailors whose rating is better than some sailor called Horatio.

SELECT S.sid

FROM Sailors S

WHERE S.rating >

Database Management Systems (R19) Dr. S Rao Chintalapudi


ANY ( SELECT S2.rating

FROM Sailors S2

WHERE S2.sname = 'Horatio' );

Q: Find sailors whose rating is better than every sailor called Horatio.

SELECT S.sid

FROM Sailors S

WHERE S.rating >

ALL ( SELECT S2.rating

FROM Sailors S2

WHERE S2.sname = 'Horatio' );

Q: Find the sailors with the highest rating.

SELECT S.sid

FROM Sailors S

WHERE S.rating >= ALL ( SELECT S2.rating

FROM Sailors S2 )

Note that IN and NOT IN are equivalent to = ANY and <> ALL, respectively.

Aggregate operators:

SQL supports five aggregate operators, which can be applied on any column.

1. COUNT()-returns number of values


2. SUM()-returns sum of values
3. AVG()-returns average of values
4. MAX()-returns maximum value
5. MIN() –returns minimum value

Q: Find the average age of all sailors

SELECT AVG (S.age)

FROM Sailors S

Q:Find the average age of sailors with a rating of 10.

Database Management Systems (R19) Dr. S Rao Chintalapudi


SELECT AVG (S.age)

FROM Sailors S

WHERE S.rating = 10;

Q:Find the name and age of the oldest sailor.

SELECT S.sname, S.age

FROM Sailors S

WHERE S.age =

( SELECT MAX (S2.age)

FROM Sailors S2 )

Q: Count the number of sailors.

SELECT COUNT(*)

FROM sailors;

Q: Count the number of different sailor names.

SELECT COUNT ( DISTINCT S.sname )

FROM Sailors S;

The GROUP BY and HAVING Clauses:

The general form of an SQL query with GROUP BY and HAVING clauses is:

SELECT [ DISTINCT] select-list

FROM from-list

WHERE qualification

GROUP BY grouping-list

HAVING group-qualification;

Q: Find the age of the youngest sailor for each rating level.

SELECT S.rating, MIN (S.age)


FROM Sailors S

Database Management Systems (R19) Dr. S Rao Chintalapudi


GROUP BY S.rating;

Q: Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old) for
each rating level with at least two such sailors.
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT (*) > 1;

Q: Find the average age of sailors for each rating level that has at least two sailors.

SELECT S.rating, AVG (S.age) AS avgage

FROM Sailors S

GROUP BY S.rating

HAVING COUNT (*) > 1;

NULL values:

 The SQL NULL is the term used to represent a missing value.


 A NULL value in a table is a value in a field that appears to be blank.
 A field with a NULL value is a field with no value.
 It is very important to understand that a NULL value is different than a zero value or a field
that contains spaces.

NOT NULL Constraint:

SQL> CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR2(20),
AGE INT,
ADDRESS VARCHAR2(25) ,
SALARY FLOAT,
);

 Here, NOT NULL signifies that column should always accept an explicit value of the given
data type.
 A field with a NULL value is the one that has been left blank during the record creation.

Database Management Systems (R19) Dr. S Rao Chintalapudi


IS NOT NULL operator:

SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY


FROM CUSTOMERS
WHERE SALARY IS NOT NULL;

IS NULL operator:

SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY


FROM CUSTOMERS
WHERE SALARY IS NULL;

Complex integrity constraints in SQL:

Constraints over a Single Table Constraint:

Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER, age INTEGER,
PRIMARY KEY (sid), CHECK (rating >= 1 AND rating <=10));

Example of Table Constraints:

Create TABLE Reserves (sid INTEGER, bid INTEGER, day DATE, FOREIGN KEY (sid)
REFERENCES Sailors FOREIGN KEY (bid) REFERENCES Boats, CONSTRAINT
noInterLakeRes CHECK (Interlake <> (SELECT B.bname FROM Boats B WHERE B.bid =
Reserves.bid)));

Domain Constraints:

CREATE DOMAIN ratingval INTEGER DEFAULT 1 CHECK (VALUE >= 1 AND VALUE <=10);

Triggers:

A trigger is a procedure that is automatically invoked by the DBMS in response to specified


changes to the database, and is typically specified by the DBA.

Active Databases:

A database that has a set of associated triggers is called an active database.

A trigger description contains three parts:

 Event: A change to the database that activates the trigger.


 Condition: A query or test that is run when the trigger is activated.
 Action: A procedure that is executed when the trigger is activated and its condition is
true.

Database Management Systems (R19) Dr. S Rao Chintalapudi


Triggers can be defined on the table, view, schema, or database with which the event is
associated.

Benefits of Triggers:

Triggers can be written for the following purposes −

 Generating some derived column values automatically


 Enforcing referential integrity
 Event logging and storing information on table access
 Auditing
 Synchronous replication of tables
 Imposing security authorizations
 Preventing invalid transactions

Creating Triggers:
The syntax for creating a trigger is-

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;

Where,

 CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an existing


trigger with the trigger_name.
 {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will 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 will be updated.

Database Management Systems (R19) Dr. S Rao Chintalapudi


 [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, such as INSERT, UPDATE, and DELETE.
 [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will 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.

Select * from customers;


+----+----------+-----+-----------+----------+
| 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 |
+----+----------+-----+-----------+----------+

The following program creates a row-level trigger for the customers table that would fire for
INSERT or UPDATE or DELETE operations performed on the CUSTOMERS table. This
trigger will display the salary difference between the old values and new values –

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;
/
Trigger created.

 OLD and NEW references are not available for table-level triggers, rather you can use
them for record-level triggers.

INSERT Operation:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

Database Management Systems (R19) Dr. S Rao Chintalapudi


VALUES (7, 'Kriti', 22, 'HP', 7500.00 );

When a record is created in the CUSTOMERS table, the above create trigger,
display_salary_changes will be fired and it will display the following result.
Old salary:
New salary: 7500
Salary difference:
Because this is a new record, old salary is not available and the above result comes as null

UPDATE operation:
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
When a record is updated in the CUSTOMERS table, the above create trigger,
display_salary_changes will be fired and it will display the following result
Old salary: 1500
New salary: 2000
Salary difference: 500

Introduction to schema refinement / Purpose of Normalization:

 Normalization is the process of organizing the data in the database.


 Normalization is used to minimize the redundancy from a relation or set of relations.
 It is also used to eliminate the undesirable characteristics like Insertion, Update and
Deletion Anomalies.
 Normalization divides the larger table into the smaller table and links them using
relationship.
 The normal form is used to reduce redundancy from the database table.

Problems caused by Redundancy:

There are three types of anomalies that occur when the database is not normalized. These are –
Insertion, update and deletion anomaly.

Update Anomaly: If one copy of such repeated data is updated, an inconsistency is created
unless all copies are similarly updated.

Insertion Anomaly: It may not be possible to store certain information unless some other,
unrelated, information is stored as well.

Deletion Anomaly: It may not be possible to delete certain information without losing some
other, unrelated, information as well.

Database Management Systems (R19) Dr. S Rao Chintalapudi


Example:

emp_id emp_name emp_address emp_dept


101 Rick Delhi D001
101 Rick Delhi D002
123 Maggie Agra D890
166 Glenn Chennai D900
166 Glenn Chennai D004

Update anomaly: In the above table we have two rows for employee Rick as he belongs to two
departments of the company. If we want to update the address of Rick then we have to update the
same in two rows or the data will become inconsistent. If somehow, the correct address gets
updated in one department but not in other then as per the database, Rick would be having two
different addresses, which is not correct and would lead to inconsistent data.

Insert anomaly: Suppose a new employee joins the company, who is under training and
currently not assigned to any department then we would not be able to insert the data into the
table if emp_dept field doesn‟t allow nulls.

Delete anomaly: Suppose, if at a point of time the company closes the department D890 then
deleting the rows that are having emp_dept as D890 would also delete the information of
employee Maggie since she is assigned only to this department.

To overcome these anomalies we need to normalize the data.

Functional dependencies:

 The attributes of a table is said to be dependent on each other when an attribute of a table
uniquely identifies another attribute of the same table.
 Ex: Stu_id attribute uniquely identifies the Stu_Name attribute of student table because if
we know the student id we can tell the student name associated with it.
 This is known as functional dependency.
 It can be written as Stu_Id->Stu_Name
 Stu_Name is functionally dependent on Stu_Id

Formal definition of Functional Dependency :

Database Management Systems (R19) Dr. S Rao Chintalapudi


 If column A of a table uniquely identifies the column B of same table then it can
represented as A->B
 Attribute B is functionally dependent on attribute A

Types of functional dependencies:

 Trivial functional dependency


 non-trivial functional dependency
 Multivalued functional dependency
 Transitive functional dependency

Trivial functional dependency :

 The dependency of an attribute on a set of attributes is known as trivial functional


dependency if the set of attributes includes that attribute.

 A ->B is trivial functional dependency if B is a subset of A.

 The following dependencies are also trivial: A->A & B->B

 Ex: {Student_Id, Student_Name} -> Student_Id is a trivial functional dependency as


Student_Id is a subset of {Student_Id, Student_Name}.

 if we know the values of Student_Id and Student_Name then the value of Student_Id can
be uniquely determined.

 Student_Id -> Student_Id & Student_Name -> Student_Name are trivial dependencies
too

Non Trivial functional dependency :

 If a functional dependency X->Y holds true where Y is not a subset of X then this
dependency is called non trivial Functional dependency.

 The following functional dependencies are non-trivial:


emp_id -> emp_name (emp_name is not a subset of emp_id)
emp_id -> emp_address (emp_address is not a subset of emp_id)

 The following dependencies are trivial:


{emp_id, emp_name} -> emp_name [emp_name is a subset of {emp_id, emp_name}]

 If a FD X->Y holds true where X intersection Y is null then this dependency is said to be
completely non trivial function dependency.

Database Management Systems (R19) Dr. S Rao Chintalapudi


Multivalued Functional dependency:

 Multivalued dependency occurs when there are more than one independent multivalued
attributes in a table.

 Consider a bike manufacture company, which produces two colors (Black and Red) in
each model every year.

 Here columns manuf_year and color are independent of each other and dependent on
bike_model.

 In this case these two columns are said to be multivalued dependent on bike_model.

 These dependencies can be represented like this:

 bike_model ->> manuf_year

bike_model ->> color

bike_model manuf_year color


M1001 2007 Black
M1001 2007 Red
M2012 2008 Black
M2012 2008 Red
M2222 2009 Black
M2222 2009 Red

Transitive functional dependency :

 A functional dependency is said to be transitive if it is indirectly formed by two


functional dependencies.

 X -> Z is a transitive dependency if the following three functional dependencies hold


true:

X->Y

Y does not ->X

Y->Z

 A transitive dependency can only occur in a relation of three of more attributes.

{Book} ->{Author} (if we know the book, we knows the author name)

Database Management Systems (R19) Dr. S Rao Chintalapudi


{Author} does not ->{Book}

{Author} -> {Author_age}

 Therefore as per the rule of transitive dependency:

{Book} -> {Author_age} should hold, that makes sense because if we know the book
name we can know the author‟s age.

Book Author Author_age


Game of Thrones George R. R. Martin 66
Harry Potter J. K. Rowling 49
Dying of the Light George R. R. Martin 66

First Normal Form:

 An attribute (column) of a table cannot hold multiple values.

 It should hold only atomic values.

Ex:

emp_id emp_name emp_address emp_mobile


101 Herschel New Delhi 8912312390
8812121212
102 Jon Kanpur
9900012222
103 Ron Chennai 7778881212
9990000123
104 Lester Bangalore
8123450987

 Two employees (Jon & Lester) are having two mobile numbers so the company stored
them in the same field as you can see in the table above.
 This table is not in 1NF as the rule says “each attribute of a table must have atomic
(single) values”, the emp_mobile values for employees Jon & Lester violates that rule.

To make the table complies with 1NF we should have the data like this:

Database Management Systems (R19) Dr. S Rao Chintalapudi


emp_id emp_name emp_address emp_mobile
101 Herschel New Delhi 8912312390
102 Jon Kanpur 8812121212
102 Jon Kanpur 9900012222
103 Ron Chennai 7778881212
104 Lester Bangalore 9990000123
104 Lester Bangalore 8123450987

Second Normal Form :

 A table is said to be in 2NF if both the following conditions hold:

• Table is in 1NF (First normal form)

• No non-prime attribute is dependent on the proper subset of any candidate key of


table.

 An attribute that is not part of any candidate key is known as non-prime attribute.

Example: Suppose a school wants to store the data of teachers and the subjects they teach. They
create a table that looks like this: Since a teacher can teach more than one subjects, the table can
have multiple rows for a same teacher.

teacher_id subject teacher_age


111 Maths 38
111 Physics 38
222 Biology 38
333 Physics 40
333 Chemistry 40

Candidate Keys: {teacher_id, subject}


Non prime attribute: teacher_age

The table is in 1 NF because each attribute has atomic values. However, it is not in 2NF because
non prime attribute teacher_age is dependent on teacher_id alone which is a proper subset of
candidate key. This violates the rule for 2NF as the rule says “no non-prime attribute is
dependent on the proper subset of any candidate key of the table”.

To make the table complies with 2NF we can break it in two tables like this:

Database Management Systems (R19) Dr. S Rao Chintalapudi


teacher_details table:

teacher_id teacher_age
111 38
222 38
333 40

teacher_subject table:

teacher_id subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry

Now the tables comply with Second normal form (2NF).

Third Normal Form:

 A table is said to be in 3NF if both the following conditions hold:

• Table must be in 2NF

• Transitive functional dependency of non-prime attribute on any super key


should be removed.

 An attribute that is not part of any candidate key is known as non-prime attribute.
 In other words 3NF can be explained like this: A table is in 3NF if it is in 2NF and for
each functional dependency X-> Y at least one of the following conditions hold:
• X is a super key of table
• Y is a prime attribute of table
 An attribute that is a part of one of the candidate keys is known as prime attribute.
 Example: Suppose a company wants to store the complete address of each employee,
they create a table named employee_details that looks like this:

emp_id emp_name emp_zip emp_state emp_city emp_district


1001 John 282005 UP Agra Dayal Bagh
1002 Ajeet 222008 TN Chennai M-City

Database Management Systems (R19) Dr. S Rao Chintalapudi


1006 Lora 282007 TN Chennai Urrapakkam
1101 Lilly 292008 UK Pauri Bhagwan
1201 Steve 222999 MP Gwalior Ratan

Super keys: {emp_id}, {emp_id, emp_name}, {emp_id, emp_name, emp_zip}…so on


Candidate Keys: {emp_id}
Non-prime attributes: all attributes except emp_id are non-prime as they are not part of any
candidate keys.

Here, emp_state, emp_city & emp_district dependent on emp_zip. And, emp_zip is dependent on
emp_id that makes non-prime attributes (emp_state, emp_city & emp_district) transitively
dependent on super key (emp_id). This violates the rule of 3NF.

emp_id ----->emp_zip

emp_zip is not----->emp_id

emp_zip ----->{emp_state,emp_city,emp_district}

emp_id ---->{emp_state,emp_city,emp_district}

To make this table complies with 3NF we have to break the table into two tables to remove the
transitive dependency:

employee table:

emp_id emp_name emp_zip


1001 John 282005
1002 Ajeet 222008
1006 Lora 282007
1101 Lilly 292008
1201 Steve 222999

employee_zip table:

emp_zip emp_state emp_city emp_district


282005 UP Agra Dayal Bagh
222008 TN Chennai M-City

Database Management Systems (R19) Dr. S Rao Chintalapudi


282007 TN Chennai Urrapakkam
292008 UK Pauri Bhagwan
222999 MP Gwalior Ratan

Boyce-Codd Normal Form:

 It is an advance version of 3NF that‟s why it is also referred as 3.5NF.

 BCNF is stricter than 3NF.

 A table complies with BCNF if it is in 3NF and for every functional dependency X->Y,
X should be the super key of the table.

Example: Suppose there is a company wherein employees work in more than one
department. They store the data like this:

emp_id emp_nationality emp_dept dept_type dept_no_of_emp


1001 Austrian Production and planning D001 200
1001 Austrian stores D001 250
1002 American design and technical support D134 100
1002 American Purchasing department D134 600

Functional dependencies in the table above:

emp_id -> emp_nationality

emp_dept -> {dept_type, dept_no_of_emp}

Candidate key: {emp_id, emp_dept}

The table is not in BCNF as neither emp_id nor emp_dept alone are keys.

To make the table comply with BCNF we can break the table in three tables like this:

emp_nationality table:

emp_id emp_nationality
1001 Austrian
1002 American

Database Management Systems (R19) Dr. S Rao Chintalapudi


emp_dept table:

emp_dept dept_type dept_no_of_emp


Production and planning D001 200
stores D001 250
design and technical support D134 100
Purchasing department D134 600

emp_dept_mapping table:

emp_id emp_dept
1001 Production and planning
1001 stores
1002 design and technical support
1002 Purchasing department

Functional dependencies:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no_of_emp}

Candidate keys:
For first table: emp_id
For second table: emp_dept
For third table: {emp_id, emp_dept}

This is now in BCNF as in both the functional dependencies left side part is a key.

Super Key Vs Candidate Key Vs Primary Key:

A super key is a set of one or more attributes (columns), which can uniquely identify a row in a
table.

Candidate keys are selected from the set of super keys, the only thing we take care while
selecting candidate key is: It should not have any redundant attribute. That‟s the reason they are
also termed as minimal super key.

Table: Employee

Emp_SSN Emp_Number Emp_Name


--------- ---------- --------
123456789 226 Steve
999999321 227 Ajeet
888997212 228 Chaitanya

Database Management Systems (R19) Dr. S Rao Chintalapudi


777778888 229 Robert

The above table has following super keys. All of the following sets of super key are able to
uniquely identify a row of the employee table.

 {Emp_SSN} 1
 {Emp_Number} 1
 {Emp_SSN, Emp_Number} 2
 {Emp_SSN, Emp_Name} 2
 {Emp_SSN, Emp_Number, Emp_Name} 3
 {Emp_Number, Emp_Name}2

A candidate key is a minimal super key with no redundant attributes. The following two set of
super keys are chosen from the above sets as there are no redundant attributes in these sets.

 {Emp_SSN}
 {Emp_Number}

A Primary key is selected from a set of candidate keys. This is done by database admin or
database designer. We can say that either {Emp_SSN} or {Emp_Number} can be chosen as a
primary key for the table Employee.

Fourth Normal Form

 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.

Ex:

STUDENT

STU_ID COURSE HOBBY


21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey

Database Management Systems (R19) Dr. S Rao Chintalapudi


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

STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics

STUDENT_HOBBY

STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey

Properties of Decomposition:

 When a relation in the relational model is not in appropriate normal form then the
decomposition of a relation is required.
 In a database, it breaks the table into multiple tables.
 If the relation has no proper decomposition, then it may lead to problems like loss of
information.
 Decomposition is used to eliminate some of the problems of bad design like anomalies,
inconsistencies, and redundancy.

Database Management Systems (R19) Dr. S Rao Chintalapudi


Types of Decomposition:

Lossless join Decomposition:

 If the information is not lost from the relation that is decomposed, then the decomposition
will be lossless.
 The lossless decomposition guarantees that the join of relations will result in the same
relation as it was decomposed.
 The relation is said to be lossless decomposition if natural joins of all the decomposition
give the original relation.

Example:

EMPLOYEE_DEPARTMENT table:

EMP_ID EMP_NAME EMP_AGE EMP_CITY DEPT_ID DEPT_NAME


22 Denim 28 Mumbai 827 Sales
33 Alina 25 Delhi 438 Marketing
46 Stephan 30 Bangalore 869 Finance
52 Katherine 36 Mumbai 575 Production
60 Jack 40 Noida 678 Testing

 The above relation is decomposed into two relations EMPLOYEE and DEPARTMENT

EMPLOYEE table:

EMP_ID EMP_NAME EMP_AGE EMP_CITY


22 Denim 28 Mumbai
33 Alina 25 Delhi
46 Stephan 30 Bangalore
52 Katherine 36 Mumbai
60 Jack 40 Noida

Database Management Systems (R19) Dr. S Rao Chintalapudi


DEPARTMENT table

DEPT_ID EMP_ID DEPT_NAME


827 22 Sales
438 33 Marketing
869 46 Finance
575 52 Production
678 60 Testing

Now, when these two relations are joined on the common column "EMP_ID", then the resultant
relation will look like:

Employee ⋈ Department

EMP_ID EMP_NAME EMP_AGE EMP_CITY DEPT_ID DEPT_NAME


22 Denim 28 Mumbai 827 Sales
33 Alina 25 Delhi 438 Marketing
46 Stephan 30 Bangalore 869 Finance
52 Katherine 36 Mumbai 575 Production
60 Jack 40 Noida 678 Testing

Hence, the decomposition is Lossless join decomposition.

Dependency Preserving Decomposition:

 It is an important constraint of the database.


 In the dependency preservation, at least one decomposed table must satisfy every
dependency.
 If a relation R is decomposed into relation R1 and R2, then the dependencies of R either
must be a part of R1 or R2 or must be derivable from the combination of functional
dependencies of R1 and R2.
 For example, suppose there is a relation R (A, B, C, D) with functional dependency set
(A->BC). The relational R is decomposed into R1(ABC) and R2(AD) which is
dependency preserving because FD A->BC is a part of relation R1(ABC).

Fifth Normal Form

 A relation is in 5NF if it is in 4NF and not contains any join dependency and joining should
be lossless.

Database Management Systems (R19) Dr. S Rao Chintalapudi


 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).

Example:

SUBJECT LECTURER SEMESTER


Computer Anshika Semester 1
Computer John Semester 1
Math John Semester 1
Math Akash Semester 2
Chemistry 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 R1, R2 &
R3:

R1

SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math

R2

SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen

Database Management Systems (R19) Dr. S Rao Chintalapudi


R3

SEMSTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen

*****

Database Management Systems (R19) Dr. S Rao Chintalapudi

You might also like