DBMS
DBMS
3 QN
Describe the three-tier schema architecture in database
systems
ANS
The three-tier schema architecture in database systems is
designed to separate the database into three levels of
abstraction:
1. Internal Schema (Physical Level):
o Describes how data is physically stored in the
system.
o It includes details like file organization, indexing,
and storage methods.
o Focuses on optimizing data storage and retrieval
performance.
2. Conceptual Schema (Logical Level):
o Represents the logical structure of the entire
database.
o Defines all the data elements, their relationships,
and constraints, but not the physical storage
details.
o Provides a unified view of the data, abstracting the
complexities of the physical layer.
3. External Schema (View Level):
o Represents user-specific views of the database.
o Defines how different users or applications interact
with the database, showing only the relevant data
to each user or group.
o Allows for customization of access and
presentation, ensuring data security and privacy.
4.Q
What are the different types of database users? Explain their
roles with suitable examples.
ANS
Different types of database users and their roles are:
1. Database Administrators (DBAs):
o Role: They are responsible for managing and
maintaining the database system. They ensure data
security, backup, performance optimization, and
resolve any issues.
o Example: A DBA may configure user permissions or
perform regular database backups.
2. End Users:
o Role: They interact with the database through
applications or user interfaces. They enter, retrieve,
and process data according to their needs.
o Example: A sales employee accessing customer
data to place an order or check inventory.
3. Application Programmers:
o Role: They design and develop applications that
interact with the database using programming
languages like Java, Python, or SQL to execute
database queries and transactions.
o Example: A developer creates a web application to
retrieve and display data from the database.
4. Data Analysts:
o Role: They analyze data stored in the database to
generate reports and insights for decision-making.
o Example: A data analyst queries the database to
produce financial reports for the management
team.
5. System Analysts:
o Role: They analyze business requirements and
translate them into technical specifications for
database design and system integration.
o Example: A system analyst determines the data
structure needed for a new customer management
system.
5Q
Define Data Model and briefly explain the types of Data
Models.
ANS
A Data Model is a conceptual framework used to define the
structure, relationships, and constraints of data within a
database. It provides a way to organize and manage data,
ensuring consistency and efficiency in data management.
Types of Data Models:
1. Hierarchical Data Model:
o Structure: Data is organized in a tree-like structure
with parent-child relationships.
o Example: A company's organizational structure,
where each employee reports to a manager,
forming a hierarchy.
2. Network Data Model:
o Structure: Data is organized in a graph structure,
allowing multiple relationships between entities
(records).
o Example: A network of cities where multiple cities
can be connected by different routes.
3. Relational Data Model:
o Structure: Data is stored in tables (relations) with
rows (records) and columns (attributes), and
relationships are established through keys.
o Example: A student database where each student is
a record in a table, and relationships between
tables are managed using foreign keys.
4. Object-Oriented Data Model:
o Structure: Data is represented as objects, similar to
how objects are used in object-oriented
programming, with attributes and methods.
o Example: A database for a car dealership where
each car is an object with properties like make,
model, and price.
5. Entity-Relationship (ER) Model:
o Structure: Data is represented by entities (objects)
and the relationships between them, often used for
designing databases.
o Example: A database for a library where entities are
books, authors, and borrowers, and relationships
are defined between them.
6Q
List and explain different types of attributes and differentiate
between strong entity and weak entity in ER Model
ANS
Types of Attributes in ER Model:
1. Simple (Atomic) Attribute:
o Description: An attribute that cannot be divided
further into smaller components.
o Example: A person's age or a product’s price.
2. Composite Attribute:
o Description: An attribute that can be divided into
smaller sub-parts, which are meaningful
individually.
o Example: A "Full Name" attribute that can be
divided into "First Name" and "Last Name."
3. Derived Attribute:
o Description: An attribute whose value can be
derived from other attributes.
o Example: "Age" can be derived from the "Date of
Birth" attribute.
4. Multi-valued Attribute:
o Description: An attribute that can have multiple
values for a single entity.
o Example: A person may have multiple phone
numbers.
5. Key Attribute:
o Description: An attribute that uniquely identifies an
entity.
o Example: "Employee ID" for an employee entity.
1. Specialization:
• Description: Specialization is the process of defining sub-
entities (or subclasses) from a higher-level entity (or
superclass) based on certain characteristics or attributes.
It's essentially the reverse of generalization.
• Purpose: It helps model more specific entities from a
general entity.
Real-life Example:
• Consider an entity Vehicle. The Vehicle entity could be
specialized into specific types like Car, Truck, and
Motorcycle, each having additional attributes or
behaviors specific to that type of vehicle.
• Vehicle (Superclass): Attributes could include Vehicle ID,
Make, Model.
• Car (Subclass): Additional attributes might include
Number of Doors, Fuel Type.
• Truck (Subclass): Additional attributes might include
Payload Capacity.
• Motorcycle (Subclass): Additional attributes might
include Engine Type.
2. Generalization:
• Description: Generalization is the process of combining
two or more lower-level entities (or subclasses) into a
higher-level, more general entity. It’s the reverse of
specialization.
• Purpose: It allows you to abstract common attributes or
relationships from multiple entities into a single, more
generalized entity.
Real-life Example:
• Consider the entities Salaried Employee and Hourly
Employee. Both share common attributes such as
Employee ID, Name, and Address. Using generalization,
these two entities can be combined into a more general
entity, Employee.
• Employee (Superclass): Common attributes could
include Employee ID, Name, Address.
• Salaried Employee (Subclass): Could have additional
attributes like Annual Salary.
• Hourly Employee (Subclass): Could have additional
attributes like Hourly Rate and Hours Worked.
Key Differences:
• Specialization: From a general entity to more specific
ones (e.g., Vehicle → Car, Truck, Motorcycle).
• Generalization: From specific entities to a more general
entity (e.g., Salaried Employee and Hourly Employee →
Employee).
8Q
Consider an online library management system. Design an ER
diagram that includes entities like Books, Members, and
Transactions. Specify attributes and relationships
ANS
Here's a simple ER diagram design for an online library
management system, including the Books, Members, and
Transactions entities, along with their attributes and
relationships:
1. Entities and Attributes:
• Book
o Attributes:
▪ Book_ID (Primary Key)
▪ Title
▪ Author
▪ Genre
▪ Publisher
▪ ISBN
▪ Publication_Year
• Member
o Attributes:
▪ Member_ID (Primary Key)
▪ First_Name
▪ Last_Name
▪ Email
▪ Phone_Number
▪ Address
▪ Membership_Date
• Transaction
o Attributes:
▪ Transaction_ID (Primary Key)
▪ Issue_Date
▪ Due_Date
▪ Return_Date
▪ Fine (if applicable)
2. Relationships:
• Borrows (Between Member and Book)
o A Member can borrow many Books, and each Book
can be borrowed by many Members (many-to-
many relationship).
o Attributes of the relationship:
▪ Transaction_ID (to uniquely identify each
borrowing transaction)
▪ Issue_Date
▪ Due_Date
▪ Return_Date
• Has (Between Transaction and Member)
o A Transaction is associated with one Member,
representing the member who borrowed the book.
• Has (Between Transaction and Book)
o A Transaction is associated with one Book,
representing the book being borrowed.
UNIT -2
1Q
Describe the importance of NULL values in relational
databases. In what situations is using NULL values beneficial?
ANS
Importance of NULL Values in Relational Databases:
• NULL means "no value" or "unknown value." It helps to
show when data is missing, not yet entered, or not
applicable.
• NULL is different from an empty string ("") or zero; it
truly represents the absence of data.
When NULL Values are Beneficial:
1. Missing Information:
o When data is not available or has not been
provided yet.
o Example: A person’s middle name is unknown, so
the field can be NULL.
2. Optional Data:
o When some data is optional and not required to be
filled in.
o Example: A customer might not have a fax number,
so the field can be NULL.
3. New Records:
o When adding a new record and some information is
not yet available.
o Example: A new order might not have a shipment
date yet, so it’s set as NULL.
4. Unknown Values:
o When the value is unknown or yet to be
determined.
o Example: The expiry date of a product is unknown
at the time of entry, so it's NULL until later.
5. Missing Relationships:
o When a relationship does not apply to a certain
record.
o Example: If a book does not have an author listed
yet, the author field can be NULL.
Why NULL is Useful:
• Shows Missing Data: It helps to clearly mark where data
is missing, instead of using misleading default values like
empty strings or zeros.
• Helps with Accurate Data: It keeps data accurate and
reflects real-world situations where information is
sometimes missing or unknown.
• Flexible for Queries: It allows the database to handle
missing data correctly when searching or analyzing the
information.
In simple terms, NULL helps databases handle situations
where data is missing or unknown, making the system more
accurate and flexible.
2Q
Explain the difference between dropping, truncating and
deleting a table, write example query for each one.
ANS
Difference Between Dropping, Truncating, and Deleting a
Table:
1. DROP:
o Action: Completely removes the table from the
database, along with its structure and data.
o Effect: The table is permanently deleted and cannot
be recovered (unless backups exist).
o Use: Used when you no longer need the table at all.
o Example Query:
o DROP TABLE table_name;
2. TRUNCATE:
o Action: Removes all rows from the table but keeps
the table structure for future use.
o Effect: The data is deleted, but the table structure
(columns, constraints, etc.) remains intact. It is
faster than DELETE because it doesn't log individual
row deletions.
o Use: When you want to delete all data from the
table but keep the table itself for future use.
o Example Query:
o TRUNCATE TABLE table_name;
3. DELETE:
o Action: Deletes specific rows from the table based
on a condition (or all rows if no condition is
provided).
o Effect: The data is deleted, but the table structure
remains. DELETE is slower than TRUNCATE because
each row deletion is logged.
o Use: When you want to delete specific data or rows
from the table.
o Example Query:
o DELETE FROM table_name WHERE condition;
Key Differences:
• DROP: Removes the table and all its data permanently.
• TRUNCATE: Removes all rows but keeps the table
structure.
• DELETE: Removes specific rows or all rows based on a
condition.
Summary:
• Use DROP when you want to completely remove the
table.
• Use TRUNCATE when you need to remove all rows but
keep the table.
• Use DELETE when you need to remove specific rows or
when you need to have control over which rows are
deleted.
3Q
What are integrity constraints in a relational database?
Explain key constraints and entity integrity constraints with
example
ANS
Integrity Constraints in a Relational Database:
Integrity constraints are rules that ensure the accuracy and
consistency of the data in a relational database. They make
sure that the data entered into the database is valid and
reliable.
Key Types of Integrity Constraints:
1. Entity Integrity Constraint:
o Ensures that each record in a table has a unique
identifier, typically through the primary key. The
primary key must be unique and cannot be NULL.
o Example:
▪ In a Student table, the Student_ID must be
unique and cannot be NULL.
o CREATE TABLE Student (
o Student_ID INT PRIMARY KEY, -- No NULL
allowed in Student_ID
o Name VARCHAR(50)
o );
2. Referential Integrity Constraint:
o Ensures that a foreign key in one table refers to a
valid record in another table.
o Example:
▪ In a Book table, the Publisher_ID must exist in
the Publisher table.
o CREATE TABLE Publisher (
o Publisher_ID INT PRIMARY KEY
o );
o
Summary:
• Domains define valid values for each column.
• Attributes define the structure (columns) of a table.
• Tuples represent individual data entries (rows).
• Relations are the tables that store the data.
These concepts help in creating well-organized, consistent,
and easily manageable databases.
5Q
List and explain any five Relational Algebra operators with
suitable example for each
ANS
Here are five Relational Algebra operators with simple
explanations and examples:
1. SELECT (σ):
• Definition: The SELECT operator is used to filter rows
based on a specified condition.
• Example:
o Query: Retrieve all students who are older than 18.
• σ(Age > 18)(Student)
o This selects rows where the Age is greater than 18
from the Student table.
2. PROJECT (π):
• Definition: The PROJECT operator is used to select
specific columns from a table.
• Example:
o Query: Retrieve only the Name and Age columns
from the Student table.
• π(Name, Age)(Student)
o This selects only the Name and Age columns from
the Student table.
3. UNION (∪):
• Definition: The UNION operator combines two relations
and returns all rows that are present in either of the two
tables, without duplicates.
• Example:
o Query: Combine the data of Students and Teachers
who have the same City.
• π(City)(Students) ∪ π(City)(Teachers)
o This combines the City values from both the
Students and Teachers tables.
4. INTERSECTION (∩):
• Definition: The INTERSECTION operator returns rows
that are common in both relations.
• Example:
o Query: Find students who are also enrolled in the
Math course.
• π(Student_ID)(Student) ∩
π(Student_ID)(Math_Enrollment)
o This returns the Student_ID values that appear in
both the Student and Math_Enrollment tables.
5. JOIN (⨝):
• Definition: The JOIN operator combines rows from two
tables based on a related column.
• Example:
o Query: Retrieve the names of students along with
their Course name by joining the Student and
Enrollment tables.
• Student ⨝ Enrollment
o This joins the Student table with the Enrollment
table based on a common column (e.g.,
Student_ID) to show the courses the students are
enrolled in.
Summary of Operators:
1. SELECT (σ): Filters rows based on a condition.
2. PROJECT (π): Selects specific columns.
3. UNION (∪): Combines two relations, removing
duplicates.
4. INTERSECTION (∩): Returns common rows in both
relations.
5. JOIN (⨝): Combines related rows from two tables based
on a condition.
These operators help manipulate and retrieve data efficiently
in relational databases.
6Q
Compare Relational Algebra and Relational Calculus. How do
they differ in their approach to querying databases?
ANS
Relational Algebra vs. Relational Calculus
Relational Algebra and Relational Calculus are both used for
querying relational databases, but they differ in how they
express queries.
1. Relational Algebra:
• Type: Procedural language (describes how to get the
result).
• Approach: Focuses on specifying the steps or operations
to be performed on the database.
• Operations: Uses operators like SELECT, PROJECT,
UNION, JOIN, etc., to manipulate relations (tables)
directly.
• Result: It tells you the specific sequence of operations to
get the desired result.
• Example:
o To get the names of students who are older than
18:
o σ(Age > 18)(Student)
2. Relational Calculus:
• Type: Non-procedural language (describes what result is
needed).
• Approach: Focuses on specifying the desired result, not
how to achieve it.
• Operations: Uses logical expressions (like conditions) to
define the result.
• Result: It tells you the condition that the result must
satisfy, leaving the actual method to the system.
• Example:
o To get the names of students who are older than
18:
o { S.Name | S ∈ Student AND S.Age > 18 }
Key Differences:
Feature Relational Algebra Relational Calculus
Non-procedural
Procedural (describes
Type (describes what data to
how to get data)
get)
Describes operations Describes the condition
Query
step-by-step to satisfy
Focus How to retrieve data What data to retrieve
Requires knowing More abstract and easier
Complexity
specific operations to understand
Example σ(Age > 18)(Student) { S.Name
Summary:
• Relational Algebra specifies the steps needed to get the
data.
• Relational Calculus specifies the condition that the data
must satisfy.
Both are foundational in database theory, but Relational
Algebra is more about the procedure, while Relational
Calculus is about the result.
7Q
Consider the relation: Student Schema (Stu_ID, Stu_Name,
Stu_Age, Stu_Dept, Stu_GPA) Write SQL queries to: insert,
update, delete, alter , drop
ANS
1. INSERT:
To insert a new record into the Student table.
INSERT INTO Student (Stu_ID, Stu_Name, Stu_Age, Stu_Dept,
Stu_GPA)
VALUES (1, 'John Doe', 20, 'Computer Science', 3.8);
2. UPDATE:
To update an existing record in the Student table. For
example, to change the GPA of a student with Stu_ID 1.
UPDATE Student
SET Stu_GPA = 3.9
WHERE Stu_ID = 1;
3. DELETE:
To delete a record from the Student table. For example, to
delete a student with Stu_ID 1.
DELETE FROM Student
WHERE Stu_ID = 1;
4. ALTER:
To modify the structure of the Student table. For example,
adding a new column for Stu_Email.
ALTER TABLE Student
ADD Stu_Email VARCHAR(100);
5. DROP:
To delete the entire Student table from the database.
DROP TABLE Student;
Summary:
• INSERT: Adds new data.
• UPDATE: Modifies existing data.
• DELETE: Removes data.
• ALTER: Changes the structure of the table.
• DROP: Deletes the table completely.
8Q
Given a company database with relations Employee(Emp_ID,
Name, Salary, Dept_ID) and Department(Dept_ID,
Dept_Name), Demonstrate the usage of selection, projection,
rename operators of relational algebra for the above relation
ANS
Here are examples of the selection, projection, and rename
operators for the given relations:
Relations:
1. Employee(Emp_ID, Name, Salary, Dept_ID)
2. Department(Dept_ID, Dept_Name)
1. SELECTION (σ):
The selection operator filters rows based on a condition.
Example:
Select employees with a Salary greater than 50,000:
σ(Salary > 50000)(Employee)
• This will return all employees whose Salary is greater
than 50,000.
2. PROJECTION (π):
The projection operator selects specific columns from a table.
Example:
Get the names and salaries of employees:
π(Name, Salary)(Employee)
• This will return a list of Name and Salary for all
employees.
3. RENAME (ρ):
The rename operator renames a relation or an attribute.
Example:
Rename the Employee relation to "Staff":
ρ(Staff)(Employee)
• This will rename the Employee table to Staff.
Example:
Rename the "Name" attribute to "Employee_Name" in the
Employee relation:
ρ(Employee_Name/Name)(Employee)
• This will rename the Name column to Employee_Name
in the Employee relation.
Summary:
• Selection (σ): Filters data based on a condition.
• Projection (π): Chooses specific columns.
• Rename (ρ): Renames tables or columns.
These operations help in querying and manipulating data in a
relational database efficiently.
9Q
A relational schema contains the tables Orders(Order_ID,
Customer_ID, Order_Date, Amount) and
Customers(Customer_ID, Name, City). Write an SQL query to
list all customers who have placed an order in the last 6
months. Write an SQL query to find no. of orders group by
Order_Date. Write an SQL query to find no. of customers
count group by City. Write an SQL query to find customers
orderby City name in descending order. Write an SQL query
to find maximum amount from orders.
ANS
Here are the SQL queries for the tasks you've mentioned:
1. List all customers who have placed an order in the last 6
months:
SELECT DISTINCT c.Name, c.City
FROM Customers c
JOIN Orders o ON c.Customer_ID = o.Customer_ID
WHERE o.Order_Date >= CURDATE() - INTERVAL 6 MONTH;
• This query selects customers who have placed orders in
the last 6 months by joining the Customers and Orders
tables.
Summary:
1. Customers who placed orders in the last 6 months.
2. Number of orders grouped by Order_Date.
3. Number of customers grouped by City.
4. Customers ordered by City in descending order.
5. Maximum Amount from the Orders table.
These queries provide useful insights into customer and order
data in an easy-to-understand way.