0% found this document useful (0 votes)
33 views17 pages

DBMS Answers Mid Questin Bank

Uploaded by

w5hnjtb5tk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views17 pages

DBMS Answers Mid Questin Bank

Uploaded by

w5hnjtb5tk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit 1: Introduction to Database - Answers

1. What is a database? (1 mark)

A database is an organized collection of data that is stored and accessed electronically. It allows for
efficient retrieval, insertion, updating, and management of data. Databases are designed to handle
large amounts of information in a structured format.

2. What is a Database Management System (DBMS)? How does it differ from a traditional file
system? (2 marks)

A Database Management System (DBMS) is a software system that facilitates the creation,
management, and manipulation of databases. It provides an interface between the users and the
databases to perform various tasks like storing, retrieving, and updating data.

Difference between DBMS and traditional file system:

• Data Structure: In a traditional file system, data is stored in flat files, and there is no
structure for handling complex data relationships. In a DBMS, data is stored in tables and is
structured in a way that supports relationships, constraints, and queries.

• Data Integrity: A DBMS ensures data integrity through constraints (like primary and foreign
keys), while file systems may lack such mechanisms.

• Concurrency and Security: DBMS supports concurrent access, transactions, and security
features like user authentication, while file systems may not provide such features.

• Data Redundancy: A DBMS minimizes data redundancy by normalizing data, whereas a file
system may store duplicate data across multiple files.

3. List and explain the primary components of a DBMS. (3 marks)

The primary components of a DBMS are:

1. Database Engine: This is the core service for storing, retrieving, and managing data. It is
responsible for the actual creation, reading, updating, and deletion of data from the
database.

2. Database Schema: The schema defines the structure of the database, including tables,
views, relationships, and other database elements. It is a blueprint for the database.

3. Query Processor: The query processor translates SQL queries into instructions that the
database engine can execute. It includes a parser, optimizer, and query execution plan
generator.

4. Database Manager: This component ensures data consistency, integrity, and security by
enforcing access controls and transaction management.

5. User Interface: The user interface enables interaction with the DBMS. It can be a command-
line interface (CLI) or a graphical user interface (GUI) for users to input queries, visualize
data, and perform other operations.

4. What are the advantages of using a DBMS? (3 marks)

The advantages of using a DBMS include:


1. Data Redundancy Reduction: A DBMS ensures that data is not duplicated, which reduces
storage costs and improves efficiency by maintaining a single, central repository.

2. Data Integrity and Accuracy: DBMS enforces constraints (like primary and foreign keys) to
ensure data accuracy and consistency across the database.

3. Improved Data Security: A DBMS allows for access control mechanisms, ensuring that only
authorized users can view or modify data, improving overall security.

4. Support for Concurrent Access: Multiple users can access the database simultaneously
without conflicts, thanks to transaction management and concurrency control features.

5. Data Independence: DBMS provides a level of abstraction between the data and the
application, allowing changes to the database structure without affecting the applications
using the database.

5. What are the key functions of a DBMS? (2 marks)

The key functions of a DBMS are:

1. Data Storage Management: DBMS is responsible for storing data efficiently and retrieving it
when needed. It manages the physical storage of data on storage devices like hard drives.

2. Data Retrieval and Query Processing: DBMS processes queries from users or applications
and retrieves the required data based on those queries.

3. Transaction Management: DBMS manages transactions to ensure that data modifications


are done in a consistent and reliable manner. It handles commit, rollback, and concurrency
control.

4. Backup and Recovery: DBMS ensures data is regularly backed up and can be recovered in
case of a system failure or data corruption.

6. Explain the concept of data abstraction in a DBMS. (2 marks)

Data abstraction in a DBMS is the process of hiding the complexity of the data storage and
presentation from the users. It allows users to interact with data without needing to understand the
underlying details of how data is stored or structured. There are three levels of data abstraction:

1. Physical Level: Describes how data is stored in the database (e.g., files, indices).

