FRONT SHEET
Subject: Database Systems
Coursework title: Database Systems
Course Teacher: Tayyaba Farhat
Student Name: Syeda Kashaf Naqvi
Submission Date: October 28, 2024
Number of words used: _________ (please insert number of words)
Format
The coursework should be typed in 12-point font, lines spaced at 1½ or double
spacing.
Standard cover sheet should be attached. In addition a title page and Marking
Scheme for the coursework (which is given towards the end of this coursework)
should be attached.
By submitting this coursework, I certify this to be my own work. I understand that if
this is untrue I will have committed an assessment offence e.g. plagiarism within the
College regulations and that disciplinary proceedings may be brought against me.
Student’s signature: Date: 27th January, 2024
Database Project
Course: Database Systems
Assignment on Database Systems
Prepared for: Tayyaba Farhat
Date: 28 October 2024
Prepared by:
Syeda Kashaf Naqvi - 125
Section:
“This assignment is my own work. The sources of all quotations, both direct and indirect, have been
fully cited; All material used in the preparation of this assignment has been acknowledged. This
assignment has not been submitted for assessment in any other paper.”
Signed by:
Syeda Kashaf Naqvi - 125 28 October 2024 2
Table of Contents
Question 1:..............................................................................................................................................5
Question 2:..............................................................................................................................................5
Question 3:..............................................................................................................................................7
Question 4:..............................................................................................................................................9
Syeda Kashaf Naqvi - 125 28 October 2024 3
Table of Figures:
Figure 1: ERD for Summit Hotels...........................................................................................................6
Syeda Kashaf Naqvi - 125 28 October 2024 4
Question 1:
a) What is the difference between Delete Drop and Truncate?
Delete: It deletes specific rows from the table where a specific condition is met.
Drop: It is used to delete the entire table along with its data.
Truncate: This deletes all the records/rows from the table.
b) How can we apply pattern on a string column during creation?
We can use the Check constraint as:
Email nvarchar (100) Check (Email LIKE _%@_%._%)
This will create an email like pattern for an attribute named email, _ is for a single char and %
is a wildcard for zero to any number of characters.
c) What is referential Integrity?
Referential integrity means that the data remains consistent throughout the database, the
foreign key values must only reference valid values that must be present in the other table.
d) In three Tier Architecture, what do we mean by logical data independency?
Logical data independency refers to the ability to modify the database schema at the logical
level without affecting the application programs or the user interface in the presentation layer.
It ensures that changes to how data is stored or organized do not require modifications to the
application code, providing a separation between the database schema and the application
layer.
Question 2:
Consider the following Statement and Draw the Entity-Relationship model (ERD) by showing
all attributes, identifiers, relationships, Cardinalities and Modalities. Provide physical model to
elaborate Primary keys, Unique Keys and Foreign keys.
Summit Hotels & Resorts
Primary Keys (PK):
Guest: GuestID
Reservation: ReservationID
Room: RoomNumber
Staff: StaffID
Assignment: AssignmentID
Unique Keys:
Guest: Email
Syeda Kashaf Naqvi - 125 28 October 2024 5
Room: Nightly Rate
Staff: Contact Details
Foreign Keys (FK):
Reservation: GuestID (references Guest. GuestID)
Reservation: RoomNumber (references Room. RoomNumber)
Reservation Assignment: StaffID (references Staff. StaffID), ReservationID (references
Reservation, ReservationID)
Assignment: StaffID (references Staff,StaffID)
Figure 1: ERD for Summit Hotels
Syeda Kashaf Naqvi - 125 28 October 2024 6
Question 3:
Consider following Relational schema and write the DDL commands (Marks 9)
a) Write a SQL query to create all the tables, attributes and relations. Add auto-increment
constraint for primary key in all tables.
Create Table Staff
(
StaffID int identity(1,1) Primary key,
FirstName nvarchar(250),
LastName nvarchar(250),
Position nvarchar(200),
ContactDetails nvarchar(150)
)
Create Table Books
(
BooksID int identity(1,1) Primary key,
Title nvarchar(300),
Author nvarchar(250),
PublicationDate date,
Genre nvarchar(300),
Syeda Kashaf Naqvi - 125 28 October 2024 7
AvailabilityStatus nvarchar(100)
)
Create Table Patron
(
PatronID int identity(1,1) Primary key,
PName nvarchar(200),
Address nvarchar(max),
Phone nvarchar(20),
Email nvarchar(150)
)
Create Table Transaction
(
TransactionID int identity(1,1) Primary key,
DueDate date,
ReturnDate date,
FineAmount Money,
Books_book_id int foreign key references Books (BookID),
Patron_patron_id int foreign key references Patron (PatronID),
Assignment_assignment_id int foreign key references Assignment (AssignmentID)
)
Create Table Transaction_Assignment
(
AssignmentID int identity(1,1) primary key ,
Assignment_Date_Time date,
Status nvarchar(100) ,
Staff_staff_id int foreign key references Staff (StaffID),
Transaction_transaction_id int foreign key references Transactions (TransactionID),
)
b) Add default constraint in any of the string columns.
Alter Table Patron
Alter Column Address Set Default ‘NA’
c) For return date in transaction table, add default constraint to enter current date and
time
Alter Table Transactions
Alter Column ReturnDate Set Default getdate()
Syeda Kashaf Naqvi - 125 28 October 2024 8
d) Use alter command to add salary column in staff table.
Alter table Staff
Add salary int
e) For Books table, use alter command and add a constraint on Genre that it could only
have “Classical”, “Sci-Fic” and “Religious” as values.
Alter table Books
Add constraint genrechk Check (Genre IN(‘Classical’,’Sci-Fic’,’Religious’)
Question 4:
Consider schema of question 3 and solve following question
a) Write a query to display name and genre of the book as a sentence as following
Book _(Book title here)_ if from genre ___Sci-Fic____
Select CONCAT(‘Book’,title,’if from genre’,genre) AS Sentence From Books
b) Write a query to display all staff members whose salary is greater than Average salary
Select * from Staff Where Salary > (SELECT AVG(salary) FROM staff)
c) Write a query to display all transactions details where the fine is applied
Select * from Staff where fine is not NULL
OR
Write a query to display all books that are available
Select * from Books where status = ‘Available’ or status = ‘available’
d) Write a query to display all transaction whose due date was October 2022
Select * from transaction where MONTH(due_date) = 10 AND YEAR(due_date) = 2022;
Syeda Kashaf Naqvi - 125 28 October 2024 9