0% found this document useful (0 votes)
14 views5 pages

23 Test 2 Memo

Uploaded by

Uapi Muukua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

23 Test 2 Memo

Uploaded by

Uapi Muukua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Database Programming Test 2

Instructions:

Total Marks: 50

Section A

Theory

Question 1. Identify the uses and types of control structures (4)

Mark Allocation: (2 ) Marks for stating the purpose of control structures

(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.

They are used for making decisions,

repeating a block of statements,

and jumping from one section of code to another.

Examples are IF Statements and loops 1

Question 2. Explain the use of composite key data types?(4)

(1) mark for each correct use of composite keys

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)

From Lesson 3 Theory and Practical

(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

Question 4. Scenario: Bookstore Inventory Management

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.

Here are some tables in the "Readers' Haven" database:

‘Books’

• BookID (Primary Key, Auto Increment)


• Title
• Author
• Genre
• Price
• StockCount

‘Sales’

• SaleID (Primary Key, Auto Increment)


• BookID (Foreign Key to Books)
• QuantitySold
• SaleDate

Provide an SQL statements Create a stored procedure named sp_GetBooksByAuthorAndGenre which


takes two input parameters: @AuthorName and @GenreType. The procedure should return a list of
books (Title and Price) by the specified author and of the specified genre (6)

(1) Correct create procedure (0.5) for correct parameters (0.5) Correct data types (1)(1)(1)(1)

CREATE PROCEDURE sp_GetBooksByAuthorAndGenre

@AuthorName VARCHAR(100), (with any correct datatype they use)

@GenreType VARCHAR(100)

AS
BEGIN

SELECT Title, Price

FROM Books

WHERE Author = @AuthorName AND Genre = @GenreType;

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’

• EmployeeID (Primary Key, Auto Increment)


• FirstName
• LastName

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)

DECLARE @GivenFirstName NVARCHAR(50);

DECLARE @GivenLastName NVARCHAR(50);

SET @GivenFirstName = 'Ndina';

SET @GivenLastName = 'Iitumba';

IF EXISTS (SELECT 1 FROM Employees WHERE FirstName = @GivenFirstName AND LastName =


@GivenLastName)

PRINT 'Employee is part of the staff.'

ELSE

PRINT 'Employee not found.';

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

WHILE @Counter <= 10 1 mark

BEGIN

INSERT INTO Employees(Value) VALUES (@Counter); 1 mark

SET @Counter = @Counter + 1; 1 mark

END;

Question 9: We have a company system that tracks employee details and salary changes. We have
two tables:

‘employees’

• employee_id (Primary Key, INT, Auto-increment)


• first_name (VARCHAR)
• last_name (VARCHAR)
• salary (DECIMAL)

‘salary_changes’

• change_id (Primary Key, INT, Auto-increment)


• employee_id (INT, Foreign Key to employees.employee_id)
• old_salary (DECIMAL)
• new_salary (DECIMAL)
• change_date (DATE)

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)

CREATE TRIGGER tr_BeforeSalaryUpdate 1 mark

After UPDATE 1 mark

ON employees 1 mark

BEGIN

Update employees 1 mark


Set Change_Date =GetDATE() 1 mark

WHERE employeeID = employeeID

END;

Question 10: Using a table below called EmployeeDetails with the columns: EmpID, EmpLastName, and
Employee.
CREATE TABLE EmployeeDetails (

EmpID INT PRIMARY KEY,

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;

You might also like