2. Logical Level: Describes what data is stored in the database, without specifying how it is
stored (e.g., tables, relations).

3. View Level: Provides different perspectives of the database for different users, focusing on
relevant data (e.g., user-specific views or reports).

7. What is data independence? Why is it important in database management? (3 marks)

Data independence refers to the ability to change the schema at one level without affecting the
schema at the next higher level. It ensures that changes in data storage or structure do not affect
application programs or user views.

• Logical Data Independence: The ability to change the logical schema (e.g., adding or deleting
tables) without affecting the external schema or application programs.
• Physical Data Independence: The ability to change the physical schema (e.g., storage devices
or file organization) without impacting the logical schema or applications.

Data independence is important because it allows the database system to adapt to changes in
technology and business needs without requiring extensive rewrites of application programs.

8. Differentiate between logical and physical data independence. (2 marks)

• Logical Data Independence: The ability to change the logical schema (e.g., changing the
table structure) without affecting the external schema or application programs. It is difficult
to achieve, as changes in the logical schema often require application modifications.

• Physical Data Independence: The ability to change the physical schema (e.g., changing how
data is stored) without affecting the logical schema or external applications. It is easier to
achieve as physical changes do not affect the structure or the way users interact with the
data.

11. Explain the external, conceptual, and internal levels of a database system. What is the
significance of each level in database management? (5 marks)

A database system is designed with three levels of abstraction:

1. External Level (User View): This level defines how individual users or user groups view the
data. It provides a tailored view of the database to different users. For example, one user
may see a subset of the data or have a specific format for data presentation.

o Significance: It ensures that users can interact with the database without dealing
with the complexities of the internal structure.

2. Conceptual Level (Logical View): This level defines the logical structure of the entire
database. It provides a unified and abstracted view of the data, describing what data is
stored and its relationships without specifying how it is stored.

o Significance: It ensures consistency of the data and hides the complexities of the
physical storage.

3. Internal Level (Physical View): This level deals with the actual storage of data on the storage
devices. It defines how the data is physically stored, organized, and indexed.

o Significance: It optimizes the storage and retrieval of data, ensuring that the system
performs efficiently.

12. What is mapping in the context of database levels? (3 marks)

Mapping refers to the process of defining the relationship between the different levels of a database
system. There are three types of mappings:

1. External to Conceptual Mapping: Defines how the external views (user views) are related to
the conceptual schema (logical structure).

2. Conceptual to Internal Mapping: Describes how the logical schema is related to the physical
schema (internal storage).

3. External to Internal Mapping: Establishes how user views are related to the physical storage
of data.
Mappings ensure that users can interact with the database at a high level of abstraction while
maintaining the efficiency and consistency of the underlying physical storage.

13. Who are the different types of users in a database system? (4 marks)

Different types of users in a database system include:

1. End Users: Individuals who interact with the database to retrieve or modify data according to
their needs. They may use tools like forms or reports.

2. Database Administrators (DBAs): Responsible for the overall management of the database
system, including security, backups, and performance optimization.

3. Application Programmers: Developers who design applications that interact with the
database, creating interfaces or writing queries for users.

4. System Analysts: Professionals who design the database system’s structure and ensure that
it meets the organization’s requirements.

14. What are the key responsibilities of a Database Administrator (DBA)? (3 marks)

The key responsibilities of a Database Administrator (DBA) include:

1. Database Design: Creating the database structure, including tables, schemas, and
relationships.

2. Security Management: Ensuring data security by managing user access, implementing


authentication, and maintaining encryption.

3. Performance Monitoring: Monitoring and optimizing the performance of the database,


including indexing, query optimization, and resource allocation.

4. Backup and Recovery: Implementing backup strategies and recovery procedures to ensure
data is protected and can be restored in case of failure.

5. Data Integrity and Consistency: Ensuring that the database maintains accuracy, consistency,
and compliance with business rules.

