Extra Marks Flashcards
Given the following database Schema answer the questions that
follow:
STUDENT(cisNum, name, gender,age )
BOOK(bookId, title)
BORROW (cisNum, bookId, dateBorrowd, dueDate)
a) Write SQL statement snippets that would enforce entity integrity and referential integrity for all three tables.
Entity Integrity:
ALTER TABLE STUDENT
ADD PRIMARY KEY (cisNum);
ALTER TABLE BOOK
ADD PRIMARY KEY (bookId);
ALTER TABLE BORROW
ADD PRIMARY KEY (cisNum, bookId);
Referential Integrity:
ALTER TABLE BORROW
ADD FOREIGN KEY (cisNum) REFERENCES STUDENT(cisNum);
ALTER TABLE BORROW
ADD FOREIGN KEY (bookId) REFERENCES BOOK(bookId);
Given the following database Schema answer the questions that
follow:
STUDENT(cisNum, name, gender,age )
BOOK(bookId, title)
BORROW (cisNum, bookId, dateBorrowd, dueDate)
b) Explain how entity integrity may prevent the insertion of a row of data into a table.
Entity integrity ensures unique primary keys, rejecting insertion attempts with duplicate primary key values, preserving uniqueness.
Given the following database Schema answer the questions that
follow:
STUDENT(cisNum, name, gender,age )
BOOK(bookId, title)
BORROW (cisNum, bookId, dateBorrowd, dueDate)
c) Explain how referential integrity may prevent the insertion of a row of data into a table.
Referential integrity ensures foreign key values match primary key values in referenced tables, rejecting insertions without matching keys.
Explain what this procedure does.
CREATE OR REPLACE PROCEDURE ladystudents
As
CURSOR abc IS select * from student where gender
= ‘F’ ;
Females abc%ROWTYPE;
BEGIN
OPEN abc
LOOP
FETCH abc into Females;
DBMS_OUTPUT.PUT_LINE(Females.TutorName);
EXIT WHEN abc%NOTFOUND;
END LOOP;
CLOSE abc;
END;
The procedure fetches and outputs the TutorName of female students using a cursor, iterating through the result set and printing the TutorName for each row.
Explain what this trigger does.
CREATE OR REPLACE PROCEDURE upadareMaleAges
As
CURSOR abc IS select * from student where gender =
‘M’ ;
Females abc%ROWTYPE;
BEGIN
OPEN abc
LOOP
FETCH abc into Males;
Update student set age = Males.age + 2 where cis
= Males.cis;
EXIT WHEN abc%NOTFOUND;
END LOOP;
CLOSE abc;
END;
The procedure updates the ages of male students by adding 2 to their current age in the STUDENT table.
Analyse the logical tables below and answer the questions that follow.
Client(client_Id, name, address,gender, age)
Account(acc_no, acc_name, balance)
Withdrawal (acc_no, client_Id,amt_withdrawn, date)
Note: Primary keys are underlined and the primary key for withdrawal
table is foreign
a) Name the strong entities featured on the above logical tables
Strong entities are Client and Account.
Write SQL statements to do the following
i. write an SQL to create the table withdrawal
CREATE TABLE Withdrawal (
acc_no INT,
client_Id INT,
amt_withdrawn DECIMAL(10, 2),
date DATE,
PRIMARY KEY (acc_no),
FOREIGN KEY (client_Id) REFERENCES Client(client_Id)
);
Write SQL statements to do the following
SQL Statement to List Clients who Made Withdrawals on 23 September 2020:
SELECT * FROM Client
WHERE client_Id IN (
SELECT client_Id FROM Withdrawal
WHERE date = ‘2020-09-23’
);
Write SQL statements to do the following
SQL Statement to Give Total Number of Clients:
SELECT COUNT(*) AS TotalClients FROM Client;
Write SQL statements to do the following
SQL Statement to Add City Column to Client Table:
ALTER TABLE Client
ADD city VARCHAR(50);
Write SQL statements to do the following
Trigger to Impose Maximum Withdrawal Amount:
CREATE OR REPLACE TRIGGER maxWithdrawal
BEFORE INSERT ON Withdrawal
FOR EACH ROW
BEGIN
IF :NEW.amt_withdrawn > 4000.00 THEN
RAISE_APPLICATION_ERROR(-20000, ‘Maximum withdrawal amount exceeded’);
END IF;
END;