DBMS NJNN
DBMS NJNN
Ans
➢ Application of DBMS
1. E-Commerce
2. Online television streaming
3. Social media
4. Banking & insurance
5. Airline& Railway
6. Universities and colleges /schools
7. Human Resource Department
8. Payroll Management
9. Hospitals and Medical Stores
10. Government organizations
ans
The 3-tier architecture is a commonly used architectural approach in
Database Management Systems (DBMSs) for the design and development
of applications that work with databases. The 3-tier architecture divides an
application’s components into three tiers or layers. Each layer has its own
set of responsibilities.
DBMS 3-Tier architecture divides the complete system into three inter-
related but independent modules as shown below
DBMS
Q-4 Define Data Abstraction and explain different levels of data abstraction.
Ans
➢ Physical data
• Physical data independece is the ability to modify the physical schema
without requiring any change in logical schema and application program.
• Modification at the internal levels are occasionally necessary to improve
performance
DBMS
Ans
DBMS
Example :
1. NOT NULL Constraint CREATE TABLE Students ( Student
ID INT, Name VARCHAR(100) NOT
Ensures that a column cannot have a NULL );
NULL value.
Example:-
2. UNIQUE Constraint CREATE TABLE Employees (
EmployeeID INT UNIQUE,
Ensures that all the values in a column Name VARCHAR(100)
are unique across the database, );
meaning no two rows can have the
same value in that column.
Example
3. PRIMARY KEY Constraint CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
Combines the NOT NULL and UNIQUE OrderDate DATE,
constraints. It ensures that the column CustomerID INT
(or combination of columns) that is the );
primary key has unique, non-null
values. A table can have only one
primary key.
Example
4. FOREIGN KEY Constraint CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
Ensures that the value in one column OrderDate DATE,
matches the value in another column CustomerID INT,
(usually in a different table). It FOREIGN KEY (CustomerID)
establishes a relationship between two REFERENCES
tables by linking a column in one table Customers(CustomerID)
to the primary key in another table. );
DBMS
Example
DEFAULT Constraint CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Provides a default value for a column Name VARCHAR(100),
when no value is specified. JoinDate DATE DEFAULT
CURRENT_DATE
);
Q- 9 Define Primary key, foreign key, NOT NULL constraints and referential integrity
constraint?
• Primary Key: A column (or set of columns) that uniquely identifies each
row in a table. It cannot have NULL values and must be unique for each
record.
• Foreign Key: A column (or set of columns) that establishes a link between
data in two tables by referring to the primary key in another table. It ensures
that a value in one table must correspond to a value in another.
• NOT NULL: Ensures that a column cannot have NULL values, meaning
every row must contain a valid (non-null) value in that column.
• Referential Integrity: A rule that ensures foreign key values in one table
correspond to primary key values in another table, maintaining valid
relationships between tables and preventing orphaned records.
Ans
Network Model:
• Structure: The Network model organizes data using a graph structure, with
records represented as nodes and relationships as edges. Each record can
have multiple parent and child records, forming a many-to-many
relationship.
• Representation: Data is stored in a hierarchical structure, but unlike the
hierarchical model, a child can have multiple parents.
• Example: Consider an employee and project database where an employee
can work on multiple projects, and a project can have multiple employees.
The Network model represents this as a complex, interconnected graph.
Relational Model:
Ans
Works with tuples (rows) of a relation Works with individual fields (domains)
(table). of a relation.
DBMS
Q-12 Explain the working of the Cartesian Product operation with an appropriate
Table.
Definition:
If you have two sets AAA and BBB, the Cartesian Product of AAA and BBB,
denoted as A×BA \times BA×B, is the set of all possible pairs (a,b)(a, b)(a,b),
where a∈Aa \in Aa∈A and b∈Bb \in Bb∈B.
Formally:
Example:
• Set A (Students):
Student ID Name
1 Alice
DBMS
2 Bob
• Set B (Courses):
Course
Course ID
Name
101 Math
102 Science
Course
Student ID Name Course ID
Name
1 Alice 101 Math
1 Alice 102 Science
2 Bob 101 Math
2 Bob 102 Science
Explanation:
• The Cartesian Product takes each row from Set A and pairs it with every row
from Set B.
• For example, Alice (Student ID 1) is paired with both Math (Course ID 101)
and Science (Course ID 102).
• Similarly, Bob (Student ID 2) is paired with both Math and Science.
Key Points:
1. If Set A has mmm elements and Set B has nnn elements, the resulting
Cartesian Product will have m×nm \times nm×n elements.
2. In database terms, the Cartesian Product can create large results if the sets
are large, so it should be used with caution when the tables have many rows.
Q-13 List the relational algebra operators. Discuss any one such algebra operator
with suitable examples.
Ans
Result
Ans Armstrong's axioms are a set of inference rules used in relational database
theory to derive all functional dependencies (FDs) on a relational database schema.
These rules are sound (only generate correct functional dependencies) and complete
(can derive all functional dependencies that hold in the schema). They were
introduced by William W. Armstrong in 1974.
Here are the three basic axioms, along with their meanings:
• Meaning: If an attribute set YYY is a subset of another set XXX, then XXX
functionally determines YYY. This is called a trivial dependency because the
set of attributes trivially determines its own subset.
2. Augmentation (Extension)
If X→YX \to YX→Y, then XZ→YZXZ \to YZXZ→YZ for any ZZZ.
• Meaning: If XXX determines YYY, then adding another attribute set ZZZ to
both XXX and YYY still results in a valid functional dependency. This
essentially says that adding more attributes to both sides of a dependency
doesn't invalidate it.
DBMS
If X→YX \to YX→Y and Y→ZY \to ZY→Z, then X→ZX \to ZX→Z.
Using the three basic axioms, you can derive some additional rules:
• Union: If X→YX \to YX→Y and X→ZX \to ZX→Z, then X→YZX \to
YZX→YZ.
• Decomposition: If X→YZX \to YZX→YZ, then X→YX \to YX→Y and X→ZX
\to ZX→Z.
• Pseudotransitivity: If X→YX \to YX→Y and WY→ZW Y \to ZWY→Z, then
XW→ZXW \to ZXW→Z.
These axioms are fundamental to reasoning about functional dependencies and are
crucial for database normalization, which is the process of organizing a database to
minimize redundancy and improve data integrity.
Ans Let R be a relation schema having n attributes A1, A2, A3,…, An.
Q-16 What is meant by normalization? Write its need. List and discuss various
normalization forms.
Ans
• Normalization is the process of removing redundant data from tables to
improve data. Integrity, scalability and storage efficiency.
• data integrity (completeness, accuracy and consistency of data)
• scalability (ability of a system to continue to function well in a growing
amount of work)
• storage efficiency (ability to store and manage data that consumes the least
amount of space)
What we do in normalization?
• Normalization generally involves splitting an existing table into multiple
(more than one) tables, which can be re-joined or linked each time a query is
issued (executed).
• Normal forms:
➢ 1NF (First normal form)
➢ 2NF (Second normal form)
➢ 3NF (Third normal form)
➢ BCNF (Boyce–Codd normal form)
➢ 4NF (Forth normal form)
➢ 5NF (Fifth normal form)
Q-17 state true or false : any relation schema that satisfies BCNF also satisfies 3NF
DBMS
Ans True.
Any relation schema that satisfies Boyce-Codd Normal Form (BCNF) also satisfies Third
Normal Form (3NF).
This is because BCNF is a stricter form of 3NF. BCNF has more stringent requirements
than 3NF, so if a relation schema satisfies BCNF, it automatically satisfies the conditions
for 3NF as well. However, the reverse is not necessarily true; a schema that satisfies 3NF
may not satisfy BCNF.
Ans
Ans
DBMS
The B Tree is a particular m-way tree which can be used to access discs in a
variety of ways. At most m children and m-1 keys can be found in a B-Tree of
order m. One of the main advantages of the B tree is its capacity to store a
large number of keys inside a single node and huge key values while keeping
The need for B-tree emerged as the demand for faster access to physical
storage media such as a hard disk grew. With a bigger capacity, backup
storage devices were slower. There was a demand for data structures that
A binary search tree, an AVL tree, a red-black tree, and other data structures
can only store one key in each node. When you need to store a significant
number of keys, the height of such trees grows quite vast, and the time it
B-tree, on the other hand, can store multiple keys inside a single node and
has several child nodes. It considerably reduces the height, allowing for
Ans
B-tree
A B-tree is a self-balancing tree data structure that maintains sorted data and
making it ideal for databases and file systems. Unlike binary search trees, B-trees
are designed to work efficiently on disk storage, where reading from and writing to
Characteristics:
retrieval by directly accessing the desired record based on the value of the
The efficiency of hashing lies in the fact that the hash function computes the
address or bucket where the data is stored, allowing for constant time
Hash Function
A hash function takes a key as input and produces an integer (hash code)
Hashing
Static Hashing
Dynamic Hashing
Static Hashing:
In static hashing, the number of buckets in memory is fixed, and this number does
not change during the insertion or deletion of records. A fixed hash function maps
Dynamic Hashing:
Dynamic hashing adjusts the number of buckets dynamically based on data size. It
uses techniques like extendible hashing or linear hashing to grow and shrink the
Ans
Static hashing:- in static hashing, the number of buckets in memory is fixed, and this
number does not change during the insertion or deletion of records. A fixed hash function
maps the data to these buckets
Buckets: A predefined number of buckets store records when records exceed bucket
capacity, overflow buckets are used.
DBMS
Hash function : A hash function, say h (key ), maps a search key to a bucket
data set. Means, the data set is modified by adding data to or removing the data from,
on demand hence the name ‘Dynamic’ hashing. Thus, the resulting data bucket keeps
increasing or decreasing depending on the number of records.
In this hashing technique, the resulting number of data buckets in memory is ever-
changing.
Q-22 Explain the steps of query processing with the help of a neat diagram.
Ans
DBMS
Q-23 Describe different join strategies for a query and explain how they affect
Performance.
Ans Here’s a concise breakdown of join strategies and their performance impact:
Key Factors: Table size, indexing, memory, and data sorting dictate which strategy is
optimal.
Q-24 Explain linear search and binary search algorithms for selection operation.
Ans Linear Search and Binary Search are two fundamental algorithms used for searching
elements in a collection, such as an array or list. They differ in their approach and
efficiency.
1. Linear Search:
Description:
• In a linear search, you start from the first element and sequentially check each
element of the list until you find the target element or reach the end of the list.
• It doesn't require the list to be sorted.
Algorithm:
Example:
python
Copy code
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
DBMS
return i
return -1 # Target not found
2. Binary Search:
Description:
• In a binary search, the list must be sorted beforehand. The search algorithm works
by repeatedly dividing the search interval in half. It compares the target value with
the middle element of the array.
• If the target is less than the middle element, it searches the left half; if greater, it
searches the right half.
Algorithm:
Example:
python
Copy code
def binary_search(arr, target):
low = 0
high = len(arr) - 1
Q-25 Explain linear search and binary search algorithms for selection operation.
Ans
Linear search(A1)
• It scans each blocks and tests all records to see whether they satisfy the selection
Condition .
• Cost of linear search (worst case )=br
• Br denotes number of blocks containing records from relation r
• If the selection condition is there on a (primary) key attribute, then system can
stop searching if the required record is found.
• Cost of linear search (best case)=(br/2)
• If the selection is on non (primary) key attribute then multiple block may
contains required records, then the cost of scanning such blocks need to be
added to the cost estimate.
• Linear search can be applied regardless of
• Selection condition
• Ordering of records inthe file (relation)
• This algorithm is slower than binary search algorithm.
Binary search :-
• Generally this algorithm is sed if selection is an equality comparison on the
(primary) key attribute and file (relation) is ordered (sorted) on (primary) key
attribute
• Cost of binary search =[log2(br)] br denotes number if blocks containing records
from relation r
• This algorithm is father than linear search algorithm.
and use that index for reading the relation in sorted order. However, using an
index, we sort the relation logically, not physically. Thus, sorting is
performed for cases that include:
The UNIQUE and PRIMARY KEY constraints in a database both ensure that the
values in certain columns are unique, but they have important differences:
1. Uniqueness of Values:
a. UNIQUE Constraint: Ensures that all values in a column (or a
combination of columns) are unique across the rows in a table.
However, it allows multiple NULL values in the column, as NULL is
treated as an unknown value and not compared for uniqueness.
b. PRIMARY KEY Constraint: Also ensures uniqueness, but it doesn't
allow NULL values at all. Every row must have a unique value for the
column or set of columns designated as the primary key.
2. Purpose:
a. UNIQUE: Typically used when a column must have unique values,
but it's not necessarily the primary identifier for a record in the table
(e.g., email addresses).
b. PRIMARY KEY: A column (or combination of columns) that
uniquely identifies each record in the table. It is the main identifier for
records, ensuring no two records can share the same primary key.
3. Number of Constraints Allowed:
a. UNIQUE: You can have multiple UNIQUE constraints in a single
table.
b. PRIMARY KEY: There can only be one PRIMARY KEY constraint
per table.
DBMS
4. Index Creation:
a. UNIQUE: Automatically creates a unique index to enforce the
constraint, ensuring efficient lookups for unique values.
b. PRIMARY KEY: Also creates a unique index but often used as the
default clustered index (though this can vary depending on the
database system).
Q-26 Apply different SQL aggregate functions with an
Example.
Ans
SQL aggregate functions perform calculations on a set of values and return a single value.
Here are common SQL aggregate functions, along with examples:
sql
Copy code
SELECT COUNT(*) AS total_employees
FROM employees;
• Explanation: This query counts the total number of rows in the employees table,
giving the total number of employees.
sql
Copy code
SELECT SUM(salary) AS total_salary
FROM employees;
• Explanation: This query calculates the total salary of all employees by summing the
salary column.
sql
Copy code
DBMS
sql
Copy code
SELECT MIN(salary) AS minimum_salary
FROM employees;
• Explanation: This query returns the minimum salary from the employees table.
sql
Copy code
SELECT MAX(salary) AS maximum_salary
FROM employees;
• Explanation: This query returns the highest salary in the employees table.
Aggregate functions can be used with the GROUP BY clause to calculate values for different
groups of data.
sql
Copy code
SELECT department, COUNT(*) AS total_employees, AVG(salary) AS
average_salary
FROM employees
GROUP BY department;
• Explanation: This query groups the data by department and returns the total
number of employees and average salary for each department.
DBMS
You can use the HAVING clause to filter the results of an aggregate function.
sql
Copy code
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
• Explanation: This query returns only departments that have more than 10
employees.
sql
Copy code
SELECT COUNT(DISTINCT department) AS distinct_departments
FROM employees;
sql
Copy code
SELECT COUNT(*) AS total_employees FROM employees;
sql
Copy code
SELECT SUM(salary) AS total_salary FROM employees;
sql
Copy code
SELECT AVG(salary) AS average_salary FROM employees;
sql
Copy code
SELECT MIN(salary) AS minimum_salary FROM employees;
DBMS
sql
Copy code
SELECT MAX(salary) AS maximum_salary FROM employees;
sql
Copy code
SELECT COUNT(DISTINCT department) AS unique_departments FROM
employees;
sql
Copy code
SELECT department, AVG(salary) FROM employees GROUP BY
department;
sql
Copy code
SELECT department, COUNT(*) FROM employees GROUP BY
department HAVING COUNT(*) > 10;
Q-26 How does the CHECK constraint help maintain data integrity? Explain with
an example.
Ans
The CHECK constraint in SQL helps maintain data integrity by ensuring that the
values entered into a column satisfy a specific condition or meet a defined rule.
This ensures that only valid data is stored in the table according to the business or
data rules.
Example:
Suppose you have an employees table, and you want to ensure that an employee's
salary is always greater than 0 and less than or equal to 100,000. You can apply the
CHECK constraint as follows:
sql
Copy code
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2),
CHECK (salary > 0 AND salary <= 100000)
);
• Explanation: This CHECK constraint ensures that every value entered into
the salary column is greater than 0 and less than or equal to 100,000. If
someone tries to insert a salary outside this range, the database will reject the
data and enforce integrity.
DBMS
Benefits of CHECK:
• Ensures logical consistency (e.g., age can't be negative, quantity can't exceed
stock levels).
• Reduces the risk of errors or invalid data being stored in the database.
Ans
Primary Key and Foreign Key are fundamental concepts in relational databases
that help maintain the integrity and relationships between tables.
1. Primary Key:
• Characteristics:
o Uniqueness: Each value in the primary key column must be unique
across the table.
o Non-nullable: A primary key cannot have NULL values.
o Single table: Each table can have only one primary key, although it
can be composed of multiple columns (composite key).
• Example:
sql
Copy code
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2)
);
2. Foreign Key:
• Definition: A foreign key is a column (or a set of columns) in one table that
references the primary key of another table. It establishes a relationship
between the two tables and enforces referential integrity by ensuring that the
value in the foreign key column matches a valid value in the referenced
primary key.
• Characteristics:
o Referential Integrity: Ensures that a record in one table corresponds
to a valid record in another table.
o Nullable: Foreign keys can accept NULL values, which means the
relationship is optional.
o Multiple per table: A table can have multiple foreign keys.
• Example:
sql
DBMS
Copy code
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
Q-28 Write SQL statements (Query) for the following : 1. Create table Employee
(emp_no, emp_name, emp_sal, emp_comm, dept_no). 2. Insert a minimum of 8
values in the table Employee 3. display table Employee.
Ans
emp_name VARCHAR(100),
dept_no INT
DBMS
);
VALUES
Q-