1. Unit 13 – Your Answers
Ted Carr (S00942627)
IT 2351 – Enterprise Database Systems
06 November 2016
Your Answers
1 Transaction with ROLLBACK
Text of your CREATE PROCEDURE
-- 13.1 Ted Carr (S00942627)
-- Demonstrate transaction failure with ROLLBACK
USE college;
DROP procedure IF EXISTS Fix_Egghead1;
DELIMITER //
CREATE PROCEDURE Fix_Egghead1()
BEGIN
DECLARE sql_error TINYINT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET sql_error = TRUE;
START TRANSACTION;
DELETE FROM student
WHERE student.ID = 27;
INSERT INTO faculty
VALUES (DEFAULT, 'Egghead', 'Eduardo', 'EduardoEgghead@college.edu', CURDATE(), 105000, 1);
IF sql_error = FALSE THEN
Page 1 of 4
2. Unit 13 – Your Answers
COMMIT;
SELECT 'The transaction was committed.’
ELSE
ROLLBACK;
SELECT 'The transaction was rolled back.’
END IF;
END //
DELIMITER ;
CALL Fix_Egghead1();
Screen shot of the result grid from stored procedure
Transaction with COMMIT
Page 2 of 4
3. Unit 13 – Your Answers
2 Text of your CREATE PROCEDURE
-- 13.2 Ted Carr (S00942627)
-- Demonstrate transaction success with ROLLBACK
USE college;
DROP procedure IF EXISTS Fix_Egghead1;
DELIMITER //
CREATE PROCEDURE Fix_Egghead1()
BEGIN
DECLARE sql_error TINYINT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET sql_error = TRUE;
START TRANSACTION;
DELETE FROM registration
WHERE registration.StudentID = 27;
DELETE FROM student
WHERE student.ID = 27;
INSERT INTO faculty
VALUES (DEFAULT, 'Egghead', 'Eduardo', 'EduardoEgghead@college.edu', CURDATE(), 105000, 1);
IF sql_error = FALSE THEN
COMMIT;
Page 3 of 4
4. Unit 13 – Your Answers
SELECT 'The transaction was committed.’
ELSE
ROLLBACK;
SELECT 'The transaction was rolled back.’
END IF;
END //
DELIMITER ;
CALL Fix_Egghead1();
Screen shot of the result grid from stored procedure
Page 4 of 4