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

DBMS Unit3

This document discusses SQL and schema refinement. It provides an overview of SQL including data definition and manipulation languages. It also covers topics like basic SQL queries, nested queries, aggregate functions, and normal forms for schema refinement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

DBMS Unit3

This document discusses SQL and schema refinement. It provides an overview of SQL including data definition and manipulation languages. It also covers topics like basic SQL queries, nested queries, aggregate functions, and normal forms for schema refinement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Unit-III: SQL & Schema Refinement

SQL: Form of Basic SQL Query – Examples of Basic SQL Queries, Introduction to Nested
Queries, Correlated Nested Queries, Set – Comparison Operators, Aggregate Operators, NULL
values – Comparison using Null values – Logical connectives – AND, OR and NOT – Impact on
SQL Constructs, Outer Joins, Disallowing NULL values, Complex Integrity Constraints in SQL
Triggers.

Introduction to Schema Refinement – Problems Caused by redundancy, Decompositions –


Problem related to decomposition, Functional Dependencies - Reasoning about FDS, Normal
Forms – FIRST, SECOND, THIRD Normal forms – BCNF –Properties of Decompositions- Loss
less join Decomposition, Dependency preserving Decomposition, Schema Refinement in Data
base Design – Multi valued Dependencies – FOURTH Normal Form, Join Dependencies, FIFTH
Normal form, Inclusion Dependencies.

SQL Overview

SQL is a programming language for Relational Databases. It is designed over relational algebra
and tuple relational calculus. SQL comes as a package with all major distributions of RDBMS.

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.

SQL uses the following set of commands to define database schema − Examples of Basic SQL
Queries

CREATE

Creates new databases, tables and views from RDBMS.

For example −

Create database tutorialspoint;


Create table article;
Create view for_students;

DROP

Drops commands, views, tables, and databases from RDBMS.

For example−
Drop object_type object_name;
Drop database tutorialspoint;
Drop table article;
Drop view for_students;

ALTER

Modifies database schema Alter object_type object_name parameters;

For example−

Alter table article add subject varchar;

This command adds an attribute in the relation article with the name subject of string type.

Data Manipulation Language

SQL is equipped with data manipulation language (DML). DML modifies the database instance
by inserting, updating and deleting its data. DML is responsible for all forms data modification
in a database. SQL contains the following set of commands in its DML section −

 SELECT/FROM/WHERE
 INSERT INTO/VALUES
 UPDATE/SET/WHERE
 DELETE FROM/WHERE

These basic constructs allow database programmers and users to enter data and information into
the database and retrieve efficiently using a number of filter options.

SELECT/FROM/WHERE

 SELECT − This is one of the fundamental query command of SQL. It is similar to the
projection operation of relational algebra. It selects the attributes based on the condition
described by WHERE clause.
 FROM − This clause takes a relation name as an argument from which attributes are to
be selected/projected. In case more than one relation names are given, this clause
corresponds to Cartesian product.
 WHERE − This clause defines predicate or conditions, which must match in order to
qualify the attributes to be projected.

For example −

Select author_name From book_author Where age > 50;


This command will yield the names of authors from the relation book_author whose age is
greater than 50.

INSERT INTO/VALUES

This command is used for inserting values into the rows of a table (relation).

Syntax−

INSERT INTO table (column1 [, column2, column3 ...]) VALUES (value1 [, value2, value3 ... ])
or
INSERT INTO table VALUES (value1, [value2, ... ])

For example −

INSERT INTO tutorialspoint (Author, Subject) VALUES ("anonymous", "computers");

UPDATE/SET/WHERE

This command is used for updating or modifying the values of columns in a table (relation).

Syntax −

UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE


condition]

For example −

UPDATE tutorialspoint SET Author="webmaster" WHERE Author="anonymous";

DELETE/FROM/WHERE

This command is used for removing one or more rows from a table (relation).

Syntax −DELETE FROM table_name [WHERE condition];

For example −DELETE FROM tutorialspoints WHERE Author="unknown";

Introduction to Nested Queries (Sub query)

A nested query is a query that has another query embedded within it; the embedded query is
called a sub query. The embedded query can of course be a nested query itself.
 A Scalar Sub query returns a single column and a single row; i.e., is a single
value. In principal, a scalar sub query can be used whenever a single value is
needed.

 A row sub query returns multiple columns, but again only a single row. A row