15. What is a data model? Why are data models important in database design? (2 marks)

A data model is a conceptual framework that defines the structure, relationships, and constraints of
data in a database. Data models are essential in database design because:

• They provide a blueprint for organizing

and structuring data.

• They define how data elements relate to each other, ensuring data consistency and integrity.

• They help in understanding user requirements and translating them into an efficient
database structure.

16. Explain the hierarchical data model with an example. (3 marks)

The hierarchical data model organizes data in a tree-like structure, where each record has a single
parent and can have multiple children. The data is stored in nodes, with parent-child relationships.
• Example: An organizational structure where the root node is the company, and each branch
represents different departments. Each department can have several employees as children,
but an employee can belong to only one department.

17. What is the network data model? How is it different from the hierarchical model? (3 marks)

The network data model allows more complex relationships by permitting a record to have multiple
parent nodes, unlike the hierarchical model, which restricts each record to a single parent. It uses a
graph structure to represent relationships.

• Difference: The hierarchical model has a strict tree-like structure, whereas the network
model supports more complex many-to-many relationships.

18. Describe the relational data model. Why is it widely used in modern database systems? (3
marks)

The relational data model organizes data into tables (relations) with rows (tuples) and columns
(attributes). Each table represents an entity, and relationships between tables are defined using
foreign keys.

• Why widely used: It simplifies data modeling, supports powerful querying (SQL), ensures
data integrity with constraints, and allows easy expansion as new data types or relationships
are needed.

Unit 2: Relational Data Model - Answers

19. What is a relational database? Explain its structure. (3 marks)

A relational database is a type of database that stores data in the form of tables (also known as
relations). These tables are composed of rows (tuples) and columns (attributes), where each row
represents a record, and each column represents a data field. The key feature of relational databases
is that they allow for relationships between tables, which are maintained through keys (primary and
foreign keys).

Structure of a relational database:

• Tables (Relations): Data is organized in tables with rows and columns.

• Rows (Tuples): Each row in a table represents a record in the database.

• Columns (Attributes): Each column represents a specific attribute or field of the data.

• Keys (Primary, Foreign): Keys ensure the uniqueness of records and maintain relationships
between tables.

20. Define the following terms: table, row (tuple), column (attribute), and field. (4 marks)

• Table: A table is a collection of related data stored in a structured format with rows and
columns. It is also called a relation in a relational database.

• Row (Tuple): A row, or tuple, is a single record in a table, containing values for each attribute.
Each tuple represents one instance of an entity.

• Column (Attribute): A column, or attribute, is a single field in a table that holds specific data
type values across all rows. It represents a characteristic or property of the entity.
• Field: A field is the intersection of a row and a column. It holds the actual data value for a
specific attribute and record.

21. What is a relation in the context of relational databases? How is it represented? (3 marks)

In a relational database, a relation refers to a table that consists of rows (tuples) and columns
(attributes). Each row in the relation represents a unique record, and each column represents a
property or characteristic of the entity described by the relation.

A relation is represented as:

• Relation Name (Table Name)

• Attributes (Columns), which have a name and a domain (data type).

• Tuples (Rows), which are the actual data entries that satisfy the constraints defined for the
relation.

22. Explain the concept of a schema in relational databases. How is it different from an instance?
(3 marks)

A schema in a relational database is the logical structure that defines the organization of data within
a database. It includes the tables, the columns within those tables, the data types of the columns,
and the relationships between the tables. The schema serves as a blueprint for the database and
does not change frequently.

An instance refers to the actual data stored in the database at any given time. It represents the
current state of the database, where the data within the tables (rows and columns) can vary.

• Schema: The design or structure of the database.

• Instance: The specific data in the database at a particular point in time.

23. What are constraints in relational databases? Give examples of domain, entity integrity, and
referential integrity constraints. (5 marks)

Constraints are rules applied to database tables that enforce data integrity and consistency. They
ensure that the data in the database is accurate, valid, and adheres to specified business rules.

