23 Test 2 Memo
23 Test 2 Memo
Instructions:
Total Marks: 50
Section A
Theory
(1) mark for mentioning a control structure, (1)mark for each correct purpose
Any answer in this lines- Control structures are fundamental constructs in database programming, and
they are used for the flow of code execution to be directed in different ways based on specific
conditions or sequences needed by the user/database programmer.
From Lesson 5 theory- The are used top allow you access to this: You will have all the related data as a
single unit. • You can easily access and modify the data. • Data is easier to manage, relate, and transport
if it is composite. • Consider having a single bag for all you laptop components instead of having a
separate bag for each component.
Us records when you want to store values of different data types that are logically related. If you create
a record to hold employee details, identify that all the values stored are related because they provide
information about a particular employee. • Use collections when you want to store values of the same
data type.
Question 3 Differentiate between COMMIT and ROLLBACK to distinguish between the two. (4)
(1) for correct meaning of Commit, (1) for correct example of possible outcome
(1)for correct meaning of Rollback (1) for correct example of possible outcome
COMMIT to save all, permanently to the database and the rollback command will undo all the
operations that have been executed.
Section B
Practical
You are the lead database developer for "Readers' Haven", a bookstore that has both physical and online stores.
The company has a database system in place to track its inventory of books. As the company is expanding its
operations, there is a need to have more refined procedures to efficiently handle common database operations.
‘Books’
‘Sales’
(1) Correct create procedure (0.5) for correct parameters (0.5) Correct data types (1)(1)(1)(1)
@GenreType VARCHAR(100)
AS
BEGIN
FROM Books
END;
Question 8: Building upon the existing "Readers' Haven" bookstore scenario: "Readers' Haven" also
manages its staff through the same database system. Given the following table:
‘Employees’
Write an SQL query that takes two inputs, @GivenFirstName and @GivenLastName, representing an
employee's first and last name respectively. The query should:
1. Check if the employee with the given first and last name exists in the Employees table.
2. If the employee exists, it should return a message saying, "Employee is part of the staff."
3. If the employee doesn't exist, it should return a message saying, "Employee not found." (5)
ELSE
Using the scenario given provide an SQL Statement using a WHILE loop and a counter to insert 10 rows
into the Employees table, with values from 1 to 10. (6 )
DECLARE @Counter INT = 1; 1 mark
BEGIN
END;
Question 9: We have a company system that tracks employee details and salary changes. We have
two tables:
‘employees’
‘salary_changes’
Whenever an employee's salary is updated in the employees table, we want to log the change into the
salary_changes table, recording the old and new salaries, the date of the change, and the employee's ID.
Provide an SQL Statement to create a trigger called tr_BeforeSalaryUpdate which is set to run before an
update operation on the employees table. If the old salary (OLD.salary) is different from the new one
(NEW.salary), the trigger will insert a new record into the salary_changes table, logging the salary
change. The CURDATE() function is used to capture the current date of the change. (5)
ON employees 1 mark
BEGIN
END;
Question 10: Using a table below called EmployeeDetails with the columns: EmpID, EmpLastName, and
Employee.
CREATE TABLE EmployeeDetails (
EmpLastName VARCHAR(100),
Employee VARCHAR(100)
);
Provide your SQL Statement to demonstrate the transaction to insert a new record (5)
1 mark for begin transaction, 1 mark for Insert, 1 mark for table name , 1 mark for attributes, 1 mark for
Values
BEGIN TRANSACTION;
INSERT INTO EmployeeDetails (EmpID, EmpLastName, Employee) VALUES (1, 'Ndinelao', 'Iitumba');
COMMIT;