sub query can be used whenever a row value constructor is needed.

 A table sub query returns one or more columns and multiple rows. A table sub
query can be used whenever a table is needed.

Examples

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

A. Select S.sname from Sailors S where S.sid in (Select R.sid from Reserves R where
R.bid=103);

Q. Find the names of Sailors who have reserved a red boat.

A. Select S.sname from Sailors S where S.sid in (select R.sid from Reserves R where R.bid in
(select b.bid from Boats b where b.color=’red’));

Q. Find the names of Sailors who have not reserved a red boat.

A. Select S.sname from Sailors S where S.sid NOT IN (select R.sid from Reserves R where
R.bid in (select b.bid from Boats b where b.color=’red’));

Q. Find the details of student who have paid the highest amount so far in their course.

A. select sno, sname, course, totalfee, feepaid form students where (course, feepaid) in (select
course, max (feepaid) from students group by course);

Q. Find the details of students whose TOTALFEE is more than average TOTALFEE.

A. select sno, sname from students where totalfee > (select avg (totalfee) from students).

Correlated Nested Queries:

A Correlated sub query is a nested query that receives a value from the main query and then
sends a value back to main query.

Q. Find the details of student whose total fee is more than the average total fee of their course.
That means, if totalfee of a student is 10000 and the average totalfee of his/her course is less than
10000 then the details of that student are to be displayed.

A. select sno, sname, totalfee, course from students x where totalfee > (select avg (totalfee) from
students where course=x.course);
Set-Comparison Operators:

The following list gives the Set-Comparison Operators

 EXISTS

 NOT EXISTS

 IN

 UNIQUE

 ANY

 ALL

EXISTS and NOT EXISTS operators:

These two operators are exclusively used in correlated sub query. EXISTS checks whether any
row is existing in the sub query, and NOT EXISTS does the opposite.

EXISTS is different from other operators like IN, ANY etc. because it doesn’t compare values of
columns, instead, it checks whether any row is retrieved from sub query or not. If any row is
retrieved from sub query the EXISTS returns true otherwise it returns false.

The following will select student who have paid at least one instalment.

Select sno, sname from students where exists (select sno from instalments where
students.sno=sno);

IN operator:

It Checks a single value against a list of values.

Q. Find the details of subjects if version is either 3.0 or 5.0 or 6.0

A. select subcode, subdesc, version from subjects where version in (‘3.0’,’5.0’,’6.0’);

UNIQUE

It is closely related to EXISTS is the UNIQUE predicate. When we apply UNIQUE to a


subquery, the resulting condition returns true if no row appears twice in the answer to the
subquery, that is, there are no duplicates; in particular, it returns true if the answer is empty.

ANY and ALL Operators


Both are used for comparing one value against a set of values. ALL specifies that all the values
given in the list should be taken into account, whereas ANY specifies the condition be satisfied
when any of the satisfied the condition.

SYNTAX

Operator ANY list

Operator ALL list

The operator can be any one of the standard relational operators (=,>=,>,<,<=,!=) and list is a
series of values.

Example

Q. Find the sailors with highest rating.

A. select S.sid from sailors S where S.rating >= ALL (select S2.rating from sailors S2);

Select sno, sname, totalfee, feepaid from students where feepaid > ANY (select feepaid from
students);

The following list illustrates the result of ANY and ALL operator.

Rate ANY/ALL operator Result

10

10

10

10

Rate > Any (15, 20) False

Rate > ANY (5, 15) True

Rate > ALL (10, 20) False

Rate > ALL (5, 7) True

Subquery Correlated Subquery

Executed only for once before main-query Executed once for each row of main-query

Sends a value to main-query Receives a value from main query and sends a
value to main-query

SQL Aggregate Functions

SQL aggregate functions return a single value, calculated from values in a column.

Useful aggregate functions:

 AVG() - Returns the average value


 COUNT() - Returns the number of rows
 FIRST() - Returns the first value
 LAST() - Returns the last value
 MAX() - Returns the largest value
 MIN() - Returns the smallest value
 SUM() - Returns the sum

The AVG() Function

The AVG() function returns the average value of a numeric column.

SQL AVG() Syntax


SELECT AVG(column_name) FROM table_name

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Products" table:


SQL AVG() Example

The following SQL statement gets the average value of the "Price" column from the "Products"
table:

Example
SELECT AVG(Price) AS PriceAverage FROM Products;

SQL COUNT(column_name) Syntax