• Domain Constraint: Ensures that the values in a column are within a specified range or
domain. It is the restriction on the type of data that can be stored in a column.

o Example: A DOB column must store valid date values, and a Salary column must
store numeric values greater than 0.

• Entity Integrity Constraint: Ensures that each row in a table has a unique identifier (primary
key) and that this identifier is not null.

o Example: The Student_ID column in the Students table must be unique and not null.

• Referential Integrity Constraint: Ensures that foreign key values in one table correspond to
primary key values in another table. This maintains relationships between tables.

o Example: In the Enrollment table, the Student_ID must reference a valid Student_ID
in the Students table.

24. What is a domain in a relational database? Why is it important? (3 marks)


A domain in a relational database refers to the set of valid values that an attribute (column) can take.
The domain defines the allowable data type, value range, and any constraints on the values that can
be entered into a column.

Importance of domain:

• It ensures data integrity by restricting the types of data that can be stored in a column.

• It helps maintain consistency and accuracy of the data by preventing invalid or inappropriate
values from being entered.

25. What is a primary key? Why is it necessary for a relation? (4 marks)

A primary key is a column (or set of columns) in a relational database table that uniquely identifies
each record (row) in that table. The primary key must have the following properties:

• Uniqueness: No two rows can have the same value for the primary key.

• Not null: The primary key cannot have a null value.

Necessity for a relation:

• A primary key ensures that each record can be uniquely identified.

• It is essential for establishing relationships between tables (through foreign keys).

• It enforces entity integrity by preventing duplicate records in the table.

26. What is a foreign key, and how does it establish relationships between two relations? (4 marks)

A foreign key is a column (or set of columns) in a table that refers to the primary key in another
table. It establishes a relationship between the two tables, ensuring that the data is consistent and
that a record in one table corresponds to a valid record in the related table.

How it establishes relationships:

• The foreign key in the child table links to the primary key in the parent table, creating a
reference.

• For example, in a Orders table, the Customer_ID foreign key may reference the Customer_ID
primary key in the Customers table, establishing a relationship between customers and their
orders.

27. What is relational algebra, and why is it important in relational databases? (5 marks)

Relational algebra is a formal query language used to manipulate and retrieve data from relational
databases. It consists of a set of operations that work on relations (tables) to produce new relations
as results. It is the theoretical foundation for SQL and helps in understanding and formulating
queries.

Importance in relational databases:

• It provides a set of operations (such as selection, projection, and join) to manipulate data.

• It enables the optimization of database queries by helping define the most efficient ways to
retrieve data.

• It serves as the basis for querying relational databases in SQL.


28. List the fundamental operators in relational algebra. (5 marks)

The fundamental operators in relational algebra are:

1. Selection (σ): Retrieves rows from a relation that satisfy a specified condition.

2. Projection (π): Retrieves specified columns from a relation.

3. Union (∪): Combines the rows of two relations, eliminating duplicates.

4. Set Difference (−): Returns rows that are in one relation but not in another.

5. Cartesian Product (×): Combines every row of one relation with every row of another
relation, creating a new relation.

6. Rename (ρ): Renames the attributes of a relation.

29. Explain the following relational algebra operations with examples:

• Selection (σ): Filters rows based on a condition.

o Example: σ(Age > 18)(Students) returns all students who are older than 18.

• Projection (π): Extracts specific columns from a relation.

o Example: π(Name, Age)(Students) returns only the Name and Age columns from the
Students table.

• Union (∪): Combines two relations, removing duplicates.

o Example: Students ∪ Graduates combines the rows from both tables without
duplicates.

• Set Difference (−): Returns rows from the first relation that are not in the second relation.

o Example: Students − Graduates returns students who are not yet graduated.

• Cartesian Product (×): Combines every row from one relation with every row from another.

o Example: Students × Courses results in a relation where each student is paired with
every course.

