0% found this document useful (0 votes)
4 views

Databse programming Assignement

This document is an assignment report detailing SQL programs created for a database using Wampserver. It includes the implementation of stored procedures, functions, triggers, cursors, assertions, and views to manage student data, such as updating phone numbers and enforcing age restrictions. The report provides code examples and explanations for each SQL component utilized in the cs2db database.

Uploaded by

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

Databse programming Assignement

This document is an assignment report detailing SQL programs created for a database using Wampserver. It includes the implementation of stored procedures, functions, triggers, cursors, assertions, and views to manage student data, such as updating phone numbers and enforcing age restrictions. The report provides code examples and explanations for each SQL component utilized in the cs2db database.

Uploaded by

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

NAMES: MUGWANEZA MANZI Audace

RegNumber: 223010386
Class: Computer Science II
Date: 19 March 2025

Assignment of Database programming at Mar 19, 2025


This report consist of SQL Programs that perform various task on the database. I used
Wampserver, which is a combination of Apache, MySQL and PHP. I used MySQL in browser.

In my commands you will see comments begin with -- as my MySQL version allowed me to use
● SQL Stored Procedures
These are my tables in cs2db database

Lecturers table has ID,Lecturer and SuperVisor columns.

I created two procedures


This procedure is being used to Update the phone number for any specific student by
passing his Id in the parameter.
mysql> call UpdatePhone(1)
Calling this procedure with 2 as a parameter sets, phone for student with id 2 to
0123456789

DELIMETER ;;
CREATE PROCEDURE UpdatePhone()
BEGIN
UPDATE student
SET phone = '0123456789'
WHERE ID = parm;
END
DELIMETER;
A corresponding image
● SQL Functions
This function displays all tables in database
BEGIN
DECLARE table_count INT;

SELECT COUNT(*) INTO table_count


FROM information_schema.tables
WHERE table_schema = DATABASE();
Result of executing the function

RETURN table_count;
END
This function displays current time.
BEGIN
RETURN NOW();
END

Result of executing the function.


● SQL Triggers
This trigger prevents from entering phone number which is less than 10 characters

DELIMITER $$

CREATE TRIGGER prevent_short_phone


BEFORE INSERT ON student
FOR EACH ROW
BEGIN
-- Check if phone number length is less than 10
IF CHAR_LENGTH(NEW.Phone) < 10 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Phone number must be at least 10 digits long';
END IF;
END $$

DELIMITER ;
Result of executing violating the rules
● SQL Cursors
This cursor adds a word verified to people names with phone number length greater or
equal to 10.
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE student_id INT;

-- Declare a cursor to fetch IDs of students with valid phone numbers


DECLARE cur CURSOR FOR
SELECT ID FROM student WHERE CHAR_LENGTH(Phone) >= 10;

-- Declare a handler for when no more rows are found


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;

read_loop: LOOP
-- Fetch a student ID
FETCH cur INTO student_id;

-- Exit the loop if no more rows


IF done THEN
LEAVE read_loop;
END IF;

-- Update the Fname of students with valid phone numbers


UPDATE student
SET Fname = CONCAT(Fname, ' - Verified')
WHERE ID = student_id;
END LOOP;

CLOSE cur;
END

Result of executing the cursor

● SQL Assertions
This Assertion is used to prevent inserting Age below 18. Down Is the result ot tring to
violate this rule in my database.
Here is the next result when users try to update their Age below 18

● SQL Views
This creates a view all tables in my database
CREATE VIEW student_supervisors AS
SELECT
s.ID AS student_id,
s.Fname AS student_name,
l.lecturer AS supervisor_name
FROM student s
JOIN lecturers l ON s.ID = l.ID;

You might also like