Professional Documents
Culture Documents
Database Test Two SCHEME
Database Test Two SCHEME
Suppose that every time a new student is registered, the system should create a login account for that student. The
login details are stored in the table called users. The table fields and their values are listed here under:
1. Write DDL statement to create the users table using the information on the above table. (2 Marks)
CREATE TABLE users
(
userID VARCHAR(100) NOT NULL PRIMARY KEY,
password TEXT NOT NULL,
role VARCHAR(100) NOT NULL DEFAULT 'student',
lastLogin DATETIME NOT NULL DEFAULT NOW()
);
2. Write a before insert trigger that will create the login details in the users table for every student that will be
registered in the student table. (3 Marks)
DELIMITER !!
CREATE TRIGGER before_student_insert BEFORE INSERT ON student
FOR EACH ROW
BEGIN
INSERT INTO users SET
userID =LCASE(NEW.regNo),
password =MD5(UCASE(NEW.surName));
END!!
DELIMITER ;
3. Write a procedure called getStudentByGender that receive gender value (male or female) and display all details for
all students with that gender value. (3 Marks)
DELIMITER !!
CREATE PROCEDURE getStudentByGender (IN jinsi VARCHAR(10))
BEGIN
SELECT * FROM student WHERE sex=jinsi;
END !!
DELIMITER ;
4. create a view called viewWatanzania that display from the student table, regNo, surName, otherNames, sex and
program for all Tanzania student. Your view should display only 12 records skipping the first 18 records. (2 Marks)
CREATE VIEW viewWatanzania AS
SELECT regNo, surname, otherNames, sex, program FROM student WHERE nationality='Tanzanian' LIMIT 18,12;