The COUNT(column_name) function returns the number of values (NULL values will not be
counted) of the specified column:

SELECT COUNT(column_name) FROM table_name;

SQL COUNT(*) Syntax

The COUNT(*) function returns the number of records in a table:

SELECT COUNT(*) FROM table_name;

SQL COUNT(DISTINCT column_name) Syntax

The COUNT(DISTINCT column_name) function returns the number of distinct values of the
specified column:

SELECT COUNT(DISTINCT column_name) FROM table_name;

Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with
Microsoft Access.

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:


SQL COUNT(column_name) Example

The following SQL statement counts the number of orders from "CustomerID"=7 from the
"Orders" table:

Example
SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM Orders
WHERE CustomerID=7;

The MAX() Function

The MAX() function returns the largest value of the selected column.

SQL MAX() Syntax


SELECT MAX(column_name) FROM table_name;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Products" table:


SQL MAX() Example

The following SQL statement gets the largest value of the "Price" column from the "Products"
table:

Example
SELECT MAX(Price) AS HighestPrice FROM Products;

The MIN() Function

The MIN() function returns the smallest value of the selected column.

SQL MIN() Syntax


SELECT MIN(column_name) FROM table_name;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Products" table:

SQL MIN() Example

The following SQL statement gets the smallest value of the "Price" column from the "Products"
table:

Example
SELECT MIN(Price) AS SmallestOrderPrice FROM Products;

The SUM() Function

The SUM() function returns the total sum of a numeric column.


SQL SUM() Syntax
SELECT SUM(column_name) FROM table_name;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "OrderDetails" table:

SQL SUM() Example

The following SQL statement finds the sum of all the "Quantity" fields for the "OrderDetails"
table:

Example
SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;

The GROUP BY Statement

The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set by one or more columns.

SQL GROUP BY Syntax


SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:

And a selection from the "Shippers" table:

SQL GROUP BY Example

Now we want to find the number of orders sent by each shipper.

The following SQL statement counts as orders grouped by shippers:

Example
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
The HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.

SQL HAVING Syntax


SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:

SQL HAVING Example

Now we want to find  if any of the employees has registered more than 10 orders.

We use the following SQL statement:


Example
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;

The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax


SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

ORDER BY Example

The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" column:
Example
SELECT * FROM Customers
ORDER BY Country;

ORDER BY DESC Example

The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country DESC;

ORDER BY Several Columns Example

The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column:

Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;

ORDER BY Several Columns Example 2

The following SQL statement selects all customers from the "Customers" table, sorted ascending
by the "Country" and descending by the "CustomerName" column:

Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

4.2.4.9 Lecture -9

Implementation of different types of joins

SQL JOIN

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.

The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN
returns all rows from multiple tables where the join condition is met.

Let's look at a selection from the "Orders" table:


Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the
"Customers" table. The relationship between the two tables above is the "CustomerID" column.

Then, if we run the following SQL statement (that contains an INNER JOIN):

Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Different SQL JOINs

Before we continue with examples, we will list the types of the different SQL JOINs you can
use:

 INNER JOIN: Returns all rows when there is at least one match in BOTH tables
 LEFT JOIN: Return all rows from the left table, and the matched rows from the right
table
 RIGHT JOIN: Return all rows from the right table, and the matched rows from the left
table
 FULL JOIN: Return all rows when there is a match in ONE of the tables
SQL INNER JOIN Keyword

The INNER JOIN keyword selects all rows from both tables as long as there is a match between
the columns in both tables.

SQL INNER JOIN Syntax


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;

PS! INNER JOIN is the same as JOIN.

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:


SQL INNER JOIN Example

The following SQL statement will return all customers with orders:

Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in
the right table (table2). The result is NULL in the right side when there is no match.

SQL LEFT JOIN Syntax


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

PS! In some databases LEFT JOIN is called LEFT OUTER JOIN.

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:


SQL LEFT JOIN Example

The following SQL statement will return all customers, and any orders they might have:

Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows
in the left table (table1). The result is NULL in the left side when there is no match.

SQL RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

PS! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.


Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:

SQL RIGHT JOIN Example

The following SQL statement will return all employees, and any orders they have placed:

Example
SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;

SQL FULL OUTER JOIN Keyword

The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right
table (table2).

The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

SQL FULL OUTER JOIN Syntax


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

SQL FULL OUTER JOIN Example

The following SQL statement selects all customers, and all orders:

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