• Rename (ρ): Renames the attributes of a relation.

o Example: ρ(S, Students) renames the Students relation to S.

30. What are join operations in relational algebra? Explain the types of joins (natural join, inner
join, outer join) with examples. (5 marks)

Join operations combine rows from two relations based on a related column.

• Natural Join: Combines rows from two relations based on common columns, eliminating
duplicate columns.

o Example: Students ⨝ Courses joins Students and Courses based on common


attributes (e.g., Student_ID).

• Inner Join: Combines rows from two relations where there is a match in both relations.
o Example: Students ⨝ (Students.Course_ID = Courses.Course_ID) Courses returns
students who are enrolled in courses.

• Outer Join: Combines rows from two relations and includes rows from one relation even if
there is no match in the other relation

. Types of outer joins:

• Left Outer Join: Includes all rows from the left relation.

• Right Outer Join: Includes all rows from the right relation.

• Full Outer Join: Includes rows from both relations, even if there's no match.

31. What is an entity in the context of an ERD? Provide examples of entities in a university
database. (5 marks)

An entity in an Entity-Relationship Diagram (ERD) represents a real-world object or concept that has
a distinct existence and is relevant to the database. Entities have attributes and relationships with
other entities.

Examples in a university database:

• Student: Represents a student in the university.

• Course: Represents a course offered at the university.

• Professor: Represents a professor who teaches courses.

32. What are attributes? Describe the different types of attributes (simple, composite, derived,
multi-valued) with examples. (5 marks)

Attributes are properties or characteristics of an entity that are stored in a database.

• Simple Attribute: A single, indivisible attribute.

o Example: Student_ID is a simple attribute of the Student entity.

• Composite Attribute: An attribute composed of multiple sub-attributes.

o Example: Address can be a composite attribute with sub-attributes like Street, City,
and ZipCode.

• Derived Attribute: An attribute whose value can be derived from other attributes.

o Example: Age can be derived from the DOB attribute.

• Multi-valued Attribute: An attribute that can have multiple values.

o Example: Phone Numbers can have multiple entries for a single Student.

33. What are mapping cardinalities in ERDs? Why are they important? (3 marks)

Mapping cardinalities define the number of instances of one entity that can or must be associated
with each instance of another entity in a relationship. Cardinalities are essential for determining the
types of relationships between entities.

Types:
• One-to-One (1:1)

• One-to-Many (1:N)

• Many-to-One (N:1)

• Many-to-Many (N:N)

Importance:

• They help design and enforce the appropriate relationships and constraints in the database.

• They ensure that relationships are represented correctly in the ER diagram and the resulting
database schema.

34. Explain the four types of cardinality constraints (one-to-one, one-to-many, many-to-one, many-
to-many) with examples. (4 marks)

1. One-to-One (1:1): A single record in one table is related to a single record in another table.

o Example: Each Student has a unique Student_ID and can have only one
Student_Profile.

2. One-to-Many (1:N): A single record in one table can be related to multiple records in
another table.

o Example: A Professor can teach many Courses.

3. Many-to-One (N:1): Multiple records in one table are related to a single record in another
table.

o Example: Many Students can belong to one Department.

4. Many-to-Many (N:N): Multiple records in one table are related to multiple records in
another table.

o Example: Students can enroll in multiple Courses, and each Course can have many
Students.

35. Explain Extended E-R Features. (5 marks)

Extended Entity-Relationship (EER) features extend the basic ER model to support additional
concepts like specialization, generalization, and aggregation, offering more detail in the modeling
process.

• Specialization: The process of dividing a higher-level entity into lower-level entities based on
distinguishing characteristics.

o Example: A Person entity can be specialized into Employee and Student entities.

• Generalization: The process of combining multiple lower-level entities into a higher-level


entity.

o Example: Employee and Customer entities can be generalized into a Person entity.

• Aggregation: A higher-level abstraction that allows treating relationships between entities as


