DBMS Question Bank
DBMS Question Bank
COMPUTER ENGINEERING
DEPARTMENT
QUESTION BANK
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Questions
Unit 1
1. Define following terms. [2 marks per definition] data, databse, information, DBMS,
metadata, records, data dictionary, field.
Unit 2
Unit 3
2. Explain order by, group by and having clause with example. (4m)
Unit 4
Unit 5
CE, PIET-DS 2
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Metadata: metadata is data about data. Example, data such as table name,
column name, data type, authorized users and user access privileges for any
table.
Internal level (physical level) conceptual level (logical level) external level (view
level)
CE, PIET-DS 3
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
It uses the physical data model. It is used to define that how the data will be
stored in a block.
It level describes what data are to be stored in the database and describes
what Relationship exists among those data.
It describes the database part that a particular user group is interested and
hides the remaining database from that user group.
Same data can be accessed by different users with different customizes views.
The user is not conserved about the physical data storage details.
Guarantee atomicity
Application of DBMS:
CE, PIET-DS 5
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Schema definition
Monitoring performance
CE, PIET-DS 6
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
The hardware in a database environment means the computers and computer peripherals
that are being used to manage a database, and the software means the whole thing right
from the operating system (OS) to the application programs management software like
M.S. Access or SQL-Server.
The people in a database environment include those people who administrate and use the
System.
phases.
Analyze and break down the data to be understood by the non-tech person
Data Information
CE, PIET-DS 8
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Data redundancy:
Data inconsistency:
Limited sharing:
Data stored in different formats, different folder & different computers, due
to this data isolation. It is difficult to share data among different application.
CE, PIET-DS 9
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
SQL is widely used because it provides a standard way to interact with a database,
regardless of the specific database management system being used. SQL is also highly
efficient at managing large amounts of data, making it a popular choice for organizations
that need to store and analyze large datasets.
1. Data retrieval: SQL is used to retrieve specific data from a database, such as
customer information or product inventory.
2. Data manipulation: SQL allows users to add, delete, or modify data within a
database.
3. Database management: SQL is used to manage the structure and relationships
between tables within a database.
4. Data analysis: SQL can be used to perform complex queries and aggregate data
from multiple tables, making it a powerful tool for data analysis and reporting.
CE, PIET-DS 10
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
1. CREATE: This command is used to create a new database object, such as a table,
view, or index. The syntax for the CREATE command varies depending on the
type of object being created.
Syntax:
Example:
CE, PIET-DS 11
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
3. DROP: This command is used to delete an existing database object. The DROP
command can be used to delete a table, view, index, or other object from the
database.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE employees;
4. TRUNCATE: This command is used to delete all data from a table without deleting
the table structure itself. TRUNCATE is faster than using the DELETE command
because it does not log each row deletion.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE employees;
Example:
CE, PIET-DS 12
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Example:
CE, PIET-DS 13
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
name VARCHAR(50),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
Output:
DML (Data Manipulation Language) commands are used to manipulate data within a
database. Here are some common DML commands along with their syntax and examples:
Example:
INSERT INTO employees (id, name, age, department, salary) VALUES (1,
'John Doe', 30, 'Sales', 50000);
INSERT INTO employees (id, name, age, department, salary) VALUES (2,
'arti', 28, 'Sales', 51000);
CE, PIET-DS 14
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
INSERT INTO employees (id, name, age, department, salary) VALUES (3,
'sidddhi', 20, 'Sales', 52000);
Output:
Syntax:
Example:
Output:
Syntax:
Example:
Output:
Syntax:
Example:
Output:
5. MERGE: This command is used to insert, update, or delete data in a table based
on a specified condition.
Syntax:
Example:
WHEN NOT MATCHED THEN INSERT (id, name, age, department, salary)
VALUES (n.id, n.name, n.age, n.department, n.salary)
DELETE;
Example:
Syntax:
Example:
CE, PIET-DS 17
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Syntax:
COMMIT;
Example:
Syntax:
ROLLBACK;
Example:
Syntax:
SAVEPOINT savepoint_name;
Example:
Syntax:
CE, PIET-DS 18
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Example:
Here, column1, column2, ... are the names of the columns that you want to retrieve data
from, and table_name is the name of the table from which you want to retrieve data. The
WHERE clause specifies the condition that must be met in order for a row to be included
in the result set.
For example, suppose you have a table called employees with columns id, name,
department, and salary. You can use the following SELECT statement to retrieve the
names and salaries of all employees who work in the "Sales" department and earn more
than $50,000:
Example:
SELECT name, salary FROM employees WHERE department = 'Sales' AND salary >
50000;
Output:
In this example, the WHERE clause specifies that only rows where the department is
CE, PIET-DS 19
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
"Sales" and the salary is greater than 50,000 should be included in the result set. The
SELECT statement retrieves the name and salary columns from these rows.
We can use various arithmetic operators on the data stored in the tables.
Arithmetic Operators are:
Operator
division operations.
Comparison operators:
1 = Equal to.
CE, PIET-DS 20
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Logical operators:
The Logical operators are those that are true or false. They return true or false values to
combine one or more true or false values.
1. AND: Logical AND compares between two Booleans as expressions and returns
true when both expressions are true.
2. OR: Logical OR compares between two Booleans as expressions and returns true
when one of the expressions is true.
3. NOT: Not takes a single Boolean as an argument and changes its value from false
to true or from true to false.
The SQL BETWEEN operator tests an expression against a range. The range consists of
a beginning, followed by an AND keyword and an end expression.
The operator returns TRUE when the search value present within the range otherwise
returns FALSE.
The results are NULL if any of the range values are NULL.
Syntax:
Example:
name VARCHAR(50),
age INT,
department VARCHAR(50),
CE, PIET-DS 21
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
salary DECIMAL(10,2)
);
INSERT INTO employees (id, name, age, department, salary) VALUES (1, 'John Doe',
30, 'Sales', 50000);
INSERT INTO employees (id, name, age, department, salary) VALUES (2, 'arti', 28,
'Sales', 51000);
INSERT INTO employees (id, name, age, department, salary) VALUES (3, 'sidddhi',
20, 'Sales', 52000);
Output:
The LIKE command is used in a WHERE clause to search for a specified pattern in a
column.
_ - Represents a single character (MS Access uses a question mark (?) instead)
The following SQL selects all customers with a CustomerName starting with "a":
Syntax:
Example:
CE, PIET-DS 22
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
CE, PIET-DS 23
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Output:
The following SQL selects all customers that are located in "Germany", "France" and
"UK":
Syntax:
Example:
name VARCHAR(50),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees (id, name, age, department, salary) VALUES (1, 'John Doe',
30, 'A', 50000);
INSERT INTO employees (id, name, age, department, salary) VALUES (2, 'arti', 28,
'B', 51000);
INSERT INTO employees (id, name, age, department, salary) VALUES (3, 'sidddhi',
20, 'C', 52000);
CE, PIET-DS 24
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Output:
The GROUP BY command is used to group the result set (used with aggregate functions:
COUNT, MAX, MIN, SUM, AVG).
The following SQL lists the number of customers in each country: Example
GROUP BY Country;
Example:
name VARCHAR(50),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees (id, name, age, department, salary) VALUES (1, 'John Doe',
28, 'A', 50000);
CE, PIET-DS 25
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
INSERT INTO employees (id, name, age, department, salary) VALUES (2, 'arti', 28, 'B',
51000);
INSERT INTO employees (id, name, age, department, salary) VALUES (3, 'sidddhi', 20,
'C', 52000);
FROM employees
GROUP BY age;
Output:
HAVING:
The following SQL lists the number of customers in each country. Only include countries
with more than 5 customers:
Example
name VARCHAR(50),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees (id, name, age, department, salary) VALUES (1, 'John Doe',
CE, PIET-DS 26
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
INSERT INTO employees (id, name, age, department, salary) VALUES (2, 'arti', 28, 'B',
51000);
INSERT INTO employees (id, name, age, department, salary) VALUES (3, 'sidddhi', 20,
'C', 52000);
GROUP BY age
Output:
ORDER BY
The ORDER BY command is used to sort the result set in ascending or descending order.
The ORDER BY command sorts the result set in ascending order by default. To sort the
records in descending order, use the DESC keyword. The following SQL statement
selects all the columns from the "Customers" table, sorted by the "CustomerName"
column:
Example:
1)ASC
The ASC command is used to sort the data returned in ascending order. The following
SQL statement selects all the columns from the "Customers" table, sorted by the
"CustomerName" column:
Example:
name VARCHAR(50),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees (id, name, age, department, salary) VALUES (1, 'John Doe',
28, 'A', 50000);
INSERT INTO employees (id, name, age, department, salary) VALUES (2, 'arti', 28, 'B',
51000);
INSERT INTO employees (id, name, age, department, salary) VALUES (3, 'sidddhi', 20,
'C', 52000);
Output:
2)DESC
The DESC command is used to sort the data returned in descending order. The following
SQL statement selects all the columns from the "Customers" table, sorted descending by
the "CustomerName" column:
Example:
name VARCHAR(50),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees (id, name, age, department, salary) VALUES (1, 'John Doe',
28, 'A', 50000);
INSERT INTO employees (id, name, age, department, salary) VALUES (2, 'arti', 28, 'B',
51000);
INSERT INTO employees (id, name, age, department, salary) VALUES (3, 'sidddhi', 20,
'C', 52000);
Output:
LOWER will not change any characters in the string that are not letters, since case is
irrelevant for numbers and special characters, such as the dollar sign ( $ ) or modulus ( %
).
Syntax:
LOWER (string)
CE, PIET-DS 29
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Output1: hello
UPPER: This function converts alpha character values to uppercase. Also, UPPER
function too, will actually return a fixed-length string if the incoming string is fixed-
length. UPPER will not change any characters in the string that are not letters, since
case is irrelevant for numbers and special characters, such as the dollar sign ( $ ) or
modulus ( % ).
Syntax:
UPPER (string)
Output1: HELLO
INITCAP: This function converts alpha character values to uppercase for the first letter
of each word and all others in lowercase.
Syntax:
INITCAP (string)
Input1: SELECT INITCAP ('web port is a computer science portal') FROM DUAL;
CONCAT: This function always appends (concatenates ) string2 to the end of string1.
If either of the string is NULL, CONCAT function returns the non-NULL argument.
If both strings are NULL, CONCAT returns NULL.
Syntax:
CE, PIET-DS 30
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
If the input string is NULL, then LENGTH function returns NULL and not Zero. Also,
if the input string contains extra spaces at the start, or in between or at the end of the
string, then the LENGTH function includes the extra spaces too and returns the
complete length of the string.
Syntax:
LENGTH (Column|Expression)
LPAD and RPAD: These functions return the strings padded to the left or right (as per
the use);
hence the “L” in “LPAD” and the “R” in “RPAD”; to a specified length,
If the pad string is not specified, then the given string is padded on the left or right (as
per the use) with spaces.
Syntax:
TRIM: This function trims the string input from the start or end (or both).
If no string or char is specified to be trimmed from the string and there exists some
extra space at start or end of the string, then those extra spaces are trimmed off.
Syntax:
CE, PIET-DS 31
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Output1: EEKS
ABS (): It returns the absolute value of a number. Syntax: SELECT ABS (-
243.5);
Output: 243.5
ACOS (): It returns the cosine of a number. Syntax: SELECT ACOS (0.25);
Output: 1.318116071652818
ASIN (): It returns the arc sine of a number. Syntax: SELECT ASIN (0.25);
Output: 0.25268025514207865
ATAN (): It returns the arc tangent of a number. Syntax: SELECT ATAN (2.5);
Output: 1.1902899496825317
CEIL (): It returns the smallest integer value that is greater than or equal to a
number.
Output: 26
Output: 2
EXP (): It returns be raised to the power of number. Syntax: SELECT EXP (1);
Output: 2.718281828459045
FLOOR (): It returns the largest integer value that is less than or equal to a
number.
Output: 2
CE, PIET-DS 32
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Output: 125
LEAST (): It returns the smallest value in a list of expressions. Syntax: SELECT
LEAST (30, 2, 36, 81, 125);
Output: 2
Output: 0.6931471805599453
MOD (): It returns the remainder of n divided by m. Syntax: SELECT MOD (18,
4);
Output: 2
PI(): It returns the value of PI displayed with 6 decimal places. Syntax: SELECT
PI();
Output: 3.141593
POW(): It returns m raised to the nth power. Syntax: SELECT POW(4, 2);
Output: 16
Output: 6
Output: 0.9092974268256817
Output: 5
Output: -5.52037992250933
CE, PIET-DS 33
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Output: 7.53
TO_CHAR function is used to typecast a numeric or date input to character type with a
format model (optional).
SYNTAX :
TO_CHAR(date, ’format_model’)
example:
FROM employees
OUTPUT :
TO_DATE:(char[, ’format_model’])
These functions have an fx modifier. This modifier specifies the exact matching for the
character argument and date format model of a TO_DATE function.
EXAMPLE :
CE, PIET-DS 34
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
OUTPUT :
Count(salary): Return number of Non Null values over the column salary.
i.e 5.
Count(Distinct Salary): Return number of distinct Non Null values over the column
salary .i.e 4
Example:
Output: 5
Sum():
sum(salary): Sum all Non Null values of Column salary i.e., 310 sum(Distinct salary):
Sum of all distinct Non-Null values i.e., 250. Example:
Output: 456000
Avg():
Example:
Output: 50.56
Min():
CE, PIET-DS 35
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Min(salary): Minimum value in the salary column except NULL i.e., 40. Max(salary):
Maximum value in the salary i.e., 80.
Example:
Output: 50000
The INNER JOIN keyword selects all rows from both the tables as long as the condition
is satisfied.
This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be the same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
ON table1.matching_column = table2.matching_column;
CE, PIET-DS 36
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
LEFT JOIN
This join returns all the rows of the table on the left side of the join and matches rows
for the table on the right side of the join.
For the rows for which there is no matching row on the right side, the result-set will
contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
ON table1.matching_column = table2.matching_column;
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
CE, PIET-DS 37
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
RIGHT JOIN
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the
right side of the join and matching rows for the table on the left side of the join.
For the rows for which there is no matching row on the left side, the result- set will
contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN. Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
ON table1.matching_column = table2.matching_column;
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
CE, PIET-DS 38
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
FULL JOIN
FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT
JOIN.
The result-set will contain all the rows from both tables. For the rows for which there is
no matching, the result-set will contain NULL values.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
ON table1.matching_column = table2.matching_column;
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
CE, PIET-DS 39
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
If we specify a field in a table to be NOT NULL. Then the field will never accept null
value.
That is, you will be not allowed to insert a new row in the table without specifying any
value to this field.
For example, the below query creates a table Student with the fields ID and NAME as
NOT NULL.
That is, we are bound to specify values for these two fields every time we wish to insert
a new row.
EXAMPLE:
ADDRESS varchar(20)
UNIQUE
This constraint helps to uniquely identify each row in the table. i.e. for a particular
column, all the rows should have unique values.
For example, the below query creates a table Student where the field ID is specified as
UNIQUE. i.e, no two students can have the same ID. Unique constraint in detail.
Example:
);
PRIMARY KEY
Primary Key is a field which uniquely identifies each row in the table.
CE, PIET-DS 40
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
If a field in a table as primary key, then the field will not be able to contain NULL values
as well as all the rows should have unique values for this field.
So, in other words we can say that this is combination of NOT NULL and UNIQUE
constraints.
A table can have only one field as primary key. Below query will create a table named
Student and specifies the field ID as primary key.
Example:
NAME varchar(10),
FOREIGN KEY
Foreign Key is a field in a table which uniquely identifies each row of a another table.
That is, this field points to primary key of another table. This usually creates a kind of
link between the tables.
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
As we can see clearly that the field C_ID in Orders table is the primary key in
Customers table, i.e. it uniquely identifies each row in the Customers table. Therefore, it
is a Foreign Key in Orders table.
Query:
CREATE TABLE Orders (O_ID int NOT NULL, ORDER_NO int NOT NULL,
C_ID int,
CE, PIET-DS 41
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
CHECK
Using the CHECK constraint we can specify a condition for a field, which should be
satisfied at the time of entering values for this field.
For example, the below query creates a table Student and specifies the condition for the
field AGE as (AGE >= 18 ).
That is, the user will not be allowed to enter any record in the table with AGE <18.
Check constraint in detail
Example:
);
You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING
clause, FROM clause.
A subquery is a query within another query. The outer query is called as main query
CE, PIET-DS 42
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Syntax:
However, Subqueries are seen to be used most frequently with SELECT statement as
shown below:
SELECT column_name
FROM table_name
Example:
Output:
CE, PIET-DS 43
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Example:
Student has attributes like name, age, roll number, and many more. To uniquely
identify the student, we use the primary key as a roll number as it is not repeated.
Attributes can also be subdivided into another set of attributes.
There are five such types of attributes: Simple, Composite, Single-valued, Multi-
valued, and Derived attribute. One more attribute is their, i.e. Complex Attribute,
this is the rarely used attribute.
CE, PIET-DS 44
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
1)Simple attribute:
Example:
2)Composite attribute:
Example:
The address can be further split into house number, street number, city, state,
country, and pin code, the name can also be split into first name middle name, and
last name.
3)Single-valued attribute:
The attribute which takes up only a single value for each entity instance is a single-
valued attribute.
Example:
4)multi-valued attribute:
The attribute which takes up more than a single value for each entity instance is a
multi-valued attribute.
Example:
5)Derived attribute:
Example:
CE, PIET-DS 45
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
6)Complex attribute:
Those attributes, which can be formed by the nesting of composite and multi-
valued attributes, are called “Complex Attributes”. These attributes are rarely used
in DBMS(DataBase Management System). That’s why they are not so popular.
Example:
Let us consider a person having multiple phone numbers, emails, and an address.
Here, phone number and email are examples of multi-valued attributes and address
is an example of the composite attribute, because it can be divided into house
number, street, city, and state.
Example:
Entity Set:
CE, PIET-DS 46
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
An entity set is a collection or set of all entities of a particular entity type at any
point in time. The type of all the entities should be the same.
Example:
The collection of all the students from the student table at a particular instant of
time is an example of an entity set.
The collection of all the employees from the employee table at a particular instant
of time is an example of an entity set.
For example:
We have two entities, one is a student and the other is a bag and they are connected
with the primary key and foreign key. So, here we can see that the degree of
relationship is 2 as 2 entities are associating in a relationship.
Types of degree
Now, based on the number of linked entity types, we have 4 types of degrees of
relationships.
1) Unary
2) Binary
CE, PIET-DS 47
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
3) Ternary
4) N-ARY
1)Unary
In this type of relationship, both the associating entity type are the same.
So, we can say that unary relationships exist when both entity types are the same
and we call them the degree of relationship is 1. Or in other words, in a relation
only one entity set is participating then such type of relationship is known as a
unary relationship.
Example:
In a particular class, we have many students, there are monitors too. So, here class
monitors are also students. Thus, we can say that only students are participating
here.
2)Binary (degree 2)
In a Binary relationship, there are two types of entity associates. So, we can say
that a Binary relationship exists when there are two types of entity and we call
them a degree of relationship is 2.
Or in other words, in a relation when two entity sets are participating then such
type of relationship is known as a binary relationship.
This is the most used relationship and one can easily be converted into a relational
table.
CE, PIET-DS 48
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Example:
We have two entity types ‘Student’ and ‘ID’ where each ‘Student’ has his ‘ID’.
So, here two entity types are associating we can say it is a binary relationship.
Also, one ‘Student’ can have many ‘daughters’ but each ‘daughter’ should belong
to only one ‘father. We can say that it is a one-to-many binary relationship.
3)Ternary(degree 3)
In the Ternary relationship, there are three types of entity associates. So, we can
say that a Ternary relationship exists when there are three types of entity and we
call them a degree of relationship is 3.
Since the number of entities increases due to this, it becomes very complex to turn
E-R into a relational table. Now let’s understand with the examples.
Example:
We have three entity types ‘Teacher’, ‘Course’, and ‘Class’. The relationship
between these entities is defined as the teacher teaching a particular course, also
the teacher teaches a particular class.
So, here three entity types are associating we can say it is a ternary relationship.
4)N-ARY (n degree)
CE, PIET-DS 49
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
In the N-ary relationship, there are n types of entity that associates. So, we can say
that an N-ary relationship exists when there are n types of entities.
There is one limitation of the N-ary relationship, as there are many entities so it is
very hard to convert into an entity, rational table.
So, this is very uncommon, unlike binary which is very much popular.
Example:
We have 5 entities Teacher, Class, Location, Salary, Course. So, here five entity
types are associating we can say an n-ary relationship is 5.
CE, PIET-DS 50
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Example:
Each customer can purchase as many kinds of flyrods as they want (a constraint on
Purchased), but can have only one favorite flyrod/fly combination (a constraint on Fave-
Combo).
A binary relationship set is a relationship set on two entity sets. Mapping cardinalities on
binary relationship sets are simplest.
Consider a binary relationship set R on entity sets A and B. There are four possible
mapping cardinalities in this case:
1) One-to-one:
2) One-to-many:
3) Many-to-one:
4) Many-to-many:
CE, PIET-DS 52
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Attribute
1.Key attribute
2. Composite attribute
3. Multivalued attribute
4. Derived attribute
1. Key attribute:
For example, student roll number can uniquely identify a student from a set of
students. Key attribute is represented by oval same as other attributes however,
the text of key attribute is underlined.
2. Composite attribute:
CE, PIET-DS 53
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
3. Multivalued attribute:
For example – A person can have more than one phone numbers so the phone
number attribute is multivalued.
4. Derived attribute:
A derived attribute is one whose value is dynamic and derived from another
attribute.
CE, PIET-DS 54
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
CE, PIET-DS 55
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
CE, PIET-DS 56
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
It is getting harder and harder to apply the conventional ER paradigm for database
modeling as data complexity rises today.
These are two normal kinds of relationships that were added to the normal ER
model for enhancement.
These are inspired by the object-oriented paradigm, where we divide the code into
classes and objects, and in the same way, we have divided entities into subclass
and superclasses.
Specialized classes are called subclasses, and generalized classes are called
superclasses or base classes.
We can learn the concept of subclass by 'IS-A' analysis. For example, 'Laptop IS-
A computer.' Or 'Clerk IS-A employee.'
In this relationship, one entity is a subclass or superclass of another entity. For
example, in a university, a faculty member or clerk is a specialized class of
employees. So an employee is a generalized class, and all others are its subclass.
We can draw the ER diagram for these relationships. Let's suppose we have a
superclass Employee and subclasses as a clerk, engineer, and lab assistant.
CE, PIET-DS 57
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
The Enhanced ER diagram of the above example will look like this:
In the above example, we have one superclass and three subclasses. Each subclass
inherits all the attributes from its superclass so that a lab assistant will have all its
attributes, like its name, salary, etc.
CE, PIET-DS 58
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Normalization divides the larger table into smaller and links them using
relationships.
The normal form is used to reduce redundancy from the database table.
There are several levels of normalization, referred to as normal forms. The most
commonly used normal forms are first normal form (1NF), second normal form
(2NF), and third normal form (3NF).
1NF requires that each column in a table contain atomic values, meaning that the
data in each column cannot be further broken down into smaller pieces.
3NF goes further by requiring that every non-key column be dependent only on the
primary key, and not on any other non-key column. This helps to eliminate
transitive dependencies, where the value of one non-key column is dependent on
another non-key column.
CE, PIET-DS 59
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
1. X Y
The left side of FD is known as a determinant, the right side of the production is
known as a dependent.
CE, PIET-DS 60
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
For Example:
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee
table because if we know the Emp_Id, we can tell that employee name associated
with it.
1. Emp_Id → Emp_Name
Example:
CE, PIET-DS 61
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Example:
1. ID → Name,
2. Name → DOB
Using the inference rule, we can derive additional functional dependency from the
initial set.
If X ⊇ Y then X → Y
Example:
1. X = {a, b, c, d, e}
2. Y = {a, b, c}
1. If X → Y then XZ → YZ
Example:
In the transitive rule, if X determines Y and Y determine Z, then X must also determine
Z.
1. If X → Y and Y → Z then X → Z
Union rule says, if X determines Y and X determines Z, then X must also determine
Y and Z.
1. If X → Y and X → Z then X → YZ
Proof:
1. X → Y (given)
2. X → Z (given)
Decomposition rule is also known as project rule. It is the reverse of union rule.
determines Z separately.
1. If X → YZ then X → Y and X → Z
Proof:
CE, PIET-DS 63
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
1. X → YZ (given)
Normalization divides the larger table into smaller and links them using
relationships.
The normal form is used to reduce redundancy from the database table.
First normal form disallows the multi-valued attribute, composite attribute, and
their combinations.
Employee Table:
CE, PIET-DS 64
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
14 John 7272826385,
9064738238
20 Harry 8574783832
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385
14 John 9064738238
20 Harry 8574783832
In the second normal form, all non-key attributes are fully functional dependent on
the primary key
Example: Let us assume, a school can store the data of teachers and the subjects
they teach. In a school, a teacher can teach more than one subject.
Teacher Table:
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
CE, PIET-DS 65
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
The first condition for the table to be in Third Normal Form is that the table should
be in the Second Normal Form.
The second condition is that there should be no transitive dependency for non-
prime attributes, which indicates that non-prime attributes (which are not a part of
the candidate key) should not depend on other non-prime attributes in a table.
Below is a student table that has student id, student name, subject id, subject name,
and address of the student as its columns.
In the above student table, stu_id determines subid, and subid determines sub.
Therefore, stu_id determines sub via subid. This implies that the table possesses a
transitive functional dependency, and it does not fulfill the third normal form
criteria.
CE, PIET-DS 66
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
Now to change the table to the third normal form, you need to divide the table as
shown below:
In the first table, columns name, subid, and addresses only depend on stu_id.
A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-
valued dependency.
Example:
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two
independent entity. Hence, there is no relationship between COURSE and
HOBBY.
CE, PIET-DS 67
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM(DBMS) (03606207)
STUDENT table:
So to make above table into 4NF, we can decompose into two tables:
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
STUDENT_COURSE table:
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY table:
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
CE, PIET-DS 68