A selection from the result set may look like this:

TRIGGERS:
A trigger is an operation that is executed when some kind of event occurs to the database. It can
be a data or object change.
Creation of Triggers:
 Triggers are created with the CREATE TRIGGER statement.
 This statement specifies that the In which table trigger is defined and on which events
trigger will be invoked.
 To drop Trigger one can use DROP TRIGGER statement.

Syntax: CREATE TRIGGER [owner.]trigger_name ON[owner.] table_name


FOR[INSERT/UPDATE/DELETE] AS IF UPDATE(column_name) [{AND/OR}
UPDATE(COLUMN_NAME)...] { sql_statements }

Triggers Types:
a) Row level Triggers b) Statement Level Triggers
Row Level triggers :A row level trigger is fired each time the table is affected by the
triggering statement.
 Example: If an UPDATE statement updates multiple rows of a table, a row
trigger s fired once for each row affected by the update statement.
 A row trigger will not run, if a triggering statement affects no rows. 
 If FOR EACH ROW clause is written that means trigger is row level trigger.
Statement Level Triggers: A statement level trigger is fired once on behalf of the
triggering statement, regardless of the number of rows in the table that the triggering
statement affects, even If no rows are affected.
 Example: If a DELETE statement deletes several rows from a table, a statement
level DELETE trigger is fired only once.
 Default when FOR EACH ROW clause is not written in trigger that means
trigger is statement level trigger
Rules of Triggers:
 Triggers cannot create or modify Database objects using triggers For example, cannot
perform “CREATE TABLE… or ALTER TABLE” sqlstatements under the triggers.
 It cannot perform any administrative tasks o For example, cannot perform “BACKUP
DATABASE…” task under the triggers  It cannot pass any kind of parameters.
 It cannot directly call triggers  WRITETEXT statements do not allow a trigger

Advantages of Triggers:
Triggers are useful for auditing data changes or auditing database as well as managing business
rules. Below are some examples:
1)Triggers can be used to enforce referential integrity (For example you may not be able to apply
foreign keys)
2)Can access both new values and old values in the database when going to do any insert, update
or delete
Disadvantages of Triggers
 Triggers hide database operations.
 For example when debugging a stored procedure, it’s possible to not be aware that a
trigger is on a table being checked for data changes 
 Executing triggers can affect the performance of a bulk import operation .

Introduction to schema Refinement

Schema Refinement: Schema Refinement is the process that re-defines the schema of a
relation.A refinement approach is based on decompositions. Decomposition can eliminate the
redundancy.

Schema-Refinement addresses the following problems:

i) problems caused by redundancy

ii) problems caused by decomposition.

i).Problems caused by redundancy:

 Redundant Storage Some information is stored repeatedly.


 Update Anomalies if one copy of such repeated data is updated, an inconsistency is
created, unless all copies are similarly updated.
 Insertion anomalies it may not be possible to store certain information unless some other,
unrelated, information is stored.
 Deletion Anomalies It may not be possible to delete certain information without losing
some other, unrelated, information.
The hourly wages depend on rating levels. So, for example, hourly wage 10 for rating level 8 is
repeated three times. The hourly_wages in the first tuple could be updated without making a
similar change in the second tuple. We cannot insert a tuple for an employee unless we know the
hourly wage for the employee’s rating value. If we delete all tuples with a given rating value
(e.g. tuples of Smethurst and Guldu) we lose the association between the rating value and its
hourly_wage value.

Problems with Decompositions

Decompositions should be used only when needed. The information to be stored consists of
SNLRWH tuples. If we just store the projections of these tuples onto SNLRH and RW, are there
any potential problems that we should be aware of?

Problems with Decompositions

There are three potential problems to consider:Some queries become more expensive.

e.g., How much did sailor Joe earn? (salary = W*H)

Given instances of the decomposed relations, we may not be able to reconstruct the
corresponding instance of the original relation!Fortunately, not in the SNLRWH example.
Checking some dependencies may require joining the instances of the decomposed relations.

Tradeoff: Must consider these issues vs. redundancy.

Functional Dependency

Functional dependency (FD) is a set of constraints between two attributes in a relation.


Functional dependency says that if two tuples have same values for attributes A1, A2,..., An,
then those two tuples must have to have same values for attributes B1, B2, ..., Bn.

Functional dependency is represented by an arrow sign (→) that is, X→Y, where X functionally
determines Y. The left-hand side attributes determine the values of attributes on the right-hand
side.