a higher-level entity.
o Example: A relationship between Employee and Department can be aggregated into
a WorkIn entity.

36. Explain the difference between a primary key and a foreign key in an ER diagram. (3 marks)

• Primary Key: A unique identifier for a record in an entity. It ensures that each record is
unique.

o Example: Student_ID in the Student entity.

• Foreign Key: A key used to establish a relationship between two entities. It is a primary key in
another entity.

o Example: Student_ID in the Enrollment entity that references Student_ID in the


Student entity.

37. What is the difference between a strong entity and a weak entity? (3 marks)

• Strong Entity: An entity that can exist independently and has a primary key that uniquely
identifies it.

o Example: Student is a strong entity because it can be uniquely identified by


Student_ID.

• Weak Entity: An entity that depends on a strong entity for identification and does not have a
unique primary key. It is identified by a combination of its attributes and the primary key of
the strong entity.

o Example: Dependent is a weak entity, identified by Dependent_ID and Employee_ID


(the strong entity).

Unit 3: Relational Database Design and Transaction Management - Answers

38. What are the different types of anomalies in a relational database? (4 marks)

In a relational database, anomalies refer to inconsistencies or problems that arise during data
manipulation (insertion, updating, or deletion) due to poor database design. The main types of
anomalies are:

1. Insertion Anomaly: Occurs when new data cannot be inserted into a table due to missing
information for other attributes. For example, if you can't add a new student because you
don't have a corresponding course for them.

o Example: A Student table with a Course column where new students cannot be
added without assigning them to a course.

2. Update Anomaly: Occurs when updating data in one place requires making multiple updates
in other places to maintain consistency. This can lead to redundancy and inconsistencies.

o Example: If a student's address is stored in multiple tables, updating it in one table


without updating others leads to inconsistency.

3. Deletion Anomaly: Happens when deleting data inadvertently causes the loss of other
important data. This often occurs when related data is stored together in a single table.
o Example: If you delete a student from the Student-Course table, you may lose
information about the course even though the course is still relevant to other
students.

39. What is a Functional Dependency (FD)? (1 mark)

A Functional Dependency (FD) is a relationship between two attributes (or sets of attributes) in a
relation. It states that if two tuples (rows) of a relation have the same value for a set of attributes,
they must also have the same value for another attribute.

Notation: If A → B, it means that attribute A functionally determines attribute B, or for any two rows
in a relation, if the values of A are the same, then the values of B must also be the same.

40. What is the purpose of Normalization in a database? Explain 1NF, 2NF, and 3NF with an
example. (5 marks)

Normalization is the process of organizing data in a relational database to reduce redundancy and
avoid undesirable anomalies. It involves dividing large tables into smaller, manageable ones while
ensuring the relationships between them are properly maintained.

• 1NF (First Normal Form): A table is in 1NF if all columns contain atomic values (i.e., no
repeating groups or arrays) and each record has a unique identifier (primary key).

