DBMS Interview Questions and Answers
DBMS Interview Questions and Answers
1. ER Diagrams
Easy Level
What is an ER diagram, and why is it used?
- Strong Entity: Has a primary key that uniquely identifies each instance.
- Weak Entity: Depends on a strong entity for identification and lacks its own primary key.
A primary key is a unique identifier for each record in a table, ensuring no duplicate entries
exist.
Moderate Level
What are composite and derived attributes in an ER diagram? Give examples.
- Composite Attribute: Contains multiple sub-attributes (e.g., Address with Street, City, ZIP).
- Derived Attribute: Can be calculated from other attributes (e.g., Age derived from Date of
Birth).
Relationships are represented by diamond shapes connecting two or more entities, with
lines indicating the type and cardinality of the relationship.
Explain how you would identify and represent a weak entity in an ER diagram.
A weak entity does not have its own primary key and relies on a strong entity for
identification. It’s typically represented with a double rectangle and a double-lined
relationship to indicate total participation.
Hard Level
How would you represent a recursive relationship in an ER diagram?
A recursive relationship occurs when an entity relates to itself (e.g., Employee supervises
another Employee). This is represented by an entity having a relationship line looping back
to itself.
Describe a situation where you would use an associative entity. How would you
represent it in an ER diagram?
When a many-to-many relationship has its own attributes (e.g., Order and Product forming
OrderDetails with Quantity), an associative entity is used to hold relationship-specific
details, represented as a rectangle connected to each participating entity.
The choice depends on the requirements, often with the entity that logically owns or
initiates the relationship (e.g., Department as the main entity in a Manages relationship with
Employee).
Explain how you would convert an ER diagram into tables for a relational database.
What are the steps involved?
Steps include:
- Create a table for each entity with its attributes.
- Convert relationships based on cardinality (one-to-many as foreign keys, many-to-many as
join tables).
- For weak entities, include the primary key of the related strong entity as part of their
primary key.
2. Participation
Easy Level
What is participation in the context of ER diagrams?
- Total Participation: Every instance of the entity must be in the relationship (e.g., every
Student must be Enrolled in a Course).
- Partial Participation: Only some instances participate (e.g., some Employees may be
assigned to a Project).
Moderate Level
Can an entity have both total and partial participation in different relationships? Explain
with an example.
Yes, an entity may have total participation in one relationship and partial in another. For
instance, Students must be enrolled in Courses (total participation) but may optionally join
Clubs (partial participation).
Participation dictates whether foreign keys can be nullable; total participation implies non-
null constraints, while partial participation allows nulls if an instance is not in the
relationship.
What are some real-world scenarios where you would require total participation?
In a library system, every Book must be associated with a Category, and each Employee in a
payroll system must belong to a Department.
Hard Level
How would you approach designing an ER diagram if all entities have total participation
in their relationships?
Every relationship would need to ensure all entity instances are included, possibly
requiring additional entities or constraints to avoid orphaned records.
The database might enforce relationships where some entities should not be forced to
participate, leading to data inconsistency and incorrect data associations.
Easy Level
What is a cardinality ratio, and why is it important in ER diagrams?
A cardinality ratio defines the maximum number of instances of one entity that can relate to
another, helping to map relationships accurately.
- One-to-One: Each instance of an entity is associated with one instance of another (e.g., each
Person has one Passport).
- One-to-Many: One entity instance relates to many others (e.g., one Department has
multiple Employees).
- Many-to-Many: Multiple instances in one entity relate to multiple instances in another
(e.g., Students and Courses).
Moderate Level
What cardinality ratio would you assign in an ER diagram for a school where each
student has a unique library card, but each library card is assigned to only one student?
One-to-One, as each Student corresponds to a unique Library Card, and each Library Card to
one Student.
Hard Level
Can you describe how cardinality ratios influence table design when converting an ER
diagram into a relational model?
One-to-one and one-to-many relationships are handled with foreign keys, while many-to-
many require a join table with foreign keys from both entities.
Many-to-many relationships increase storage and query complexity due to additional join
tables, while one-to-many and one-to-one are more efficient for storage and retrieval.
How can you convert an M
relationship into 1
relationships in an ER model? Explain with an example.
To convert an M
relationship into 1
relationships, introduce an associative entity (also called a junction table) that acts
as an intermediary. This associative entity will hold foreign keys to both original
entities, representing each unique combination. For instance, for a relationship
between "Student" and "Course," where each student can enroll in multiple
courses and each course can have multiple students, an "Enrollment" associative
entity can be created. This entity will have two foreign keys: Student ID and
Course ID. Now, "Student" has a 1
What is an associative (or junction) entity, and why is it used in database design
to handle many-to-many relationships?
relationship into 1
Given an M
relationship between entities "Author" and "Book," how would you modify the ER
diagram to represent this relationship as 1
relationships?
To convert this M
relationship, create an associative entity called "AuthorBook," which has foreign
keys for both "Author ID" and "Book ID." Now, the relationship becomes two 1
What changes are necessary in the relational schema when converting a many-to-
many relationship to one-to-many relationships?
When converting an M
relationship to 1
, add a new table (associative entity) to serve as a bridge. This new table should
include foreign keys referencing each of the original entities, which establish two
one-to-many relationships. Constraints may be added to these foreign keys to
enforce referential integrity.
If you have an M
relationship between "Product" and "Order," explain the steps and table structure
needed to convert this into a one-to-many schema.
First, create an associative entity, e.g., "OrderProduct," with foreign keys for both
"Product ID" and "Order ID." This table will store each unique combination of
products and orders. The "Product" entity now has a 1
Describe the purpose of foreign keys in the associative table when converting an
M
relationship to 1
. Why are these keys critical?
Foreign keys in an associative table establish the connections between the original
entities, allowing each instance of one entity to link to multiple instances of the
other. They are critical because they ensure referential integrity by linking entries
in the associative table back to the original tables, preventing orphaned records
and enforcing valid data relationships.
When an M
relationship is converted to 1
relationships, what constraints are applied to ensure data integrity?
to 1
relationships?