Normalization

If a database design is not perfect, it may contain anomalies, which are like a bad dream for any
database administrator. Managing a database with anomalies is next to impossible.

 Update anomalies − If data items are scattered and are not linked to each other properly,
then it could lead to strange situations. For example, when we try to update one data item
having its copies scattered over several places, a few instances get updated properly while
a few others are left with old values. Such instances leave the database in an inconsistent
state.
 Deletion anomalies − We tried to delete a record, but parts of it was left undeleted
because of unawareness, the data is also saved somewhere else.
 Insert anomalies − We tried to insert data in a record that does not exist at all.

Normalization is a method to remove all these anomalies and bring the database to a
consistent state. NORMALIZATION

Definition: Normalization is the process of efficiently organizing data in a database. There


are two goals of the normalization process: eliminating redundant data (for example, storing
the same data in more than one table) and ensuring data dependencies make sense (only
storing related data in a table). Both of these are worthy goals as they reduce the amount of
space a database consumes and ensure that data is logically stored. There are several benefits
for using Normalization in Database.

Benefits:

a. Eliminate data redundancy


b. Improve performance
c. Query optimization
d. Faster update due to less number of columns in one table
e. Index improvement

Different types of Normal Forms

Un-normalized Form: This is a relation which satisfies all the properties of a relation.
1 Normal Form: A Relation is said to be in the first normal form if it is already in un-
normalized form and it has no repeating groups.
2 Normal Form: A relation is said to be in the second normal form if it is already in the first
normal from and it has no partial dependency.
3 Normal Form: A relation is said to be in the third normal form if it is already in the
second normal from and it has no transitive dependency.
Partial Dependency: In a relation having more than one key field, a subset of non-key fields
may depend on all the key fields but another subset of non-key fields may depend on only
one of the key fields. Such dependency is called partial dependency.
Transitive Dependency: In a relation, there may be dependency among non-key fields. Such
dependency is called as transitive dependency.
BCNF: A relation is said to be in Boyce-Codd normal from if it is already in the third normal
form and every determinant is a candidate key.
Determinant: - The attribute on the left-hand side of the arrow in a functional dependency is
called a determinant. In the below example the combination of SNO, SUBJECT is a
determinant.
SNO, SUBJECT → MARKS.
(Or)
A Determinant is any field or combination of filed on which some other filed is fully
functionally dependent
Super Key: A Super key is a set if one or more attributes that, taken collectively, allows us
to identify uniquely an entity in the entity set.
For example, the social-security attribute of the entity set customer is sufficient to distinguish
one customer entity form another. Thus, social-security is a super key. Similarly, the
combination of customer-name and social-security is a super key for the entity set customer.
The customer-name attribute of customer is not a super key, because several people might
have the same name.
Candidate Key: Super key for which no proper subset is a super key. Such minimal super
keys are called Candidate keys.
Suppose that a combination of customer-name and customer-street is sufficient to distinguish
among member of the customer entity set. Then, both {social-security} and {customer-name,
customer-street} are candidate keys. Although the attributes social-security and customer-
name together can distinguish customer entities, their combination does not form a candidate
key, since the attribute social-security alone is a candidate key.

Fourth Normal Form: A relation is said to be in the fourth normal form if it is already in
Boyce-Codd normal form and it has no mutli valued dependency.
Multivalued Dependency: MVD represents a dependency between attributes. For example
A, B and C in a relation, such that for each value of A there is a set of values for B and a set
of values for C. However, the set of values for B and C are independent of each other.
First Normal Form:

A database is in first normal form if it satisfies the following conditions:

 Contains only atomic values


 There are no repeating groups

An atomic value is a value that cannot be divided. For example, in the table shown below, the
values in the [Color] column in the first row can be divided into "red" and "green", hence
[TABLE_PRODUCT] is not in 1NF.

A repeating group means that a table contains two or more columns that are closely related.
For example, a table that records data on a book and its author(s) with the following
columns: [Book ID], [Author 1], [Author 2], [Author 3] is not in 1NF because [Author 1],
[Author 2], and [Author 3] are all repeating the same attribute.
1st Normal Form Example

How do we bring an unnormalized table into first normal form? Consider the following
example:

This table is not in first normal form because the [Color] column can contain multiple values.
For example, the first row includes values "red" and "green."

To bring this table to first normal form, we split the table into two tables and now we have
the resulting tables:

Now first normal form is satisfied, as the columns on each table all hold just one value.

2nd Normal Form Example

Consider the following example:


This table has a composite primary key [Customer ID, Store ID]. The non-key attribute is
[Purchase Location]. In this case, [Purchase Location] only depends on [Store ID], which is only
part of the primary key. Therefore, this table does not satisfy second normal form.

To bring this table to second normal form, we break the table into two tables, and now we have
the following:

What we have done is to remove the partial functional dependency that we initially had. Now, in
the table [TABLE_STORE], the column [Purchase Location] is fully dependent on the primary
key of that table, which is [Store ID].

3rd Normal Form Example

Consider the following example:


In the table able, [Book ID] determines [Genre ID], and [Genre ID] determines [Genre Type].
Therefore, [Book ID] determines [Genre Type] via [Genre ID] and we have transitive functional
dependency, and this structure does not satisfy third normal form.

To bring this table to third normal form, we split the table into two as follows:

Now all non-key attributes are fully functional dependent only on the primary key. In
[TABLE_BOOK], both [Genre ID] and [Price] are only dependent on [Book ID]. In
[TABLE_GENRE], [Genre Type] is only dependent on [Genre ID].

Fourth Normal Form


Def: -A relation is said to be in the fourth normal form if it is already in Boyce-Codd normal
form and it has no mulivalued dependency.

For example, if we have two multi-valued attributes in a relation, we have to repeat each value of
one of the attributes with every value of the other attribute, to ensure that tuples of the relation
are consistent. This type of constraint is referred to as a multi-valued dependency and result in
data redundancy. Consider the following

COURSE TEACHER TEXT

Physics Prof. Green Basic Mechanics


Prof. Brown Principles of Optics

Prof. Black

Math Prof. White Modern Algebra

Projective Geometry

Fig. Sample tabulation of CTX.

COURSE TEACHER TEXT

Physics Prof. Green Basic Mechanics

Physics Prof. Green Principles of Optics

Physics Prof. Brown Basic Mechanics

Physics Prof. Brown Principles of Optics

Physics Prof. Black Basic Mechanics

Physics Prof. Black Principles of Optics

Math Prof. White Modern Algebra

Math Prof. White Projective Geometry

Fig. Sample tabulation of CTX.

In the above example, teachers Prof. Green, Prof. Brown, Prof. Black belongs to physics course
and basic mechanics and principles of optics are physics text books only. However, as there is no
direct relationship between members of teacher and text at a given course, we create a tuple for
every combination of member of teacher and text to ensure that the relation is consistent. We
represent a MVD between attributes are

COURSE ->> TEACHER

COURSE ->> TEXT

So, MVD exist in the above relation, for example, if we want to add a new text for physics we
would have to create three new tuples, one for each member of teacher, to ensure that the relation
remains consistent. This is an example of an update anomaly caused by the presence of MVD.
We decompose the CTX relation into CT, CX relations as given below:

CT
COURSE TEACHER

Physics Prof. Green

Physics Prof. Brown

Physics Prof. Black

Math Prof. White

CX

COURSE TEXT

Physics Basic Mechanics

Physics Principles of Optics

Math Modern Algebra

Math Projective Geometry

The above new relations are in 4NF there is no MVD. Note that the 4NF relations do not display
data redundancy and the potential for update anomalies is removed. For example, to add a new
text for physics, we simple create a single tuple in the CX relation.

Join Dependency:

A Join dependency is generalization of Multivalued dependency. A Join Dependency {R1,


R2, ..., Rn} is said to hold over a relation R if R1, R2, R3, ..., Rn is a lossless-join decomposition
of R . There is no set of sound and complete inference rules for join dependency.

A join dependency (JD) can be said to exist if the join of R1 and R2 over C is equal to relation R.
Where, R1 and R2 are the decompositions R1(A, B, C), and R2 (C,D) of a given relations R (A, B, C, D).
Alternatively, R1 and R2 is a lossless decomposition of R. In other words, *(A, B, C, D), (C, D) will be a
join dependency of R if the join of the join’s attributes is equal to relation R. Here, *(R1, R2, R3, ....)
indicates that relations R1, R2, R3 and so on are a join dependency (JD) of R

Inclusion Dependency:
An Inclusion Dependency is a statement of the form that some columns of a relation are
contained in other columns. A foreign key constraint is an example of inclusion dependency.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy