CS8492 - DBMS - 1
CS8492 - DBMS - 1
Syllabus
Purpose of Database System - Views of data - Data Models- Database System Architecture -
Introduction to relational databases - Relational Model - Keys - Relational Algebra - SQL
fundamentals - Advanced SQL features - Embedded SQL - Dynamic SQL.
Contents
1.1 Introduction to Database Management
1.2 Purpose of Database System ...................................... May-07, 12, Dec.-04, ........... Marks 8
1.3 Views of Data ................................................................ May-16 ............................... Marks 16
1.4 Data Models ................................................................ Dec.-14, ................................ Marks 8
1.5 Database System Architecture .................................... May-12, 13, 14, 16, 17,
................................................................................. Dec.-08, 15, 17,................... Marks 16
1.6 Data Independence
1.7 Introduction to Relational Databases
1.8 Relational Model
1.9 Keys ........................................................................... May-06, 07, 12, Dec.-06, ..... Marks 4
1.10 Integrity Constraints ................................................. Dec.-05, ............................. Marks 10
1.11 Database integrity
1.12 Relational Algebra..................................................... May-03,04,05,14,15,16,17,18,
................................................................................. Dec.-02,07,08,11,15,16,17 . Marks 16
1.13 SQL Fundamentals ................................................... Dec.-15,............................... Marks 16
1.14 Advanced SQL Features
1.15 Dynamic SQL ............................................................. May-17, Dec.-17, ................ Marks 11
1.16 Two Marks Questions with Answers
(1 - 1)
Database Management Systems 1-2 Relational Databases
Earlier database systems are created in response to manage the commercial data.
These data is typically stored in files. To allow users to manipulate these files
various programs are written for
Database Management Systems 1-3 Relational Databases
8) Security problems : Every user is not allowed to access all the data of database
system. Since application program in file system are added in an ad hoc manner,
enforcing such security constraints become difficult.
Database systems offer solutions to all the above mentioned problems.
Difference between Database System and Conventional File System
Sr. No. Database systems Conventional file systems
3. Database systems are used when Conventional file systems are used where
security constraints are high. there is less demand for security constraints.
4. Database systems define the data in a File systems define the data in un-structured
structured manner. Also there is well manner. Data is usually in isolated form.
defined co-relation among the data.
5. Data inconsistency is less in database Data inconsistency is more in file systems.
systems.
6. User is unknown to the physical User locates the physical address of file to
address of the data used in database access the data in conventional file systems.
systems.
7. We can retrieve the data in any desired We cannot retrieve the data in any desired
format using database systems. format using file systems.
8. There is ability to access the data There is no ability to concurrently access the
concurrently using database systems. data using conventional file system.
3) Data can be isolated in separate tables for convenient and efficient use.
4) Data can be accessed efficiently using a simple query language.
5) The data integrity can be maintained. That means – the constraints can be applied
on data and it should be in some specific range.
6) The atomicity of data can be maintained. That means, if some operation is
performed on one particular table of the database, then the change must be reflected
for the entire database.
7) The DBMS allows concurrent access to multiple users by using the synchronization
technique.
8) The security policies can be applied to DBMS to allow the user to access only
desired part of the database system.
Disadvantages of Database Systems
1) Complex design : Database design is complex, difficult and time consuming.
2) Hardware and software cost : Large amount of investment is needed to setup the
required hardware or to repair software failure.
3) Damaged part : If one part of database is corrupted or damaged, then entire
database may get affected.
4) Conversion cost : If the current system is in conventional file system and if we need
to convert it to database systems then large amount of cost is incurred in purchasing
different tools, and adopting different techniques as per the requirement.
5) Training : For designing and maintaining the database systems, the people need to
be trained
University Questions
1. Compare file system with database system AU : May-07, Marks 8, May-12, Marks 2
Database is a collection of interrelated data and set of programs that allow users to
access or modify the data.
Abstract view of the system is a view in which the system hides certain details of
how the data are stored and maintained.
The main purpose of database systems is to provide users with abstract view of
the data.
The view of the system helps the user to retrieve data efficiently.
For simplifying the user interaction with the system there are several levels of
abstraction - these levels are - Physical level, logical level and view level.
Database Management Systems 1-6 Relational Databases
o This is the next higher level, which describes the what data are stored in database.
o This level also describes the relationship among the data.
o The logical level thus describes then entire database in terms of small number of
relatively simple structures.
The database administrators use logical level of abstraction for deciding what
information to keep in database.
3) View level :
o This is highest level of abstraction that describes only part of the entire database.
o The view level can provide the access to only part of the database.
o This level helps in simplifying the interaction with the system.
o The system can provide multiple views of the same system.
o Clerk at the reservation system, can see only part of the database and can access the
required information of the passenger.
Fig. 1.3.1 shows the relationship among the three levels of abstraction.
Instances : When information is inserted or deleted from the database then the
database gets changed. The collection of information at particular moment is called
instances. For example - following is an instance of student database
RollNo Name Marks
10 AAA 43
20 BBB 67
Database Management Systems 1-8 Relational Databases
Types of Schema : The database has several schema based on the levels of abstraction.
(1) Physical Schema : The physical schema is a database design described at the
physical level of abstraction.
(2) Logical Schema : The logical schema is a database design at the logical level of
abstraction.
(3) Subschema : A database may have several views at the view level which are
called subschemas.
University Question
1. Briefly explain about views of data. AU : May-16, Marks 16
Advantages :
(i) Structural Independence : Structural independence is an ability that allows us to
make changes in one database structure without affecting other. The relational
model have structural independence. Hence making required changes in the
database is convenient in relational database model.
(ii)Conceptual Simplicity : The relational model allows the designer to simply focus
on logical design and not on physical design. Hence relational models are
conceptually simple to understand.
Database Management Systems 1 - 10 Relational Databases
(iii) Query Capability : Using simple query language (such as SQL) user can get
information from the database or designer can manipulate the database structure.
(iv) Easy design,maintenance and usage : The relational models can be designed
logically hence they are easy to maintain and use.
Disadvantages :
(i) Relational model requires powerful hardware and large data storage devices.
(ii) May lead to slower processing time.
(iii) Poorly designed systems lead to poor implementation of database systems.
Advantages :
i) Simple : It is simple to draw ER diagram when we know entities and relationships.
ii) Easy to understand : The design of ER diagram is very logical and hence they are
easy to design and understand.
iii) Effective: It is effective communication tool.
iv) Integrated : The ER model can be easily integrated with Relational model.
v) Easy conversion: ER model can be converted easily into other type of models.
Disadvantages :
i) Loss of information : While drawing ER model some information can be hidden
or lost.
ii) Limited relationships : The ER model can represent limited relationships as
compared to other models.
Database Management Systems 1 - 11 Relational Databases
Advantages :
i) Enriched modelling : The object based data model has capability of modelling the
real world objects.
ii) Reusability : There are certain features of object oriented design such as inheritance,
polymorphism which help in reusability.
iii) Support for schema evolution : There is a tight coupling between data and
applications, hence there is strong support for schema evolution.
iv) Improved performance : Using object based data model there can be significant
improvement in performance using object based data model.
Disadvantages :
i) Lack of universal data model : There is no universally agreed data model for an
object based data model, and most models lack a theoretical foundation.
ii) Lack of experience : In comparison with relational database management the use of
object based data model is limited. This model is more dependent on the skilled
programmer.
iii)Complex : More functionalities present in object based data model make the design
complex.
Advantages
i) Data is not constrained by fixed schema.
ii) It is flexible.
iii) It is portable.
Disadvantage
i) Queries are less efficient than other types of data model.
University Question
1. Write short note on : Data model and its types. AU : Dec.-14, Marks 8
1.5 Database System Architecture AU : May-12, 13, 14, 16, 17, Dec.-08, 15, 17
• The typical structure of typical DBMS is based on relational data model as shown in
Fig. 1.5.1. (Refer page 1-14).
• Consider the top part of Fig. 1.5.1. It shows application interfaces used by naïve
users, application programs created by application programmers, query tools used
by sophisticated users and administration tools used by database administrator
• The two important components of database architecture are - Query processor and
storage manager.
Query processor :
The interactive query processor helps the database system to simplify and
facilitate access to data. It consists of DDL interpreter, DML compiler and query
evaluation engine.
ii) DML compiler : It translates DML statements query language into an evaluation
plan. This plan consists of the instructions which query evaluation engine
understands.
Database Management Systems 1 - 13 Relational Databases
iii) Query evaluation engine : It executes the low-level instructions generated by the
DML compiler.
When a user issues a query, the parsed query is presented to a query optimizer,
which uses information about how the data is stored to produce an efficient
execution plan for evaluating the query. An execution plan is a blueprint for
evaluating a query. It is evaluated by query evaluation engine.
Storage manager :
ii) Transaction manager : Ensures that the database remains in consistent despite
of system failures and concurrent transaction execution proceeds without
conflicting.
iv) Buffer manager : Manages the fetching of data from disk storage into main
memory. The buffer manager also decides what data to cache in main memory.
Buffer manager is a crucial part of database system.
ii) Data dictionary : Used for storing metadata, particularly schema of database.
iii) Indices : Indices are used to provide fast access to data items present in the
database
Database Management Systems 1 - 14 Relational Databases
University Questions
1. Explain the overall architecture of database system in detail.
AU : May-14,17, Dec.-17, Marks 8, May-16, Marks 16
2. With the help of a neat block diagram explain basic architecture of a database management system.
AU : May-12, May 13, Marks 16,Dec.-15, Marks 8
Q. Explain component modules of a DBMS and their interactions with the architecture
AU : Dec 08, Marks 10
By these data independence the time and cost acquired by changes in any one level can
be reduced and abstract view of data can be provided to the user.
103 Electrical 5
104 Civil 3
From this third table we can easily find out that the course to which the RollNo 001 is
admitted is computer Science.
There are some commonly used terms in Relational Model and those are -
Tuple or record or row : The single entry in the table is called tuple. The tuple
represents a set of related data. In above Student table there are four tuples. One of the
tuple can be represented as
001 AAA 88 1111111111
Attribute or columns : It is a part of table that contains several records. Each record
can be broken down into several small parts of data known as attributes. For example the
above table consists of four attributes such as RollNo,Name,Marks and Phone.
Relation schema : A relation schema describes the structure of the relation, with the
name of the relation (i.e. name of table), its attributes and their names and type.
Relation Instance : It refers to specific instance of relation i.e. containing a specific set
of rows. For example – the following is a relation instance – which contains the records
with marks above 80.
Database Management Systems 1 - 18 Relational Databases
Degree : It is nothing but total number of columns present in the relational database. In
given Student table –
The degree is 4.
iv) Degree
v) Cardinality
Solution :
i) No of Columns = 6
ii) No of Tuples= 3
iii) Different attributes are StaffID, Name,Sex, Designation, Salary, DOJ
iv) Degree= Total number of columns=6
v) Cardinality =Total number of rows = 3
Keys are used to specify the tuples distinctly in the given relation.
Various types of keys used in relational model are – Superkey, Candidate Keys,
primary keys, foreign keys. Let us discuss them with suitable example
1) Super Key(SK): It is a set of one or more attributes within a table that can uniquely
identify each record within a table. For example – Consider the Student table as
follows –
Reg No. Roll No Phone Name Marks
Clearly using the (RegNo) and (RollNo,Phone,Name) we can identify the records
uniquely but (Name, Marks) of two students can be same, hence this combination
not necessarily help in identifying the record uniquely.
Thus every candidate key is a superkey but every superkey is not a candidate key.
3) Primary Key(PK): The primary key is a candidate key chosen by the database
designer to identify the tuple in the relation uniquely. For example – Consider the
following representation of primary key in the student table
Database Management Systems 1 - 21 Relational Databases
Other than the above mentioned primary key, various possible primary keys can be
(RollNo), (RollNo,Name), (RollNo, Phone)
The relation among super key, candidate key and primary can be denoted by
Candidate Key=Super Key – Primary Key
Rules for Primary Key
(i) The primary key may have one or more attributes.
(ii) There is only one primary key in the relation.
(iii) The value of primary key attribute can not be NULL.
(iv) The value of primary key attribute does not get changed.
4) Alternate key : The alternate key is a candidate key which is not chosen by the
database designer to uniquely identify the tuples. For example –
Database Management Systems 1 - 22 Relational Databases
5) Foreign key : Foreign key is a single attribute or collection of attributes in one table
that refers to the primary key of other table.
Thus foreign keys refer to primary key.
The table containing the primary key is called parent table and the table
containing foreign key is called child table.
Example -
From above example, we can see that two tables are linked. For instance we could
easily find out that the ‘Student CCC has opted for ComputerSci course’
University Question
1. Explain distinction among the terms primary key, candidate key, foreign key and super key with
suitable example AU : May-06, 07, 12, Dec.-06, Marks 4
The primary key value helps in uniquely identifying every row in the table. Thus if the
users of the database want to retrieve any row from the table or perform any action on
that table, they must know the value of the key for that row. Hence it is necessary that the
primary key should not have the NULL value.
University Question
1. Discuss the entity Integrity and referential integrity constraints. Why are they important ? Explain
them with suitable examples. AU : Dec.-05, Marks 10
Customer
Order
Note that the "CustID" column in the "Order" table points to the "CustID" column
in the "Customer" table.
The "CustID" column in the "Customer" table is the PRIMARY KEY in the
"Customer" table.
The "CustID" column in the "Order" table is a FOREIGN KEY in the "Order" table.
The table containing the foreign key is called the child table, and the table
containing the primary key is called the referenced or parent table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
Database Management Systems 1 - 25 Relational Databases
The FOREIGN KEY constraint also prevents invalid data from being inserted into
the foreign key column, because it has to be one of the values contained in the
table it points to.
The queries present in the relational algebra are denoted using operators.
Each relational algebra is procedural. That means Each relational query describes
a step-by-step procedure for computing the desired answer, based on the order in
which operators are applied in the query.
(1) Selection :
This operation is used to fetch the rows or tuples from the table(relation).
Syntax : The syntax is
predicate(relation)
where σ represents the select operation. The predicate denotes some logic using
which the data from the relation(table) is selected.
For example - Consider the relation student as follows
2 Shyam 18 Male
3 Seeta 16 Female
4 Geeta 23 Female
sname age
Ram 21
Shyam 18
Seeta 16
Geeta 23
Fig. 1.12.2
Student Reserve
sid sname age sid isbn day
1 Ram 21 1 005 07-07-18
4 Geeta 23
Query : Find the names of all the students who have reserved isbn = 005. To
satisfy this query we need to extract data from two table. Hence the cartesian
product operator is used as
(Student.sid = Reserve.sid
^
Reserve.Isbn =005(Student × Reserve)
o For this operation to work, the relations(tables) specified should have same
number of attributes(columns) and same attribute domain. Also the duplicate
tuples are automatically eliminated from the result.
o Syntax : A ∪ B
o where A and B are relations.
o For example : If there are two tables student and book as follows –
Database Management Systems 1 - 29 Relational Databases
Student Book
sid sname age isbn bname Author
1 Ram 21 005 DBMS XYZ
4 Geeta 23
o Query : We want to display both the student name and book names from both the
tables then
Sname(Student) ∪ bname (Book)
(ii) Intersection :
o This operation is used to fetch data from both tables which is common in both
the tables.
o Syntax : A ∩ B
where A and B are relations.
o Query : If we want to find out the names of the students who are working in a
company then
name(Student) ∩ name (Worker)
Name
AAA
DDD
(iii) Set-Difference : The result of set difference operation is tuples, which are
present in one relation but are not in the second relation.
Database Management Systems 1 - 30 Relational Databases
Syntax : A – B
For Example : Consider two relations Full_Time_Employee and Part_Time_Employee, if
we want to find out all the employee working for Fulltime, then the set difference
operator is used -
EmpName(Full_Time_Employee) - EmpName (Part_Time_Employee)
(5) Join : The join operation is used to combine information from two or more relations.
Formally join can be defined as a cross-product followed by selections and projections,
joins arise much more frequently in practice than plain cross-products. The join operator
is used as ⋈
There are three types of joins used in relational algebra
i) Conditional join : This is an operation in which information from two tables is
combined using some condition and this condition is specified along with the join
operator.
A ⋈cB = c(A × B)
Thus ⋈ is defined to be a cross-product followed by a selection. Note that the
condition c can refer to attributes of both A and B. The condition C can be specified
using <,<=,>,<= or = operators.
For example consider two table student and reserve as follows -
Student Reserve
sid sname age sid isbn day
1 Ram 21 1 005 07-07-18
4 Geeta 23
If we want the names of students with sid(Student) = sid(Reserve) and isbn = 005,
then we can write it using Cartesian product as -
ii) Equijoin : This is a kind of join in which there is equality condition between two
attributes(columns) of relations(tables). For example - If there are two table Book
and Reserve table and we want to find the book which is reserved by the student
having isbn 005 and name of the book is ‘DBMS’, then :
Book Reserve
isbn bname Author sid isbn day
005 DBMS XYZ 1 005 07-07-18
iii)Natural Join : When there are common columns and we have to equate these
common columns then we use natural join. The symbol for natural join is simply ⋈
without any condition. For example, consider two tables -
Book Reserve
isbn bname Author sid isbn day
Now if we want to list the books that are reserved, then that means we want to
match Books.isbn with Reserve.isbn. Hence it will be simply
Database Management Systems 1 - 32 Relational Databases
Books ⋈ Reserve
(6) Rename operation : This operation is used to rename the output relation for any
query operation which returns result like Select, Project etc. Or to simply rename a
relation(table). The operator (rho) is used for renaming.
Syntax : (RelationNew, RelationOld)
For example : If you want to create a relation Student_names with sid and sname from
Student, it can be done using rename operator as :
ρ(Student_names, (sid.sname(Student))
(7) Divide operation
The division operator is used when we have to evaluate queries which contain the
keyword ALL.
It is denoted by A/B where A and B are instances of relation.
For example - Find all the customers having accounts in all the branches. For that
consider two tables - Customer and Account as
Customer Account
Name Branch Branch
A Pune Pune
B Mumbai Mumbai
A Mumbai
C Pune
(iii) Find the id of sailors with age over 20 who have not reserved red boat
(iv) Find the names of sailors who have reserved at least one boat
Solution :
(i) (sname((bid=103 Reserves) ⋈ Sailors)
Solution :
a. Select each student who takes at least one course in 2009, display the student
information along with the information about what the courses the student took.
b. Select each student who takes at least one course in 2009, display the student
information along with the information about what the courses the student took but
the selection must be before join operation.
c. Display the ID, Name and Course_id of all the students who took any course in the
university.
Example 1.12.3 Consider following relational database
branch(branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
loan (loan_number, branch_name, amount)
borrower (customer_name, loan_number)
account (account_number, branch_name, balance)
depositor (customer_name, account_number)
i) Find the names of all branches located in “Chennai”.
ii) Find the names of all borrowers who have a loan in branch “ABC”.
Solution :
i) branch_name(branch_city =’Chennai’) (branch))
Solution :
a) person-name(company-name = “First Bank Corporation”(works))
b) person-name, street, city(company-name = “First Bank Corporation” salary > 200000 (works ⋈ employee))
University Questions
1. Explain select, project, cartesian product and join operations in relational algebra with an example
AU : May-18, Marks 13, Dec.-16, Marks 6
2. List operations of relational algebra and purpose of each with example
AU : May-17, Marks 5
3. Differentiate between foreign key constraints and referential Integrity constraints with suitable
example.
AU : Dec.-17, Marks 6
4. Explain various operations in relational algebra with examples
AU : May 03, Marks 10, Dec-07, Marks 8, Dec.- 08, Marks 10, May-14, Marks 16
5. Explain all join operations in relational algebra
AU : May 05, Marks 8
6. Briefly explain relational algebra
AU : May 04, Marks 8
7. What is rename operation in relational algebra ? Illustrate your answer with example
AU : Dec 02, Marks 2
Database Management Systems 1 - 36 Relational Databases
(5) smallint : It is used to store small integer value. It allows machine dependent subset
of integer type.
(6) real : It allows the floating point, double precision numbers.
(7) float(n) : For representing the floating point number with precision of at least n
digits this data type is used.
Syntax
create table table_name;
Example
create table Student
(RollNo int,
Name varchar(10),
Marks numeric(3,2),
Primary key(RollNo));
The primary key attribute must be non null and unique.
(2) Insert : The insert command is used to insert data into the table. There are two
syntaxes of inserting data into SQL
Syntax
i) Insert into table_name (column1, column2, column3, ...)
values (value1, value2, value3, ...);
ii) insert into table_name
values (value1, value2, value3, ...);
Example
(i) insert into Student(RollNo,Name,Makrs) values(101,’AAA’,56.45)
(ii) insert into Student values(101,’AAA’,56.45)
(3) Delete : This command is used to delete the existing record.
Syntax
delete from table_name
where condition;
Example
Delete from student
where RollNo=10
(4) Alter: The alter table statement is used to add, delete, or modify columns in an
existing table.
The alter table statement is also used to add and drop various constraints on an existing
table.
Database Management Systems 1 - 39 Relational Databases
3 Seeta 16 3 009
4 Geeta 23
Query : Find the names of students who have reserved the books with book isbn
Select Student.sname,Reserve.isbn
From Student, Reserve
Where Student.sid=Reserve.sid
Use of SQL Join
The SQL Joins clause is used to combine records from two or more tables in a database. A
JOIN is a means for combining fields from two tables by using values common to each.
Example : Consider two tables for using the joins in SQL. Note that cid is common
column in following tables.
Student Reserve
4 NULL Geeta
1) Inner Join :
The most important and frequently used of the joins is the INNER JOIN. They are
also known as an EQUIJOIN.
The INNER JOIN creates a new result table by combining column values of two
tables (Table1 and Table2) based upon the join-predicate.
The query compares each row of table1 with each row of Table2 to find all pairs of
rows which satisfy the join-predicate.
Database Management Systems 1 - 41 Relational Databases
When the join-predicate is satisfied, column values for each matched pair of rows
of A and B are combined into a result row. It can be represented as :
2) Left Join :
The SQL 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.
This means that a left join returns all the values from the left table, plus matched
values from the right table or NULL in case of no matching join predicate.
It can be represented as -
FROM Table1
LEFT JOIN Table2
ON Table1.common_field = Table2.common_field;
Example : For above given two tables namely Student and City, we can apply Left
join. It will Return all records from the left table, and the matched records from
the right table using the common column cid. The query will be
SELECT *
FROM Student Left Join City on Student.cid=City.cid
The result will be
3) Right Join :
The SQL 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.
It can be represented as follows :
4) Full Join :
The SQL 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.
It can be represented as
Select S.sname,R.isbn
From Student as S, Reserve as R
Where S.sid=R.sid
In above case we could shorten the names of tables Student and Reserve as S and R
respectively.
Another reason to rename a relation is a case where we wish to compare tuples in the
same relation. We then need to take the Cartesian product of a relation with itself. For
example –
If the query is – Find the names of students who reserve the book of isbn 005. Then the
SQL statement will be –
Select S.sname,R.isbn
From Student as S, Reserve as R
Where S.sid=R.sid and S.isbn=005
2) Attribute Specification in Select clause : The symbol * is used in select clause to
denote all attributes. For example – To select all the records from Student table we
can write
Select* from Student
3) Ordering the display of tuples : For displaying the records in particular order we
use order by clause.
The general syntax with ORDER BY is
SELECT column_name(s)
FROM table_name
WHERE condition
ORDER BY column_name(s)
Example : Consider the Student table as follows –
Database Management Systems 1 - 45 Relational Databases
2 BBB 70 Mumbai
3 CCC 90 Pune
4 DDD 55 Mumbai
Database Management Systems 1 - 46 Relational Databases
Syntax of AND
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example : Find the student having name “AAA” and lives in city “Pune”
SELECT *
FROM Students
Where sname=’AAA’ AND city=’Pune’
Output
Syntax OR
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example : Find the student having name “AAA” OR lives in city “Pune”
SELECT *
FROM Students
Where sname=’AAA’ OR city=’Pune’
Output
3 CCC 90 Pune
Syntax NOT
SELECT column1, column2, ..
FROM table_name
WHERE NOT condition
Example : Find the student who do not have city “Pune”
SELECT *
FROM Students
Where NOT city=’Pune’
Output
The second part of the definition means, for example, that the set of fields
{RollNo, Name} is not a key for Students, because this set properly contains the
key {RollNo}. The set {RollNo, Name} is an example of a superkey, which is a set
of fields that contains a key.
The key constraint can be specified using SQL as follows -
o In SQL, we can declare that a subset of the columns of a table constitute a key by
using the UNIQUE constraint.
o At most one of these candidate keys can be declared to be a primary key, using
the PRIMARY KEY constraint. For example -
CREATE TABLE Student(RollNo integer,
Name CHAR(20),
age integer,
UNIQUE(Name,age),
CONSTRAINT StudentKey PRIMARY KEY(RollNo))
This definition says that RollNo is a Primary key and Combination of Name and
age is also a key.
o ‘Data%’ matches any string beginning with “Data”, For instance it could
be with “Database”, “DataMining”,”DataStructure”
o ‘_ _ _’ matches any string of exactly three characters.
o ‘_ _ _ %’matches any string of at least length 3 characters.
Database Management Systems 1 - 49 Relational Databases
The LIKE clause can be used in WHERE clause to search for specific patterns.
For example – Consider following Employee Database
(1) Find all the employee with EmpName starting with “s”
SQL Statement:
SELECT * FROM Employee
WHERE EmpName LIKE ‘s%’
Output
EmpID EmpName Department Date_of_Join
1 Sunil Marketing 1-Jan
3 Supriya Manager 3-Jan
4 Sonia Accounts 4-Jan
5 Suraj Sales 5-Jan
(2) Find the names of employee whose name begin with S and end with a
SQL Statement :
SELECT EmpName FROM Employee
WHERE EmpName LIKE ‘S%a’
Output
EmpName
Supriya
Sonia
(3) Find the names of employee whose name begin with S and followed by exactly
four characters
SELECT EmpName FROM Employee
WHERE EmpName LIKE ‘S_ _ _ _ ‘
Database Management Systems 1 - 50 Relational Databases
Output
EmpName
Sunil
Sonia
Suraj
Syntax
The basic syntax of a UNION clause is as follows –
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Here, the given condition could be any given expression based on your requirement.
Consider Following relations –
Student Reserve
sid sname age sid isbn day
1 Ram 21 1 005 07-07-18
4 Geeta 23
Database Management Systems 1 - 51 Relational Databases
Book
isbn bname Author
005 DBMS XYZ
006 OS PQR
Example : Find the names of the students who have reserved the ‘DBMS’ book or ‘OS’
Book
The query can then be written by considering the Student, Reserve and Book table as
SELECT S.sname
FROM Student S, Reserve R, Book B
WHERE S.sid=R.sid AND R.isbn=B.isbn AND B.bname=’DBMS’
UNION
SELECT S.sname
FROM Student S, Reserve R, Book B
WHERE S.sid=R.sid AND R.isbn=B.isbn AND B.bname=’OS’
2) Intersect : The common entries between the two tables can be represented with the
help of Intersect operator. It replaces the AND operator in the query.
Syntax
The basic syntax of a INTERSECT clause is as follows –
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
INTERSECT
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Example : Find the students who have reserved both the ‘DBMS’ book and ‘OS’ Book
The query can then be written by considering the Student, Reserve and Book table as
SELECT S.sid, S.sname
FROM Student S, Reserve R, Book B
WHERE S.sid=R.sid AND R.isbn=B.isbn AND B.bname=’DBMS’
INTERSECT
SELECT S.sname
FROM Student S, Reserve R, Book B
WHERE S.sid=R.sid AND R.isbn=B.isbn AND B.bname=’OS’
3) Except : The EXCEPT clause is used to represent the set-difference in the query.
This query is used to represent the entries that are present in one table and not in other.
Database Management Systems 1 - 52 Relational Databases
Syntax :
The basic syntax of a EXCEPT clause is as follows –
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
EXCEPT
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Example : Find the students who have reserved both the ‘DBMS’ book but not reserved
‘OS’ Book
The query can then be written by considering the Student, Reserve and Book table as
SELECT S.sid, S.sname
FROM Student S, Reserve R, Book B
WHERE S.sid=R.sid AND R.isbn=B.isbn AND B.bname=’DBMS’
EXCEPT
SELECT S.sname
FROM Student S, Reserve R, Book B
WHERE S.sid=R.sid AND R.isbn=B.isbn AND
B.bname=’OS’
The max function is used to get the maximum value from the specified column.
For example – Consider the above created Test table
SQL Statement
SELECT Max(value)
FROM Test
Output
400
The sum function is used to get total sum value from the specified column. For
example – Consider the above created Test table
SQL Statement
SELECT sum(value)
FROM Test
Output
1000
1 AAA 60 Pune
2 BBB 70 Mumbai
3 CCC 90 Pune
4 DDD 55 Mumbai
5 EEE 84 Chennai
Query : Find the total marks of each student in the city named ‘Pune’ and ‘Mumbai’ only
SELECT SUM(marks), city
FROM Student
GROUP BY city
HAVING city IN(‘Pune’,’Mumbai’)
Database Management Systems 1 - 56 Relational Databases
Output
The result will be as follows –
SUM(marks) city
150 Pune
125 Mumbai
4 Geeta 4444
Student_City
sid cid
1 101
1 103
2 101
3 102
4 102
4 103
Database Management Systems 1 - 57 Relational Databases
Example 1 - If we want to find out sid who live in city ‘Pune’ or ‘Chennai’.
We can then write independent nested query using IN operator. Here we can
use the IN operator allows you to specify multiple values in a WHERE
clause. The IN operator is a shorthand for multiple OR conditions.
Step 2 : Using cid obtained in step 1 we can find the sid. The query will be
SELECT sid
FROM Student_City
WHERE cid IN
(SELECT cid FROM City WHERE cname=’Pune’ or cname=’Chennai’)
The inner query will return a set with members 101 and 103 and outer query will return
those sid for which cid is equal to any member of set (101 and 103 in this case). So, it will
return 1, 2 and 4.
Example 2 : If we want to find out sname who live in city ‘Pune’ or ‘Chennai’.
SELECT sname FROM Student WHERE sid IN
(SELECT sid FROM Student_City WHERE cid IN
(SELECT cid FROM City WHERE cname=’Pune’ or cname=’Chennai’))
Syntax
delete from table_name
where condition;
Example
delete from student
where RollNo=10
2. Insertion : The insert command is used to insert data into the table. There are two
syntaxes of inserting data into SQL
Syntax
(i) Insert into table_name (column1, column2, column3, ...)
values (value1, value2, value3, ...);
(ii) insert into table_name
values (value1, value2, value3, ...);
Example
(i) insert into Student(RollNo,Name,Makrs) values(101,’AAA’,56.45)
(ii) insert into Student values(101,’AAA’,56.45)
3. Update : The update statement is used to modify the existing records in the table.
update table_name
set column1=value1, column2=value2,…
where condition;
Example:
Delete student
Set Name=’WWW’
where RollNo=101
Example 1.13.1 Write the DDL, DML, DCL for the students database. Which contains
student details:name, id,DOB, branch, DOJ.
Course details : Course name, Course id, Stud.id,Faculty name, id, marks
AU : Dec.-17, Marks 15
Solution :
DDL Commands
CREATE TABLE Student
(
stud_name varchar(20),
stud_id int(3),
DOB varchar(15),
branch varchar(10),
DOJ varchar(15),
);
CREATE TABLE Course
(
Database Management Systems 1 - 59 Relational Databases
course_name varchar(20),
course_id int(5),
stud_id int(3),
facult_name varchar(20),
faculty_id varchar(5),
marks real
);
DML Commands
The commands which we will use here are insert and select. The insert command is
used to insert the values into database tables. Using the select command, the database
values can be displayed.
(1) Inserting values into Student table
insert into Student(stud_name,stud_id,DOB,branch,DOJ)
values(’AAA’,11,’01-10-1999’ , ’computers’,’5-3-2018’)
The DCL command is used to control privileges in Database. To perform any operation
in the database, such as for creating tables, sequences or views, a user needs privileges.
We will use the command GRANT.
To allow a user to create tables in the database, we can use the below command,
Grant create table to user1;
Example 1.13.2 C Write the following queries in relational algebra and SQL
(i) Find the names of employee who have borrowed a book published by McGraw Hill
(ii) Find the names of employees who have borrowed all books published by McGraw-Hill
AU : May 17, Marks 10
Solution :
We will assume the databases as –
member(memb_no, name, dob)
books(isbn, title, authors, publisher)
borrowed(memb_no, isbn, date)
(i) Relational Algebra :
name((publisher=’McGraw Hill’ books) ⋈ borrowed ⋈ member)
SQL :
SELECT name
FROM member
WHERE meber.memb_no=borrowed.memb_no
AND books.isbn=borrowed.isbn
AND books.publisher=’McGraw Hill’;
(ii) Relational Algebra
(Tempname,( memb_no,isbn borrowed)/ isbn (publisher=’McGraw Hill’ books)))
name(Tempname ⋈ member)
SQL :
SELECT distinct M.name
FROM Member M,
WHERE NOT EXIST
(
(SELECT isbn
FROM books
WHERE publisher = ’McGrawHill’
)
EXCEPT
(SELECT isbn
FROM borrowed R
Database Management Systems 1 - 61 Relational Databases
Solution :
(i)
SELECT P.name,C.description
FROM Professor P, Course C
WHERE P.ProfessorNumber=C.ProfessorNumber
HAVING count(DISTINCT P.name)=2
(ii)
SELECT P.name,C.description
FROM Professor P, Course C
WHERE P.ProfessorNumber=C.ProfessorNumber
(iii)
SELECT P.name,P.office, C.description
FROM Professor P, Course C
WHERE P.ProfessorNumber=C.ProfessorNumber
Database Management Systems 1 - 63 Relational Databases
(iv)
SELECT S.StudentNumber,S.StudentNumber,C.Description
FROM Student S, Course C, Registration R
WHERE S.StudentNumber=R.StudentNumber AND C.CourseNumber=R.CourseNumber
(v)
SELECT S.StudentName, P.Name
FROM Student S, Course C, Professor P, Registration R
WHERE C.ProfessorNumber=P.ProfessorNumber
AND C.CourseNumber=R.CourseNumber
AND S.StudentNumber=R.StudentNumber
GROUP BY P.ProfessorNumber
(vi)
SELECT S.StudentName, C.Description
FROM Student S, Course C, Registration R
WHERE S.StudentNumber=R.StudentNumber
AND R.CourseNumber=C.CourseNumber
GROUP BY C.CourseNumber
University Questions
1. Explain aggregate functions in SQL with example. AU : May 18, Marks 13
2. Write DDL, DML,DCL commands for the students database. AU : Dec 17, Marks 7
5. Explain the six clauses in the syntax of SQL query and show what type of constructs can be specified in
each of the six clauses. Which of the six clauses are required and which are optional.
AU : Dec 15, Marks 16
6. Explain- DDL and DML AU : Dec 14, Marks 8
The high level languages which supports embedding SQLs within it are also known as
host language.
Database Management Systems 1 - 64 Relational Databases
query_error:
printf ("SQL error: %ld\n", sqlca->sqlcode);
exit();
bad_number:
printf ("Invalid order number.\n");
exit();
}
Database Management Systems 1 - 65 Relational Databases
University Questions
1. What is the need of embedded SQL. AU : May 17, Dec 17, Marks 2
2. What is embedded SQL ? Give an example AU : Dec 16, Marks 5, May-14, Dec 14, Marks 8
Dynamic SQL is a programming technique which allows to build the SQL statements
dynamically at runtime.
Dynamic SQL statements are not embedded in the source program but stored as strings
of characters that are manipulated during a program's runtime.
These SQL statements are either entered by a programmer or automatically generated
by the program.
Dynamic SQL statements also may change from one execution to the next without
manual intervention.
Dynamic SQL facilitates automatic generation and manipulation of program modules
for efficient automated repeating task preparation and performance.
Dynamic SQL facilitates the development of powerful applications with the ability to
create database objects for manipulation according to user input.
The simplest way to execute a dynamic SQL statement is with an EXECUTE
IMMEDIATE statement. This statement passes the SQL statement to the DBMS for
compilation and execution.
Example 1.15.1 Consider the relation student(Reg.No.,name,mark, and grade). Write
embedded dynamic SQL program in C language to retrieve all the students’ records whose
mark is more than 90. AU : May 17, Marks 11, Dec 17, Marks 6
Solution :
int main() {
/* Begin program */
EXEC SQL INCLUDE SQLCA;
Database Management Systems 1 - 66 Relational Databases
Ans. :
A Database Management System (DBMS) is collection of interrelated data and various
programs that are used to handle the data.
The primary goal of DBMS is to provide a way to store and retrieve the required
information from the database in convenient and efficient manner.
In addition, the database systems must ensure the safety of information stored.
1) DBMS removes the data redundancy that means there is no duplication of data in
database.
2) DBMS allows to retrieve the desired data in required format.
3) Data can be isolated in separate tables for convenient and efficient use.
4) Data can be accessed efficiently using a simple query language.
Ans. : Data abstraction means retrieving only required amount of information of the
system and hiding background details.
Q.5 What are three levels of data abstraction ? AU : Dec 02, 04,May 14, Dec 17
1. Physical Level
2. Logical Level
3. View Level
Q.6 Is it possible for several attributes to have same domain ? Illustrate your answer
with suitable example AU : Dec 04, Dec 15
Ans. : A domain is the set of legal values that can be assigned to an attribute. Each
attribute in a database must have a well-defined domain; we can’t mix values from
different domains in the same attribute. Hence it is not possible for several attributes to
have same domain.
For example - Student domain has attributes RollNo, Name, Address. Similarly
Employee domain has EmpID, Ename,Salary,Address. We can not define the same
domain for defining several attributes.
Q.7 Write the characteristic that distinguish the database approach with File based
approach AU : May 15, Dec 16
OR What are main differences between file processing system and a DBMS ?
AU : May 06, Dec 06
Ans. :
It is a collection of conceptual tools for describing data, relationships among data,
semantics (meaning) of data and constraints.
Data model is a structure below the database.
Q.12 What is data definition language ? Give example AU : Dec 16, May 18
Ans. :
Data Definition Language (DDL) is a specialized language used to specify a database
schema by a set of definitions.
It is a language which is used for creating and modifying the structures of tables,
views, indexes and so on.
Some of the common commands used in DDL are -CREATE, ALTER, DROP.
Ans. : DCL stands for Data Control Language. It includes commands such as GRANT
and REVOKE which mainly deals with the rights, permissions and other controls of
the database system.
Q.15 Why does SQL allow duplicate tuples in a table or in a query result ? AU : Dec 15
Ans. :
Data can be the same. Two people may have the same name. Since SQL is a database
where you store your data and data can be duplicate.
Database Management Systems 1 - 69 Relational Databases
But we can apply primary key constraints, Unique constraints or Distinct keyword to
identify the record uniquely
Q.16 Why key is essential? Write the different types of keys AU : Dec 04
Ans. :
Keys are used to specify the tuples distinctly in the given relation.
Various types of keys used in relational model are – Superkey, Candidate Keys,
primary keys, foreign keys.
Ans. :
The primary key is a candidate key chosen by the database designer to identify the
tuple in the relation uniquely.
For example – Consider a Student database as Student (RollNo,Name,Address). The
primary key for this database is RollNo.The primary is underlined.
Ans. :
Foreign key is a single attribute or collection of attributes in one table that refers to the
primary key of other table.
For example - Consider a Student database as Student (RollNo,Name,Address) and
Course(CourseId, CourseName, RollNo). Here RollNo is a foreign key
Q.19 What is the difference between primary key and foreign key ? AU : Dec 05
Ans. :
Primary Key Foreign Key
Primary key is a column or a set of Foreign key is a column or a set of
columns that can be used to uniquely columns that refer to a primary key or a
identify a row in a table candidate key of another table.
A table can have a single primary key, A table can have multiple foreign keys
that can reference different tables.
Ans. :
The referential integrity rule states that “whenever a foreign key value is used it
must reference a valid, existing primary key in the parent table”.
Database Management Systems 1 - 70 Relational Databases
Example : Consider the situation where you have two tables : Employees and
Managers. The Employees table has a foreign key attribute entitled ManagedBy,
which points to the record for each employee’s manager in the Managers table.
Ans. : Domain integrity ensures that all the data items in a column fall within a
defined set of valid values. Each column in a table has a defined set of values, such as the
set of all numbers for zip (five-digit), the set of all character strings for name.
Q.22 What are different types of integrity constraints used in designing relational
databases
AU : Dec 07
Q.23 List the reasons why null value might be introduced into the database AU : May 06
Ans. : NULL is a special value provided by database in two cases – i) When field
values of some tuples are unknown(For e.g. city name is not assigned) and
ii) inapplicable(For e.g. middle name is not present).
Q.25 Describe briefly any two undesirable properties that a database design may have ?
AU : Dec 02
Ans. : The two undesirable properties that a database design may have –
Q.26 Specify with suitable examples, the different types of keys used in database
management system. AU : Dec 02
Ans. : Data independence is an ability by which one can change the data at one level
without affecting the data at another level. Here level can be physical, conceptual or
external.
Q.29 What is meant by instance and Schema of the database AU : May 04, Dec 05
Ans. :
When information is inserted or deleted from the database then the database gets
changed. The collection of information at particular moment is called instances.
The overall design of the database is called schema
Ans:
Sr.No. Static SQL Dynamic SQL
Database Management Systems 1 - 72 Relational Databases
Notes
UNIT - II
Syllabus
Entity-Relationship model - E-R Diagrams - Enhanced-ER Model - ER-to-Relational Mapping -
Functional Dependencies - Non-loss Decomposition - First, Second, Third Normal Forms,
Dependency Preservation - Boyce/Codd Normal Form - Multi-valued Dependencies and Fourth
Normal Form - Join Dependencies and Fifth Normal Form.
Contents
2.1 Introduction to Entity Relationship Model
2.2 Mapping Cardinality
2.3 ER Diagrams
2.4 Enhanced ER Model
2.5 Examples based on ER Diagram
2.6 ER to Relational Mapping ................................. May-17, .............................. Marks 13
2.7 Concept of Relational Database Design
2.8 Functional Dependencies
2.9 Concept of Redundancy and Anomalies
2.10 Decomposition ................................................... Dec.-17, ................................ Marks 7
2.11 Normal Forms ........................................................ Dec.-14, 15, May-18 ........... Marks 16
2.12 Boyce / Codd Normal Form (BCNF)
2.13 Multivalued Dependencies and Fourth Normal Form May-14, Dec.-16 ................ Marks 16
2.14 Join Dependencies and Fifth Normal Form
2.15 Two Marks Questions with Answers
(2 - 1)
Database Management Systems 2-2 Database Design
2.1.2 ER Model
The ER data model specifies enterprise schema that represents the overall logical
structure of a database.
The E-R model is very useful in mapping the meanings and interactions of real-world
entities onto a conceptual schema.
The ER model consists of three basic concepts –
1) Entity Sets
Entity : An entity is an object that exists and is distinguishable from other objects.
For example - Student named “Poonam” is an entity and can be identified by her
name. The entity can be concrete or abstract. The concrete entity can be - Person,
Book, Bank. The abstract entity can be like - holiday, concept entity is represented
as a box.
Student Employee Department
Entity set : The entity set is a set of entities of the same types. For example - All
students studying in class X of the School. The entity set need not be disjoint. Each
entity in entity set have the same set of attributes and the set of attributes will
Database Management Systems 2-4 Database Design
distinguish it from other entity sets. No other entity set will have exactly the same
set of attributes.
2) Relationship Sets
Relationship is an association among two or more entities.
The relationship set is a collection of similar relationships. For example - Following
Fig. 2.1.2 shows the relationship works_for for the two entities Employee and
Departments.
The association between entity sets is called as participation. that is, the entity sets E1,
E2, . . . , En participate in relationship set R.
The function that an entity plays in a relationship is called that entity’s role.
3) Attributes
Attributes define the properties of a data object of entity. For example if student is an
entity, his ID, name, address, date of birth, class are its attributes. The attributes help
in determining the unique entity. Refer Fig. 2.1.3 for Student entity set with attributes
- ID, name, address. Note that entity is shown by rectangular box and attributes are
shown in oval. The primary key is underlined.
Types of Attributes
3) Derived attribute :
Derived attributes are the attributes that contain values that are calculated from other
attributes. To represent derived attribute there is dotted ellipse inside the solid ellipse. For
example –Age can be derived from attribute DateOfBirth. In this situation, DateOfBirth
might be called Stored Attribute.
Fig. 2.1.4
Database Management Systems 2-6 Database Design
2.3 ER Diagrams
An E-R diagram can express the overall logical structure of a database graphically.E-R
diagrams are used to model real-world objects like a person, a car, a company and the
relation between these real-world objects.
Features of ER model
i) E-R diagrams are used to represent E-R model in a database, which makes them
easy to be converted into relations (tables).
ii) E-R diagrams provide the purpose of real-world modeling of objects which makes
them intently useful.
iii) E-R diagrams require no technical knowledge and no hardware support.
iv) These diagrams are very easy to understand and easy to create even by a naive user.
v) It gives a standard solution of visualizing the data logically.
Relationship : Rhombus is
used to setup relationships
between two or more
entities.
Derived attribute :
Derived attributes are
those which are derived
based on other attributes,
for example, age can be
derived from date of birth.
To represent a derived
attribute, another dotted
ellipse is created inside the
main ellipse
Multivalued attribute : An
attribute that can hold
multiple values is known
as multivalued attribute.
We represent it with
double ellipses in an E-R
Diagram. E.g. A person can
have more than one phone
numbers so the phone
number attribute is
multivalued.
Database Management Systems 2-9 Database Design
ii) One to many : When entity A is associated with more than one entities at a time
then there is one to many relation. For example - One customer places order at a
time.
ii) Many to one : When more than one entities are associated with only one entity then
there is is many to one relation. For example - Many student take a
ComputerSciCourse.
iii) Many to many : When more than one entities are associated with more than one
entities. For example -Many teachers can teach many students.
Now consider the CUSTOMER entity, and that the customer buys products. If all
customers pay the same price for a product, regardless of supplier, then you have
a simple binary relationship between CUSTOMER and PRODUCT. For the
CUSTOMER/ PRODUCT relationship, the intersection attribute is retail_price.
Single ternary relation : Now consider a different scenario. Suppose the customer
buys products but the price depends not only on the product, but also on the
supplier. Suppose you needed a customerID, a productID, and a supplierID to
identify a price. Now you have an attribute that depends on three things and
hence you have a relationship between three entities (a ternary relationship) that
will have the intersection attribute, price.
Database Management Systems 2 - 12 Database Design
The entity set that has primary key is called as strong entity set
4. The member of strong entity set is The member of weak entity set is called
called as dominant entity set subordinate entity set.
6. The primary key is one of the The primary key of weak entity set is a
attributes which uniquely identifies combination of partial key and primary
its member. key of the strong entity set.
For example – There can be two subclass entities namely Hourly_Emps and
Contract_Emps which are subclasses of Empoyee class. We might have attributes
hours_worked and hourly_wage defined for Hourly_Emps and an attribute
contractid defined for ContractEmps.
Therefore, the attributes defined for an Hourly_Emps entity are the attributes for
Employees plus Hourly_Emps. We say that the attributes for the entity set
Employees are inherited by the entity set Hourly_Emps and that Hourly-Emps
ISA (read is a) Employees. It can be represented by following Fig. 2.4.1.
Fig. 2.4.1
3) Overlapping : When some entity can be a member of more than one subclasses. For
example - Person can be both a Student or a Staff. The And can be used to represent
this constraint.
2.4.3 Aggregation
A feature of the entity relationship model that allows a relationship set to participate in
another relationship set. This is indicated on an ER diagram by drawing a dashed box
around the aggregation.
For example - We treat the relationship set work and the entity sets employee and
project as a higher-level entity set called work.
OR Write short notes on : E-R diagram for banking system . AU : Dec.-14, Marks 8
Database Management Systems 2 - 17 Database Design
Solution :
Example 2.5.2 Consider the relation schema given in Figure. Design and draw an ER
diagram that capture the information of this schema. AU : May-17, Marks 5
Employee(empno,name,office,age)
Books(isbn,title,authors,publisher)
Loan(empno,isbn,date)
Database Management Systems 2 - 18 Database Design
Solution :
Example 2.5.3 Construct an E-R diagram for a car insurance company whose customers own
one or more cars each.Each car has associated with it zero to any number of recorded
accidents. Each insurance policy covers one or more cars and has one or more premium
payments associated with it. Each payment is for particular period of time and has an
associated due date and date when the payment was received. AU : Dec.-16, Marks 7
Solution :
Example 2.5.4 A car rental company maintains a database for all vehicles in its current fleet.
For all vehicles, it includes the vehicle identification number license number, manufacturer,
model, date of purchase and color. Special data are included for certain types of vehicles.
Database Management Systems 2 - 19 Database Design
Solution :
Example 2.5.5 Draw E-R diagram for the "Restaurant Menu Ordering System", which will
facilitate the food items ordering and services within a restaurant. The entire restaurant
scenario is detailed as follows. The customer is able to view the food items menu, call the
waiter, place orders and obtain the final bill through the computer kept in their table. The
Waiters through their wireless tablet PC are able to initialize a table for customers, control
the table functions to assist customers, orders, send orders to food preparation staff (chef)
and finalize the customer's bill. The Food preparation staffs (chefs), with their touch-display
interfaces to the system, are able to view orders sent to the kitchen by waiters. During
preparation they are able to let the waiter know the status of each item, and can send
notifications when items are completed. The system should have full accountability and
logging facilities, and should support supervisor actions to account for exceptional
Database Management Systems 2 - 20 Database Design
circumstances, such as a meal being refunded or walked out on. AU : May-15, Marks 16
Solution :
Example 2.5.6 A university registrar’s office maintains data about the following entities :
(1) courses, including number, title, credits, syllabus, and prerequisites;
(2) course offerings, including course number, year, semester, section number,
instructor(s), timings, and classroom;
(3) students, including student-id, name, and program; and
(4) instructors, including identification number, name, department, and title.
Further, the enrollment of students in courses and grades awarded to students in each
course they are enrolled for must be appropriately modeled. Construct an E-R diagram for
the registrar’s office. Document all assumptions that you make about the mapping
constraints.
AU : Dec.-13, Marks 10
Database Management Systems 2 - 21 Database Design
Solution :
The above ER model contains the redundant information, because every Employee,
Project, Machinery combination in works_on relationship is also considered in manages
Database Management Systems 2 - 22 Database Design
We can then create a binary relationship manages for between Manager and
(Employee, Project, Machinery).
Example 2.5.8 Construct an E-R diagram for a hospital with a set of patients and a set of
medical doctors. Associate with each patient a log of the various tests and examinations
conducted. AU : Dec.-07, Marks 8
Solution :
Database Management Systems 2 - 23 Database Design
In this section we will discuss how to map various ER model constructs to Relational
Model construct.
The SQL statement captures the information for above ER diageam as follows -
Declare foreign key constraints for all these fields from the entity sets.
For example - Consider following ER model
The SQL statement captures the information for relationship present in above ER
diagram as follows -
o By this approach the relationship associated with more than one entities is
separately represented using a table. For example - Consider following ER
diagram. Each Dept has at most one manager, according to the key
constraint on Manages.
Database Management Systems 2 - 25 Database Design
Here the constraint is each department has at the most one manager to manage it.
Hence no two tuples can have same DeptID. Hence there can be a separate table
named Manages with DeptID as Primary Key. The table can be defined using
following SQL statement
Approach 2 :
Method 1 : All the entities in the relationship are mapped to individual tables
InventoryItem(ID , name)
Book(ID,Publisher)
DVD(ID, Manufacturer)
Method 2 : Only subclasses are mapped to tables. The attributes in the superclass
are duplicated in all subclasses. For example -
Book(ID,name,Publisher)
DVD(ID, name,Manufacturer)
Method 3 : Only the superclass is mapped to a table. The attributes in the subclasses
are taken to the superclass. For example -
InventoryItem(ID , name,Publisher,Manufacturer)
This method will introduce null values. When we insert a Book record in the table, the
Manufacturer column value will be null. In the same way, when we insert a DVD record
in the table, the Publisher value will be null.
Database Management Systems 2 - 28 Database Design
Example 2.6.1 Construct an E-R diagram for a hospital with a set of patients and a set of
medical doctors. Associate with each patient a log of the various tests and examinations
conducted. Also construct appropriate tables for the ER diagram you have drawn.
Solution :
ER Diagram - Refer example 2.5.8.
Relational Mapping
University Question
1. Discuss the correspondence between the ER model construct and the relational model constructs.
Show how each ER model construct can be mapped to the relational model. Discuss the option for
mapping EER construct. AU : May-17, Marks 13
R N
1 AAA
2 BBB
3 CCC
4 DDD
5 EEE
Here, R->N is true. That means the functional dependency holds true here. Because for
every assigned RollNuumber of student there will be unique name. For instance : The
name of the Student whose RollNo is 1 is AAA. But if we get two different names for the
same roll number then that means the table does not hold the functional dependency.
Following is such table -
R N
1 AAA
2 BBB
3 CCC
1 XXX
2 YYY
In above table for RollNumber 1 we are getting two different names - “AAA” and
“XXX”. Hence here it does not hold the functional dependency.
B->D
B->E
Step 2 : Find the redundant entries and delete them. This can be done as follows -
A->CD
B->AE
This is a minimal cover or Canonical cover of functional dependencies.
2.9 Concept of Redundancy and Anomalies
Definition : Redundancy is a condition created in database in which same piece of
data is held at two different places.
Redundancy is at the root of several problems associated with relational schemas.
Problems caused by redundancy : Following problems can be caused by redundancy-
i) Redundant storage : Some information is stored repeatedly.
ii) Update anomalies : If one copy of such repeated data is updated then inconsistency
is created unless all other copies are similarly updated.
iii) Insertion anomalies : Due to insertion of new record repeated information get
added to the relation schema.
iv) Deletion anomalies : Due to deletion of particular record some other important
information associated with the deleted record get deleted and thus we may lose
some other important information from the schema.
Example : Following example illustrates the above discussed anomalies or redundancy
problems
Consider following Schema in which all possible information about Employee is
stored.
Database Management Systems 2 - 35 Database Design
1) Redundant storage : Note that the information about DeptID, DeptName and
DeptLoc is repeated.
2) Update anomalies : In above table if we change DeptLoc of Pune to Chennai, then
it will result inconsistency as for DeptID 101 the DeptLoc is Pune. Or otherwise, we
need to update multiple copies of DeptLoc from Pune to Chennai. Hence this is an
update anomaly.
3) Insertion anomalies : For above table if we want to add new tuple say
(5, EEE,50000) for DeptID 101 then it will cause repeated information of
(101, XYZ,Pune) will occur.
4) Deletion anomalies : For above table, if we delete a record for EmpID 4, then
automatically information about the DeptID 102,DeptName PQR and DeptLoc
Mumbai will get deleted and one may not be aware about DeptID 102. This causes
deletion anomaly.
2.10 Decomposition AU : Dec.-17, Marks 7
Decomposition is the process of breaking down one table into multiple tables.
Formal definition of decomposition is -
A decomposition of relation Schema R consists of replacing the relation Schema by
two relation schema that each contain a subset of attributes of R and together
include all attributes of R by storing projections of the instance.
For example - Consider the following table
Employee_Department table as follows -
Employee Table
Eid Ename Age City Salary
E001 ABC 29 Pune 20000
E002 PQR 30 Pune 30000
E003 LMN 25 Mumbai 5000
Database Management Systems 2 - 36 Database Design
Department Table
Deptid Eid DeptName
D001 E001 Finance
D002 E002 Production
D003 E003 Sales
D004 E004 Marketing
D005 E005 Human Resource
The decomposition is used for eliminating redundancy.
For example : Consider following relation Schema R in which we assume that the
grade determines the salary, the redundancy is caused
Schema R
Hence, the above table can be decomposed into two Schema S and T as follows :
Schema S Schema T
Name eid deptname Grade Grade Salary
AAA 121 Accounts 2 2 8000
AAA 132 Sales 3 3 7000
BBB 101 Marketing 4 4 7000
CCC 106 Purchase 2 2 8000
3) Checking some dependencies may require joining the instances of the decomposed
relations.
4) There may be loss of information during decomposition.
Example 2.10.2 Consider the following relation R(A,B,C,D,E,F) and FDs A->BC, C->A,
D->E, F->A, E->D is the decomposition of R into R1(A,C,D), R2(B,C,D), and R3(E,F,D).
Check for lossless.
Solution :
Step 1 : R1 R2 R3=R. Here the first condition for checking lossless join is satisfied
as (A,C,D)∪ (B,C,D) ∪ (E,F,D)={A,B,C,D,E,F} which is nothing but R.
Step 2 : Consider R1∩ R2={CD} and R2∩R3={D}. Hence second condition of
intersection not being gets satisfied.
Step 3 : Now, consider R1(A,C,D) and R2(B,C,D). We find R1∩R2={CD}
(CD)+ = {ABCDE} attributes of R1 i.e.{A,C,D}. Hence condition 3 for checking
lossless join for R1 and R2 gets satisfied.
Step 4 : Now, consider R2(B,C,D) and R3(E,F,D) . We find R2∩R3={D}.
(D)+={D,E} which is neither complete set of attributes of R2 or R3.[Note that F is
missing for being attribute of R3].
Hence it is not lossless join decomposition. Or in other words we can say it is a
lossy decomposition.
Example 2.10.3 Suppose that we decompose schema R=(A,B,C,D,E) into (A,B,C) (C,D,E)
Show that it is not a lossless decomposition.
Solution :
Step 1 : Here we need to assume some data for the attributes A, B, C, D, and E.
Using this data we can represent the relation as follows –
Relation R
A B C D E
a 1 x p q
b 2 x r s
Relation R1 = (A,B,C)
A B C
a 1 x
b 2 x
Relation R2 = (C,D,E)
C D E
x p q
x r s
Database Management Systems 2 - 39 Database Design
Step 2 : Now we will join these tables using natural join, i.e. the join based on
common attribute C. We get R1 ⋈ R2 as
A B C D E
a 1 x p q
Here we get more rows or
a 1 x r s tuples than original
b 2 x p q relation R
b 2 x r s
Clearly R1 ⋈ R2 R. Hence it is not lossless decomposition.
Example 2.10.4 Consider the relation R (A, B, C) for functional dependency set {A -> B and
B -> C} which is decomposed into two relations R1 = (A, C) and R2 = (B, C). Then check if
this decomposition dependency preserving or not.
Solution : This can be solved in following steps :
Step 1 : For checking whether the decomposition is dependency preserving or not
we need to check
following condition
F+ = (F1 F2)+
Step 2 : We have with us the F+ ={ A->B and B->C }
+ +
Step 3 : Let us find (F1) for relation R1 and (F2) for relation R2
R1(A,C) R2(B,C)
A->A Trivial B->B Trivial
C->C Trivial C->C Trivial
A->C In (F)+A->B->C and it is Nontrivial B->C In (F)+ B->C and it is Non-Trivial
AC->AC Trivial BC->BC Trivial
A->B but is not useful as B is not part of R1 We can not obtain C->B
set
We can not obtain C->A
Database Management Systems 2 - 40 Database Design
Step 4 : We will eliminate all the trivial relations and useless relations. Hence we
can obtain R1 and R2 as
R1(A,C) R2(B,C)
A->C Nontrivial
B->C Non-Trivial
Example 2.10.5 Let relation R(A,B,C,D) be a relational schema with following functional
dependencies {A->B, B->C,C->D, and D->B}. The decomposition of R into (A,B), (B,C)
and (B,D). Check whether this decomposition is dependency preserving or not.
Solution :
Step 1 : Let (F)+ = {A->B, B->C, C->D,D->B}.
Step 2 : We will find (F1)+, (F2)+, (F3)+ for relations R1(A,B) , R2(B,C) and R3(B,D) as
follows -
Step 3 : We will eliminate all the trivial relations and useless relations. Hence we
can obtain R1 ∪ R2 ∪ R3 as
University Question
1. Differentiate between lossless join decomposition and dependency preserving decomposition.
AU : Dec.-17, Marks 7
As there are multiple values of phone number for sid 1 and 3, the above table is not in
1NF. We can make it in 1NF. The conversion is as follows -
For example : Consider following table in which every information about a the
Student is maintained in a table such as student id(sid), student name(sname), course
id(cid) and course name(cname).
Student_Course
sid sname cid cname
1 AAA 101 C
2 BBB 102 C++
3 CCC 101 C
4 DDD 103 Java
This table is not in 2NF. For converting above table to 2NF we must follow the
following steps -
Step 1 : The above table is in 1NF.
Step 2 : Here sname and sid are associated similarly cid and cname are associated
with each other. Now if we delete a record with sid=2, then automatically the
course C++ will also get deleted. Thus,
sid->sname or cid->cname is a partial functional dependency, because {sid,cid}
should be essentially a candidate key for above table. Hence to bring the above table
to 2NF we must decompose it as follows :
Student
Here candidate key is
sid sname cid (sid,cid)
and
1 AAA 101
(sid,cid)->sname
2 BBB 102
3 CCC 101
4 DDD 103
Course
cid cname
Here candidate key is
101 C cid
101 C
103 Java
101 1 AAA
102 2 BBB
103 3 CCC
104 4 DDD
Superkeys
{RegID}
{RegID, RollNo}
{RegID,Sname}
{RollNo,Sname}
{RegID, RollNo,Sname}
Candidate Keys
{RegID}
{RollNo}
Or in other words
In other words 3NF can be defined as : 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 :
i) X is a super key of table
ii) Y is a prime attribute of table
For example : Consider following table Student_details as follows -
Zip
zipcode cityname state
11111 Pune Maharashtra
22222 Surat Gujarat
33333 Chennai Tamilnadu
44444 Jaipur Rajasthan
55555 Mumbai Maharashtra
Similarly AB AF ∵ B F (given)
Thus now using union rule
AB ABCDEFGHIJ
AB is a key
The table can be converted to 2NF as
Database Management Systems 2 - 47 Database Design
R1 = (A, B, C)
R2 = (A, D, E, I, J)
R3 = (B, F, G, H)
R2 = (A, D, E)
R3 = (D, I, J)
R4 = (B, E)
R5 = (E, G, H).
University Questions
1. What is database normalization ? Explain the first normal form, second normal form and third
normal form. AU : May-18, Marks 13; Dec.-15, Marks 16
2. What are normal forms. Explain the types of normal form with an example.
AU : Dec.-14, Marks 16
Enrollment Table
sid course Teacher
1 C Ankita
1 Java Poonam
Database Management Systems 2 - 48 Database Design
2 C Ankita
3 C++ Supriya
4 C Archana
From above table following observations can be made :
One student can enroll for multiple courses. For example student with sid=1 can
enroll for C as well as Java.
For each course, a teacher is assigned to the student.
There can be multiple teachers teaching one course for example course C can be
taught by both the teachers namely - Ankita and Archana.
The candidate key for above table can be (sid,course), because using these two
columns we can find
The above table holds following dependencies
o (sid,course)->Teacher
o Teacher->course
The above table is not in BCNF because of the dependency teacher->course. Note
that the teacher is not a superkey or in other words, teacher is a non prime
attribute and course is a prime attribute and non-prime attribute derives the prime
attribute.
To convert the above table to BCNF we must decompose above table into Student
and Course tables
Student
sid Teacher
1 Ankita
1 Poonam
2 Ankita
3 Supriya
4 Archana
Course
Teacher course
Ankita C
Poonam Java
Ankita C
Supriya C++
Archana C
Now the table is in BCNF
Database Management Systems 2 - 49 Database Design
(AC) + = {AC} R
There is no involvement of D on LHS of the FD rules. Hence D can not be part of any
candidate key. Thus we obtain two candidate keys (AB)+ and (BC)+. Hence
prime attributes = {A,B,C}
Non prime attributes = {D}
Step 2 : Now, we will start checking from reverse manner, that means from BCNF,
then 3NF, then 2NF.
Step 3 : For R being in BCNF for X->Y the X should be candidate key or super key.
From above FDs consider C->D in which C is not a candidate key or super key.
Hence given relation is not in BCNF.
Step 4 : For R being in 3NF for X->Y either i) the X should be candidate key or super
key or ii) Y should be prime attribute. (For prime and non prime attributes refer
step 1)
o Consider C->A. In this FD the C is not candidate key. Condition for 2NF is
satisfied.
o Now consider B->D. In this FD, the B is a part of candidate key(AB or BC),
similarly D is not a prime attribute. That means partial functional
dependency occurs here. Hence condition for 2NF fails over here.
Hence given relation is not in 2NF.
Therefore we can conclude that the given relation R is in 1NF.
Example 2.12.2 Consider a relation R(ABC) with following FD A->B, B->C and C->A.
What is the normal form of R ?
Solution :
Step 1 : We will find the candidate key
(A)+ = {ABC} =R
(B)+ = {ABC} =R
(C)+ = {ABC} =R
Hence A, B and C all are candidate keys
Prime attributes = {A,B,C}
Non prime attribute{}
Step 2 : For R being in BCNF for X->Y the X should be candidate key or super key.
From above FDs
Student
sid Course Skill
1 C English
C++ German
2 Java English
French
Here sid =1 leads to multiple values for courses and skill. Following table shows this
Database Management Systems 2 - 52 Database Design
1 C English
1 C++ German
1 C German
1 C++ English
2 Java English
2 Java French
Here sid and course are dependent but the Course and Skill are independent. The
multivalued dependency is denoted as :
sid Course
sid Skill
Student Table
sid Course Skill
1 C English
1 C++ German
1 C German
1 C++ English
2 Java English
2 Java French
Now to convert the above table to 4NF we must decompose the table into following
two tables.
Database Management Systems 2 - 53 Database Design
Student_Course Table
Key : (sid,Course)
sid Course
1 C
1 C++
2 Java
Student_Skill Table
Key : (sid,Skill)
sid Skill
1 English
1 German
2 English
2 French
Thus the tables are now in 4NF.
University Questions
1. Explain first normal form, second normal form, third normal form and BCNF with example.
AU : Dec.-16, Marks 13
2. Explain Boyce Codd Normal form and fourth normal form with suitable example.
AU : May-14, Marks 16
The above table is in 4th Normal Form as there is no multivalued dependency. But it
is not in 5th normal form because if we join the above two table we may get
To avoid the above problem we can decompose the tables into three tables as
Seller_Company, Seller_Product, and Company Product table
Seller_Company Seller_Product Company_Product
Seller Company Seller Product Company Product
Rupali Godrej Rupali Cinthol Godrej Cinthol
Sharda Dabur Sharda Honey Dabur Honey
Sunil Amul Sharda HairOil Dabur HairOil
Sunil Britania Sharda RoseWater Dabur RoseWater
Sunil Icecream Amul Icecream
Sunil Biscuit Britania Biscuit
Q.2 Give the limitations of E-R model ? How do you overcome this ? AU : May-07
Ans. : 1) Loss of information content : Some information be lost or hidden in ER
model
2) Limited relationship representation : ER model represents limited relationship as
compared to another data models like relational model etc.
3) No representation of data manipulation : It is difficult to show data manipulation
in ER model.
4) Popular for high level design : ER model is very popular for designing high level
design.
Q.9 Why certain functional dependencies are called trivial functional dependencies ?
AU : May-06,12
Ans. : A functional dependency FD : X → Y is called trivial if Y is a subset of X.
This kind of dependency is called trivial because it can be derived from common
sense. If one "side" is a subset of the other, it's considered trivial. The left side is
considered the determinant and the right the dependent.
For example - {A,B} –> B is a trivial functional dependency because B is a subset of
A,B. Since {A,B} –> B includes B, the value of B can be determined. It's a trivial
functional dependency because determining B is satisfied by its relationship to
A,B
Q.13 Describe BCNF and describe a relation which is in BCNF. AU : Dec. -02
Ans. : Refer section 2.12.
Q.14 Why 4NF in normal form is more desirable than BCNF ? AU : Dec. -14
Ans. :
4NF is more desirable than BCNF because it reduces the repetition of information.
If we consider a BCNF schema not in 4NF we observe that decomposition into
4NF does not lose information provided that a lossless join decomposition is used,
yet redundancy is reduced.
Q.15 Give an example of a relation schema R and set of dependencies such that R is in
BCNF but not in 4NF. AU : May -12
Ans. : Consider relation R(A,B,C,D) with dependencies
AB C
ABC D
AC B
Here the only key is AB. Thus each functional dependency has superkey on the left.
But MVD has non-superky on its left. So it is not 4NF.
Ans. : Decomposition is the process of breaking down one table into multiple
tables.
The decomposition is used for eliminating redundancy.