database engineering
database engineering
Group – A
Q1. How do schema and instances are related to a database?
Ans. In a database, schema and instances are two fundamental concepts that describe its
structure and content.
1. Schema (Structure)
This schema defines what the students table should look like.
2. Instance (Data)
This is one instance of the database. If another student is added, the instance changes.
Think of a schema as a Mold and instances as the different batches of items produced using
that Mold.
Q2. Explain data definition language (DDL) and data manipulation language
(DML).
Ans. Data Definition Language (DDL) vs. Data Manipulation Language
(DML)
Both DDL and DML are subsets of SQL (Structured Query Language) used for interacting
with databases, but they serve different purposes.
Command Description
CREATE Creates a new table, database, index, or view.
ALTER Modifies an existing database object (e.g., adding a column).
DROP Deletes an entire table, database, or other objects.
TRUNCATE Removes all records from a table but keeps the structure.
RENAME Changes the name of an existing object.
Example of DDL:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Salary DECIMAL(10,2)
);
Deals with data manipulation (CRUD operations: Create, Read, Update, Delete).
Changes made using DML can be rolled back (not auto-committed).
Used for querying and modifying existing records.
Command Description
SELECT Retrieves data from one or more tables.
INSERT Adds new records to a table.
UPDATE Modifies existing records in a table.
DELETE Removes specific records from a table.
Example of DML:
INSERT INTO Employees (EmployeeID, Name, Age, Salary)
VALUES (1, 'Alice', 30, 50000.00);
This command inserts a new employee record into the Employees table.
Analogy:
1. Specialization
o Definition: The process of creating subclasses from a general entity
(superclass).
o Use Case: Helps model hierarchical relationships where some entities have
additional attributes.
o Example:
A "Person" entity can be specialized into "Student" and
"Professor", where:
"Student" has attributes like StudentID, Major.
"Professor" has attributes like EmployeeID, Department.
2. Generalization
o Definition: The reverse of specialization; multiple entities are merged into a
single higher-level entity.
o Use Case: Simplifies the model by reducing redundancy.
o Example:
Entities "Car", "Truck", and "Motorcycle" can be generalized into
"Vehicle", which has common attributes like Model, Manufacturer.
3. Aggregation
o Definition: A higher-level abstraction where a relationship itself is treated as
an entity.
o Use Case: Useful when a relationship needs to participate in another
relationship.
o Example:
Consider entities "Employee" and "Project" with a "Works_On"
relationship.
If we want to track the "Manager" who oversees the "Works_On"
relationship, we can aggregate "Works_On" as an entity and link it to
"Manager."
4. Category (Union)
o Definition: A subclass (child entity) can have multiple superclasses (parent
entities).
o Use Case: Used when a single entity needs to inherit attributes from multiple
sources.
o Example:
An entity "PartTimeProfessor" can be a subclass of both
"Professor" and "Employee", inheriting attributes from both.
If you're familiar with ER diagrams, an EER diagram looks similar but includes inheritance
(ISA relationships), aggregation, and generalization.
Person
/ \
Student Professor
(Has ID) (Has Salary)
This shows specialization, where "Person" is a superclass, and "Student" and "Professor"
are specialized subclasses.
Use Cases of the EER Model
Q4. What are the key constrains and domain constrains in the relational
database?
Ans. Key Constraints and Domain Constraints in a Relational Database
In a relational database, constraints ensure data integrity and consistency. Two important
types of constraints are Key Constraints and Domain Constraints.
1. Key Constraints
Key constraints ensure that each row (tuple) in a table can be uniquely identified. They help
maintain uniqueness and entity integrity in a database.
Here, StudentID is the Primary Key, ensuring each student has a unique ID.
4. Candidate Key
o A set of potential Primary Keys. Only one is chosen as the actual Primary
Key.
o Example:
A Students table might have both StudentID and Email as unique
values. Both are Candidate Keys, but only one can be the Primary
Key.
5. Super Key
o A super set of a Candidate Key.
o It may contain extra attributes that are not necessary for uniqueness.
o Example: {StudentID, Email, Name} is a Super Key, but {StudentID}
alone is a Candidate Key.
2. Domain Constraints
Domain constraints ensure that a column's values belong to a specific range, data type, or
predefined set of values.
2. Check Constraint
o Ensures values meet a specific condition.
o Example:
o CREATE TABLE Students (
o StudentID INT PRIMARY KEY,
o Age INT CHECK (Age >= 18)
o );
3. Default Constraint
o Provides a default value when no value is provided.
o Example:
o CREATE TABLE Orders (
o OrderID INT PRIMARY KEY,
o Status VARCHAR(20) DEFAULT 'Pending'
o );
Summary Table:
Constraint Type Purpose
Primary Key Uniquely identifies each row. Cannot be NULL.
Unique Key Ensures column values are unique but can be NULL.
Foreign Key Maintains referential integrity between tables.
Candidate Key A set of attributes that can be a Primary Key.
Super Key A Candidate Key + extra attributes.
Data Type Constraint Ensures values match a specific data type (e.g., INT, VARCHAR).
Check Constraint Ensures values meet a condition (e.g., Age > 18).
Default Constraint Sets a default value when no value is provided.
Not Null Constraint Ensures a column cannot have NULL values.
Enum Constraint Restricts values to a predefined set.
Real-World Example
These constraints ensure valid, consistent, and reliable data in the database.
1. Improving Readability
o Shortening long table or column names for easier understanding.
o Example: Renaming EmployeeSalaryDetails to Salaries.
2. Avoiding Naming Conflicts
o When joining multiple tables with similar column names, renaming helps
avoid ambiguity.
o Example: If both Students and Teachers tables have a column Name,
renaming helps differentiate them.
3. Simplifying Complex Queries
o Assigning shorter aliases makes queries more readable, especially in joins and
subqueries.
4. Enhancing Data Presentation
o When generating reports or views, renaming columns provides user-friendly
output.
5. Modifying Database Structure
o Permanently renaming tables or columns when business requirements change.
2. Types of Renaming
Here, EmployeeID, Name, and Salary are renamed only in the output.
Scenario:
Conclusion
Renaming is a powerful feature in relational databases that helps improve clarity, avoid
conflicts, and manage database structure changes. Whether temporary (using aliases) or
permanent (using RENAME or ALTER), it plays a vital role in making queries more readable and
databases more maintainable.
The division operator (÷) in relational algebra is used for queries that involve "for all"
conditions. It helps find entities that are related to all items in another set.
It is used when:
Mathematical Representation:
X contains only those values from A that have a corresponding entry for every value
in B.
Query Objective:
Division Logic:
Given Tables:
✅ StudentCourses (A)
StudentID CourseID
1 Math
1 Science
2 Math
2 Science
2 English
3 Math
✅ AllCourses (B)
CourseID
Math
Science
Expected Output:
Students who have taken all courses in AllCourses (Math and Science):
StudentID
1
2
SQL does not have a direct ÷ operator, but we can achieve the same effect using nested
queries.
SQL Query:
Explanation:
1. The inner NOT EXISTS query checks if there is any course in AllCourses that the
student has not taken.
2. The outer NOT EXISTS query ensures that only students who have taken every course
in AllCourses are selected.
3. DISTINCT ensures unique student IDs in the output.
5. Alternative SQL Approach Using GROUP BY and HAVING
SELECT StudentID
FROM StudentCourses
WHERE CourseID IN (SELECT CourseID FROM AllCourses)
GROUP BY StudentID
HAVING COUNT(DISTINCT CourseID) = (SELECT COUNT(*) FROM AllCourses);
Explanation:
7. Real-World Applications
8. Summary
Feature Description
Purpose Finds entities related to all values in another set
Used For Many-to-Many relationships
Example Students who completed all required courses
SQL Equivalent NOT EXISTS or HAVING COUNT(DISTINCT)
Real-World Uses Enrollment, Supplier-Parts, Employee Training
Group – B
Q1. How does a database management system (DBMS) differ from a file
management system?
Ans. Difference Between DBMS and File Management System
A Database Management System (DBMS) and a File Management System (FMS) are
both used for storing and managing data, but they differ significantly in terms of structure,
efficiency, security, and functionalities.
Each file independently holds data, requiring manual merging for retrieval.
✅ Employees Table
✅ Departments Table
DepartmentID DepartmentName
D1 HR
D2 IT
5. Conclusion
| FMS is useful for simple, personal, or small-scale storage but lacks security, integrity, and
efficiency.
| DBMS is the best choice for large, multi-user, and business-critical applications due to
data integrity, security, and performance.
Q2. What are keys in the relational model, and explain its different types?
Ans. Keys in a Relational Database
In a relational database, keys are used to identify records uniquely, enforce data
integrity, and establish relationships between tables.
1. What is a Key?
A key is an attribute (column) or a set of attributes that uniquely identifies a row (record)
in a table.
For example, in an employee’s table:
✅ SQL Example:
✅ Example:
A foreign key (FK) is a column that creates a relationship between two tables.
It references a primary key from another table.
Helps maintain referential integrity.
Customers Table
CustomerID Name
1 John
2 Sarah
Orders Table
✅ SQL Example:
A composite key consists of two or more columns that together uniquely identify a
row.
Used when a single column is not enough to ensure uniqueness.
✅ SQL Example:
An alternate key is any candidate key that was not chosen as the primary key.
✅ Example:
In the Employees Table, we had:
✅ SQL Example:
A unique key ensures that values in a column are unique, but NULL values are
allowed (unlike a Primary Key).
✅ SQL Example:
3. Summary Table
Key Type Definition Example
Uniquely identifies each row. Cannot
Primary Key EmployeeID in Employees table
be NULL.
Candidate A potential primary key (only one is
Email and EmployeeID
Key chosen).
Any column set that uniquely identifies {EmployeeID, Email}
Super Key
a row.
A key that references another table’s
Foreign Key CustomerID in Orders table
primary key.
Composite A primary key made of multiple {StudentID, CourseID} in
Key columns. Enrollment table
A candidate key not chosen as the
Alternate Key Email in Employees table
primary key.
Ensures unique values but allows
Unique Key LicensePlate in Vehicles table
NULL.
4. Conclusion
Keys ensure data integrity, uniqueness, and relationships.
Primary Keys uniquely identify records.
Foreign Keys link related tables.
Composite Keys handle complex cases.
Unique Keys enforce uniqueness but allow NULL.
Q3. What is relational calculus? And how does it differ from the relational
algebra?
Ans. Relational Calculus vs. Relational Algebra in DBMS
1. What is Relational Calculus?
This means: Select all tuples t from Students where t.Age > 20.
This means: Select names of students where Age is greater than 20.
3. What is Relational Algebra?
Relational Algebra is a procedural query language that defines how to retrieve data from
relational databases.
This means: Select all records from Students where Age > 20.
6. Summary
Group – C
Q1. What are Codd’s rule?
Ans. Codd’s 12 Rules for Relational Databases
Dr. E.F. Codd, the father of relational databases, introduced 12 rules (actually 13,
including Rule 0) to define what qualifies as a true relational database management
system (RDBMS). These rules ensure data integrity, consistency, and flexibility.
Summary
Codd’s Rules define what a true RDBMS must follow.
Most modern databases like MySQL, PostgreSQL, and Oracle follow these
principles.
SQL was designed based on these rules to ensure data consistency, security, and
flexibility.
Joins in Relational Algebra are used to combine data from two or more tables based on a
common attribute. They help retrieve related data efficiently.
✅ Notation:
R ⨝θ S
where θ is a condition.
✅ Example:
Find employees whose salary is greater than the salary of managers:
R ⨝ A=B S
✅ Example:
Find employees and their department names:
✅ Notation:
R ⋈ S
✅ Example:
Employees ⋈ Departments
4. Outer Joins
Outer Joins preserve unmatched tuples from one or both tables by filling missing values
with NULL.
✅ Notation:
R ⟕ S
✅ Example:
Employees ⟕ Departments
✅ Notation:
R ⟖ S
✅ Example:
Employees ⟖ Departments
Returns all departments, including those without employees (NULL values for
missing employees).
✅ Notation:
R ⟗ S
✅ Example:
Employees ⟗ Departments
Returns all employees and all departments, even if they don't match.
5. Semi Join (⋉ or ⋊)
Only returns matching tuples from one table, but does not include columns from
the other table.
Useful for filtering results.
R ⋉ S
✅ Right Semi Join (⋊) Notation:
R ⋊ S
✅ Example:
Find employees who belong to a department:
Employees ⋉ Departments
✅ Notation:
R ▷ S
✅ Example:
Find employees who do not belong to any department:
Employees ▷ Departments
Q3. Define relationship in DBMS and its different types with a proper diagram.
Ans. What is a Relationship in DBMS?
✅ Example:
✅ Diagram:
One entity in Table A is related to many entities in Table B, but each entity in Table
B is linked to only one entity in Table A.
✅ Example:
Teacher → Students (One teacher teaches multiple students, but each student has
only one teacher).
Department → Employees (One department has multiple employees, but each
employee belongs to one department).
✅ Diagram:
✅ Example:
Students → Courses (A student can enroll in many courses, and each course can
have many students).
Authors → Books (An author can write multiple books, and a book can have
multiple authors).
✅ Diagram:
✅ Example:
Employees → Manager (An employee reports to a manager, but the manager is also
an employee).
Family Members → Parent-Child (A person can be both a child and a parent).
✅ Diagram:
Here, the Employee table has a "Manager ID" referring to another employee.
Summary Table of Relationship Types in DBMS
Relationship
Description Example
Type
Each entity in Table A is linked to only one entity in Person →
One-to-One (1:1)
Table B. Passport
One-to-Many One entity in Table A is linked to multiple entities in Teacher →
(1:M) Table B. Students
Many-to-Many Multiple entities in Table A are linked to multiple Students →
(M:N) entities in Table B (needs a junction table). Courses
Employee →
Self-Referencing An entity relates to itself.
Manager
Conclusion