Question Bank With Answers Module 2
Question Bank With Answers Module 2
Question Bank With Answers Module 2
PART B
Consider the following relations for a database that keeps track of business trips of
salespersons in a sales office: SALESPERSON(Ssn, Name, StartYear, DeptNo)
TRIP(Ssn, FromCity, ToCity, DepartureDate, ReturnDate, TripId)
EXPENSE(TripId, AccountNo, Amount)
i) A trip can be charged to one or more accounts. Specify the foreign keys for this
schema, stating any assumptions you make.
ii) Write relation algebra expression to get the details of salespersons who have
travelled between Mumbai and Delhi and the travel expense is greater that Rs. 50000.
iii) Write relation algebra expression to get the details of salesperson who had incurred
the greatest travel expenses among all travels made.
iv) List the salespersons who have not been on any trips.
v) Find the total number of trips made by each salesperson.
Answer
The foreign keys in this schema are designed to connect related data across the tables:
● In the TRIP table, Ssn is a foreign key that references Ssn in the SALESPERSON table.
This creates a link between a trip and the salesperson who made it.
● In the EXPENSE table, TripId is a foreign key that references TripId in the TRIP table.
This links each expense to a specific trip.
iii) To find the salesperson who has incurred the greatest travel expenses among all travels
made, we first need to sum the expenses by salesperson and then find the maximum. This can
be complex in pure relational algebra without extensions for aggregation, but conceptually,
the steps involve:
iv) To list the salespersons who have not been on any trips, you can use a set difference
operation:
AGG_COUNT(TRIP; Ssn) -> COUNT_TRIPS
ANSWER:-
1.
Entity Integrity Constraint: This ensures that the primary key attribute in each relation is
not NULL.
For the employee relation: person-name should be unique and not NULL.
For the works relation: Both person-name and company-name together should be
unique and not NULL.
For the company relation: company-name should be unique and not NULL.
For the manages relation: Both person-name and manager-name together should be
unique and not NULL.
Referential Integrity Constraint: This ensures that foreign key values in a relation reference
an existing tuple in another relation.
In the works relation, person-name should exist in the employee relation, and
company-name should exist in the company relation.
In the manages relation, both person-name and manager-name should exist in the
employee relation.
Attribute Domain Constraint: This ensures that each attribute value falls within a
specified domain.
Assuming that all attributes (person-name, street, city, company-name, salary,
manager-name) are strings, this constraint ensures that no attribute value exceeds a certain
length and follows a specific format. For instance, salary should be numerical.
Explain the SELECT and PROJECT operations in relational algebra with examples.
SELECT Operation:
● The SELECT operation is used to retrieve tuples from a relation that satisfy a
specified condition.
● It filters rows based on a given predicate.
● The resulting relation contains only the tuples that meet the specified criteria.
Syntax: σ<condition>(relation)
Example: Consider a relation named employee with attributes (employee_id, name, age,
department). Let's say we want to retrieve the names of employees who belong to the "Sales"
department.
If employee relation looks like this:
EMPLOYEE ID NAME AGE DEPARTMENT
1. ALICE 30 SALES
2. BOB 35 MARKETING
3. CHARLIE 28 SALES
4. DAVID 40 HR
The SELECT operation to retrieve the names of employees in the "Sales" department
σ(department = 'Sales')(employee)
PROJECT Operation:
● The PROJECT operation is used to select a subset of attributes from a relation.
● It returns a relation with only the specified attributes, eliminating duplicates.
Syntax: π<attribute list>(relation)
Example: Continuing with the employee relation example, let's say we want to retrieve only
the names and ages of employees.
The PROJECT operation to retrieve names and ages would be:
π(name, age)(employee)
NAME AGE
ALICE 30
BOB 35
CHARLIE 28
DAVID 40
Study the tables given below and write relational algebra expressions for the queries that
follow. STUDENT(ROLLNO, NAME, AGE, GENDER, ADDRESS, ADVISOR)
COURSE(COURSEID, CNAME, CREDITS) PROFESSOR(PROFID,PNAME, PHONE)
ENROLLMENT(ROLLNO, COURSEID, GRADE) Primary keys are underlined. ADVISOR
is a foreign key referring to PROFESSOR table. ROLLNO and COURSEID in
ENROLLMENT are also foreign keys referring to the primary keys with the same name.
(i) Names of female students.
(ii) Names of male students along with adviser name.
(iii) Roll Number and name of students who have not enrolled for any course.
iv) For each course, the number of students enrolled in the course.
STUDENT(ROLLNO, NAME, AGE, GENDER, ADDRESS, ADVISOR)
π_pname((σ_sid=105(Catalog)) ⨝ Parts)
ii) Find the names of suppliers supplying some green part for less than Rs 1000:
iii) Find the IDs of suppliers who supply some red or green part:
iv) Find the names of suppliers who supply some red part:
i. For each department, find its name and the name, sex and phone number of the head
of the department.
iii. Find those students who have registered for all courses offered in the department of
Computer Science.
iv. Obtain the department Ids for departments with no lady professor.
v. Obtain the rollNo of girl students who have obtained at least one S grade
i. For each department, find its name and the name, sex, and phone number of the head of the
department:
iii. Find those students who have registered for all courses offered in the department of
Computer Science:
iv. Obtain the department Ids for departments with no lady professor:
π_rollNo(σ_sex='female'(STUDENT) ⨝ σ_grade='S'(ENROLLMENT))
The relational schema for a library describing members, books and issue information is given
below. Foreign keys have the same name as primary keys.
BOOKS(ACC-NO,ISBN, TITLE, EDITION, YEAR)
MEMBERS(MEMBERID, MEMBERNAME, MEMBERTYPE)
ISSUEDTO(ACC-NO,MEMBERID, DATE OF ISSUE)
Write relational algebra expressions for the following queries:-
i) Accession Number(s) and Name(s) of third edition books published in 2018.
ii) Names and date of issue of books taken by a member with name ‘PRIYA’.
iii) Names of books not taken by any member.
iv)List the names and types of members who have borrowed books.
v) Find the names of members who have not borrowed any books.
BOOKS(ACC-NO, ISBN, TITLE, EDITION, YEAR)
MEMBERS(MEMBERID, MEMBERNAME, MEMBERTYPE)
ISSUEDTO(ACC-NO, MEMBERID, DATE OF ISSUE)
i) Accession Number(s) and Name(s) of third edition books published in 2018:
ii) Names and date of issue of books taken by a member with name 'PRIYA':
iv) List the names and types of members who have borrowed books:
vi) Find the names of members who have not borrowed any books:
i) Create table Room, Guest, Reservation including primary and foreign key integrity
constraints:
CREATE TABLE Room ( room_number INT PRIMARY KEY, type VARCHAR(50), price
DECIMAL(10, 2), availability BOOLEAN ); -- Create Guest table CREATE TABLE Guest (
guest_id INT PRIMARY KEY,name VARCHAR(100), contact VARCHAR(20), nationality
VARCHAR(50) ); -- Create Reservation table CREATE TABLE Reservation ( reservation_id
INT PRIMARY KEY, guest_id INT, room_number INT, check_in_date DATE,
check_out_date DATE, FOREIGN KEY (guest_id) REFERENCES Guest(guest_id),
FOREIGN KEY (room_number) REFERENCES Room(room_number) );
ii) Insert values into both tables. Insertions should be carried out in the following order:
Guest, Room, Reservation.
INSERT INTO Guest (guest_id, name, contact, nationality) VALUES (1, 'John Doe',
'1234567890', 'USA'), (2, 'Jane Smith', '9876543210', 'UK'), (3, 'Alice Brown', '4567890123',
'Canada');
INSERT INTO Room (room_number, type, price, availability) VALUES (101, 'Standard',
100.00, TRUE), (102, 'Suite', 150.00, TRUE), (103, 'Standard', 110.00, FALSE); -- Insert
values into Reservation table INSERT INTO Reservation (reservation_id, guest_id,
room_number, check_in_date, check_out_date) VALUES (1, 1, 101, '2024-03-17',
'2024-03-20'), (2, 2, 102, '2024-04-01', '2024-04-05'), (3, 3, 103, '2024-05-10', '2024-05-15');
iv) Increase the room prices by 15% for rooms of type "Suite":
CREATE TABLE Company ( comp_id INT PRIMARY KEY, cname VARCHAR(100) NOT
NULL, address VARCHAR(255), turnover DECIMAL(15, 2) ); -- Create Employee table
CREATE TABLE Employee ( eid INT PRIMARY KEY, name VARCHAR(100) NOT
NULL, designation VARCHAR(50), salary DECIMAL(10, 2) NOT NULL, comp_id INT,
years_of_exp INT, FOREIGN KEY (comp_id) REFERENCES Company(comp_id) );
ii) Insert values into both tables. Insertions should be carried out in the following order:
Company, Employee.
-- Insert values into Company table INSERT INTO Company (comp_id, cname, address,
turnover) VALUES (1, 'ABC Corp', '123 Main St', 1000000.00), (2, 'XYZ Inc', '456 Elm St',
2000000.00), (3, 'LMN Ltd', '789 Oak St', 1500000.00); -- Insert values into Employee table
INSERT INTO Employee (eid, name, designation, salary, comp_id, years_of_exp) VALUES
(101, 'John Doe', 'Manager', 30000.00, 1, 5), (102, 'Jane Smith', 'Developer', 25000.00, 2, 3),
(103, 'Alice Brown', 'Analyst', 28000.00, 3, 4);
iii) Modify the table Employee to include a new column "years_of_exp":
ALTER TABLE Employee ADD COLUMN years_of_exp INT;
iv) Increment the salary of employees whose salary is less than Rs25000 by 5%:
UPDATE Employee SET salary = salary * 1.05 WHERE salary < 25000.00;