Module 2 dbms Q&A
Module 2 dbms Q&A
Relational Model is a way to organize data into tables (relations) consisting of rows (tuples) and
columns (attributes).
Key Components:
• Relation (Table):
A set of tuples sharing the same attributes.
Example: Students(StudentID, Name, Age)
• Tuple (Row):
A single entry in a table.
Example: (101, "John", 21)
• Attribute (Column):
A property/characteristic of a relation.
Example: Name, Age.
• Domain:
Set of allowed values for an attribute.
Example: Domain of "Age" is integers between 0–100.
• Schema:
Structure of a table.
Example: Students(StudentID: int, Name: varchar, Age: int)
Types:
• Primary Key:
Unique and non-null.
Example: StudentID in Students table.
• Candidate Key:
All possible unique keys.
Example: StudentID and Email (if both are unique).
• Super Key:
A set of attributes that uniquely identify a tuple.
Example: {StudentID, Name} is a Super Key.
• Foreign Key:
Links one relation to another.
Example: StudentID in Enrollment table referencing Students table.
• Alternate Key:
Candidate keys other than the primary key.
Types:
• Domain Constraint:
Attribute values must be from a valid domain.
Example: Age must be a number, not text.
• Entity Integrity:
Primary key cannot be NULL.
• Referential Integrity:
Foreign key must match primary key in another table.
• Key Constraint:
No two tuples can have the same value for a key attribute.
Fundamental Operations:
Additional Operations:
• Intersection (∩)
• Division (÷)
How it Works Specifies "how" to retrieve data Specifies "what" data to retrieve
• Outer Joins:
• Foundation of SQL.
Given Schema:
Students(StudentID, Name, Age, Department)
Courses(CourseID, CourseName, Credits)
Enrollment(StudentID, CourseID, Grade)
Approach:
9. Relational Algebra Query: Employees in HR and Salary > $50,000 [10 Marks]
Given Schema:
Employee(EmpID, Name, Department, Salary)
Department(DeptID, DeptName, ManagerID)
WorksIn(EmpID, DeptID)
Approach:
10. Relational Algebra Query: Students who borrowed books by J.K. Rowling [10 Marks]
Given Schema:
Books(BookID, Title, Author, Publisher)
Borrowed(BookID, StudentID, DueDate)
Students(StudentID, Name, Age)
Approach:
Given Schema:
Customers(CustomerID, Name, Age, City)
Orders(OrderID, CustomerID, ProductID, OrderDate)
Products(ProductID, ProductName, Price, Category)
Approach:
12. Relational Algebra Query: Doctors Treating Diabetes Patients [10 Marks]
Given Schema:
Patients(PatientID, Name, Age, Disease)
Doctors(DoctorID, Name, Specialty)
Treats(DoctorID, PatientID)
Approach:
Relational Calculus = Non-procedural query language that defines what to retrieve, not how.
Procedural Yes No
Tuple Relational Calculus (TRC) expresses queries by describing the properties of desired tuples.
Syntax:
{ t | P(t) }
where t = tuple variable, P(t) = condition.
Example:
Find Students older than 20:
{ t | t ∈ Students ∧ t.Age > 20 }
Query:
π Name, Age (σ Age > 20 (STUDENT))
Query:
π E_Name (σ Designation = 'HR' ∧ Salary > 40000 (Employee))