Answerz
Answerz
Question 8:
a. Designing a database schema for a library management system:
Here's a possible schema design:
Tables:
● Books:
○ book_id (Primary Key)
○ title
○ author
○ ISBN
○ publisher
○ publication_year
○ genre
○ availability (boolean)
● Members:
○ member_id (Primary Key)
○ name
○ address
○ phone
○ email
○ membership_type (e.g., student, faculty, staff)
● Loans:
○ loan_id (Primary Key)
○ book_id (Foreign Key referencing Books)
○ member_id (Foreign Key referencing Members)
○ date_borrowed
○ due_date
○ date_returned (nullable)
● Penalties:
○ penalty_id (Primary Key)
○ loan_id (Foreign Key referencing Loans)
○ amount
○ date_paid (nullable)
b. Calculating overdue penalties for members:
SELECT members.name, SUM(DATEDIFF(DAY, loans.due_date, GETDATE()) *
0.5) AS penalty_amount
FROM members
JOIN loans ON members.member_id = loans.member_id
WHERE loans.date_returned IS NULL AND loans.due_date < GETDATE()
GROUP BY members.name;
This query calculates the penalty amount for each member with overdue books by multiplying
the number of overdue days by a penalty rate of 0.5 per day.
c. Reporting borrowed books grouped by member names:
SELECT members.name, books.title
FROM members
JOIN loans ON members.member_id = loans.member_id
JOIN books ON loans.book_id = books.book_id
WHERE loans.date_returned IS NULL
ORDER BY members.name;
This query retrieves the names of members and the titles of the books they have borrowed but
not yet returned.
Question 9a:
Converting ER diagram with weak and strong entities into a relational schema:
1. Represent strong entities as tables: Each strong entity becomes a table with attributes
corresponding to the entity's attributes.
2. Represent weak entities as tables: Weak entities become tables with attributes
corresponding to the weak entity's attributes, plus a foreign key referencing the identifying
strong entity.
3. Create relationships: Relationships between entities are represented by foreign keys in
the appropriate tables.
Potential issues:
● Loss of information: If the weak entity's identifying attribute is not unique, information
may be lost when converting to a relational schema.
● Data redundancy: If the weak entity is highly dependent on the strong entity, data
redundancy may occur.
Question 9b:
Differentiating between 2NF, 3NF, and BCNF:
● 2NF: A relation is in 2NF if it is in 1NF and every non-prime attribute is fully functionally
dependent on the primary key.
● 3NF: A relation is in 3NF if it is in 2NF and no non-prime attribute is transitively dependent
on the primary key.
● BCNF: A relation is in BCNF if it is in 3NF and every determinant is a candidate key.
Example:
Consider the relation Student(RollNo, Name, Course, Marks).
● Not in 2NF: Marks is functionally dependent on Course, not the primary key RollNo.
● Not in 3NF: Marks is transitively dependent on RollNo through Course.
● Not in BCNF: Course is a determinant but not a candidate key.
Question 9c:
SQL queries for Student schema:
a. Retrieve the names of students who scored the highest marks in each course:
SELECT Name, Course
FROM Student
WHERE Marks IN (SELECT MAX(Marks) FROM Student GROUP BY Course);
Question 10a:
Query tree and construction:
A query tree is a visual representation of the execution plan for an SQL query. It shows the
order in which operations are performed, such as selection, projection, and join.
To construct a query tree, analyze the query and break it down into its constituent operations.
Then, represent each operation as a node in the tree, with the root node being the final result.
Example:
For the query SELECT Name, Course FROM Student WHERE Marks > 80, the query tree might
look like this:
Select Name, Course
|
|
|
Filter Marks > 80
|
|
|
Student Table
Question 10b:
View and its advantages:
A view is a virtual table that is created from one or more base tables. It does not store any data
itself but rather presents a customized view of the data in the base tables.
Advantages of views:
● Data abstraction: Views can hide the complexity of the underlying data structure.
● Data security: Views can be used to restrict access to certain data.
● Data consistency: Views can be used to ensure that data is consistent across multiple
applications.
Example:
CREATE VIEW HighScoringStudents AS
SELECT Name, Course, Marks
FROM Student
WHERE Marks > 80;
This view creates a virtual table named HighScoringStudents that contains the names, courses,
and marks of students who scored above 80.
Question 11a:
Definitions of 1NF and 2NF:
● 1NF: A relation is in 1NF if all its attributes are atomic (not divisible) and there are no
repeating groups.
● 2NF: A relation is in 2NF if it is in 1NF and every non-prime attribute is fully functionally
dependent on the primary key.
If you have any further questions, feel free to ask!
● https://github.com/mochipeachh/ThreeMusketeersLibrary
● https://www.numerade.com/questions/consider-the-following-relation-car_salecar-date_so
ld-salesman-commission-discount_amt-assume-that-a/