o Example: If a Student table has multiple courses in a single column (e.g., "Math,
Science"), it violates 1NF. To bring it to 1NF, we split the column into separate rows
for each course.

o Table in 1NF:

o Student_ID | Course

o 1 | Math

o 1 | Science

• 2NF (Second Normal Form): A table is in 2NF if it is in 1NF and every non-key attribute is fully
dependent on the primary key (i.e., there is no partial dependency).

o Example: In a table where Student_ID and Course_ID together form the primary key,
if a student's name is stored in the table, it only depends on Student_ID, not both
keys. To bring it to 2NF, the Student_Name should be moved to a separate table.

o Table in 2NF:

o Student_ID | Course_ID

o 1 | Math

o 1 | Science

• 3NF (Third Normal Form): A table is in 3NF if it is in 2NF and all non-key attributes are not
transitively dependent on the primary key.

o Example: In a table where Student_ID determines Department, and Department


determines Department_Head, we can remove the transitive dependency by
creating a separate table for Department.
o Table in 3NF:

o Student_ID | Course_ID

o 1 | Math

o 1 | Science

o Department | Department_Head

o Science | Dr. Smith

41. What are the ACID properties of a transaction? (4 marks)

The ACID properties ensure that database transactions are processed reliably. They stand for:

1. Atomicity: A transaction is an indivisible unit. It is either fully completed or fully rolled back,
with no partial operations.

o Example: If a money transfer involves two steps (debit from one account and credit
to another), both must be completed, or neither should occur.

2. Consistency: A transaction brings the database from one valid state to another, maintaining
the database's integrity constraints.

o Example: If a transaction violates any integrity constraints (like a foreign key


constraint), it will be rolled back.

3. Isolation: Transactions are isolated from one another, ensuring that the intermediate states
of a transaction are not visible to other transactions.

o Example: If two users simultaneously transfer money, their operations should not
interfere with each other.

4. Durability: Once a transaction is committed, its effects are permanent, even in the case of a
system crash.

o Example: After completing a transaction to update an account balance, the change


should be permanent, even if the system crashes afterward.

42. What is the Two-Phase Commit Protocol (2PC)? (3 marks)

The Two-Phase Commit Protocol (2PC) is a transaction management protocol used to ensure
atomicity in distributed database systems. It coordinates the commitment of a transaction across
multiple participating databases (or nodes).

• Phase 1 (Prepare): The coordinator sends a "prepare" message to all participant nodes. Each
participant must reply with either "commit" or "abort" based on whether they can
successfully execute the transaction.

• Phase 2 (Commit or Abort): If all participants reply with "commit", the coordinator sends a
"commit" message to all participants, making the transaction permanent. If any participant
replies with "abort", the coordinator sends an "abort" message to all participants, and the
transaction is rolled back.

The two phases ensure that all participants in a distributed system either commit or abort the
transaction together, maintaining consistency.
43. What is Deadlock in database transactions? (3 marks)

Deadlock occurs when two or more transactions are waiting for each other to release resources (e.g.,
locks on data) in such a way that neither transaction can proceed. Each transaction holds a lock on
one resource and waits for a lock on another resource held by another transaction, creating a cycle
of waiting.

• Example: Transaction A holds a lock on Resource 1 and waits for Resource 2, while
Transaction B holds a lock on Resource 2 and waits for Resource 1. Both transactions are
stuck in a deadlock situation.

Deadlock handling techniques:

• Timeouts: Transactions are aborted if they wait too long for a resource.

• Resource Preemption: Resources are taken from one transaction and given to another to
break the deadlock.

• Detection and Recovery: The system periodically checks for deadlocks and rolls back one or
more transactions involved in the deadlock.

Unit 4: Data Manipulation Commands - Answers

44. Consider the relational database given below. Give an expression in the relational algebra to
express each of the following queries:

• Employee (person-name, street, city)

• Works (person-name, company-name, salary)

• Company (company-name, city)

• Manages (person-name, manager-name)

(1) Find the name of all employees.


Relational Algebra Expression:

• π_person-name(Employee)

o Explanation: The π (projection) operator selects the person-name column from the
Employee relation, giving a list of all employee names.

(2) Find the city of the employee whose name is starting with "s".
Relational Algebra Expression:

• σ_person-name LIKE 's*'(Employee)

o Explanation: The σ (selection) operator filters the Employee relation for rows where
person-name starts with "s". This query uses the LIKE condition to filter the names
based on the pattern.

(3) Find all the employees from the city "Ahmedabad".


Relational Algebra Expression:

• σ_city = 'Ahmedabad'(Employee)
o Explanation: The σ (selection) operator filters the Employee relation for rows where
the city is equal to "Ahmedabad", retrieving employees who are from that city.

45. Draw E-R diagram for the student management system. (5 marks)

While I can't draw an image directly here, I can describe how to structure the E-R diagram for a
Student Management System:

1. Entities:

o Student: Attributes: Student_ID (Primary Key), Name, Date_of_Birth, Email, Phone.

o Course: Attributes: Course_ID (Primary Key), Course_Name, Instructor, Credits.

o Department: Attributes: Department_ID (Primary Key), Department_Name.

o Instructor: Attributes: Instructor_ID (Primary Key), Name, Email.

o Enrollment: (This is a junction entity between Student and Course). Attributes:


Student_ID, Course_ID, Grade.

2. Relationships:

o Enrolls: Between Student and Course. A student can enroll in multiple courses, and
each course can have many students.

▪ Cardinality: Many-to-Many.

o Belongs_to: Between Student and Department. A student belongs to one


department.

▪ Cardinality: Many-to-One.

o Teaches: Between Instructor and Course. An instructor teaches many courses.

▪ Cardinality: One-to-Many.

3. E-R Diagram Structure:

o Draw rectangles for entities (Student, Course, Instructor, Department, Enrollment).

o Use diamonds to represent relationships (Enrolls, Belongs_to, Teaches).

o Connect entities to relationships with lines, and use crow's feet to indicate the
cardinality (many, one).

46. Write a SELECT query to fetch all employees with salaries greater than 50,000 and located in
'New York'. Explain the use of the condition. (3 marks)

SQL Query:

SELECT *

FROM Employee

WHERE salary > 50000 AND city = 'New York';

Explanation:
• The SELECT * statement retrieves all columns from the Employee table.

• The WHERE clause filters the results based on two conditions:

1. salary > 50000: This condition filters employees whose salary is greater than 50,000.

2. city = 'New York': This condition filters employees who are located in the city of New
York.

• The AND operator ensures that both conditions must be true for a row to be included in the
result.

47. Write a query to calculate the total price for each item in a sales table where total price =
quantity * price. Explain the use of arithmetic operators. (3 marks)

SQL Query:

SELECT item_id, quantity * price AS total_price

FROM Sales;

Explanation:

• The SELECT statement retrieves the item_id and calculates the total_price by multiplying
quantity and price.

• The arithmetic operator * is used for multiplication.

• The AS total_price part gives the calculated result an alias (a column name) for easier
understanding.

48. What is the difference between AND and OR operators in SQL? Write a query illustrating both.
(3 marks)

• AND Operator: The AND operator returns TRUE only if both conditions are true.

o Example Query:

o SELECT *

o FROM Employee

o WHERE salary > 50000 AND city = 'New York';

This query selects employees who earn more than 50,000 and live in New York.

• OR Operator: The OR operator returns TRUE if at least one condition is true.

o Example Query:

o SELECT *

o FROM Employee

o WHERE salary > 50000 OR city = 'New York';

This query selects employees who either earn more than 50,000 or live in New York (or both).

49. Explain how to add a primary key to an existing table. Provide an example command. (3 marks)
SQL Command:

ALTER TABLE Employee

ADD CONSTRAINT pk_employee PRIMARY KEY (Employee_ID);

Explanation:

• The ALTER TABLE statement is used to modify an existing table.

• The ADD CONSTRAINT clause adds a new constraint to the table.

• pk_employee is the name given to the primary key constraint.

• (Employee_ID) specifies the column that will act as the primary key.

50. Write the SQL command to add a foreign key constraint to a column department_id in the
employees table referencing the departments table. (4 marks)

SQL Command:

ALTER TABLE Employees

ADD CONSTRAINT fk_department

FOREIGN KEY (department_id) REFERENCES Departments(department_id);

Explanation:

• The ALTER TABLE statement modifies the Employees table.

• The ADD CONSTRAINT clause adds a foreign key constraint.

• fk_department is the name of the foreign key constraint.

• FOREIGN KEY (department_id) specifies that department_id in the Employees table is the
foreign key.

• REFERENCES Departments(department_id) defines that the foreign key references the


department_id column in the Departments table.

You might also like