DROP PROCEDURE IF EXISTS printIncomeLevel;
DELIMITER //
CREATE PROCEDURE printIncomeLevel (IN monthly_value INT)
BEGIN
DECLARE income_level varchar(20);
IF monthly_value <= 4000 THEN
SET income_level = 'Low Income';
ELSEIF monthly_value > 4000 AND monthly_value <= 7000 THEN
SET income_level = 'Avg Income';
ELSE
SET income_level = 'High Income';
END IF;
INSERT INTO log (description)
VALUES ('getIncomeLevel procedure was called');
SELECT income_level;
END;
After creating the procedure, we can call it using CALL keyword:CALL printIncomeLevel(450)DROP FUNCTION IF EXISTS getIncomeLevel;
DELIMITER //
CREATE FUNCTION getIncomeLevel ( monthly_value INT )
RETURNS varchar(20)
BEGIN
DECLARE income_level varchar(20);
IF monthly_value <= 4000 THEN
SET income_level = 'Low Income';
ELSEIF monthly_value > 4000 AND monthly_value <= 7000 THEN
SET income_level = 'Avg Income';
ELSE
SET income_level = 'High Income';
END IF;
INSERT INTO log (description)
VALUES ('getIncomeLevel function was called');
RETURN income_level;
END; //
DELIMITER ;
After creating the function we can call it:SELECT getIncomeLevel(450)CASCADE - deletes or updates appropriate rows in the child table
SET NULL - sets the foreign key column of the child table to NULL
RESTRICT - rejects the operation for the parent table
NO ACTION - equivalent to RESTRICT
Note: RESTRICT is the same as omitting the ON DELETE or ON UPDATE clause, which means that it's default actionafter insert and logs into log tableDROP TRIGGER IF EXISTS after_student_insert;
DELIMITER $$
CREATE TRIGGER after_student_insert BEFORE INSERT
ON students
FOR EACH ROW
BEGIN
INSERT INTO log (description)
VALUES ('One student inserted');
END$$
DELIMITER ;before insert and checks & replaces gender valuesDROP TRIGGER IF EXISTS before_student_insert;
DELIMITER $$
CREATE TRIGGER before_student_insert BEFORE INSERT
ON students
FOR EACH ROW
BEGIN
INSERT INTO log (description)
VALUES ('One student will be inserted');
IF (NEW.gender = 'Female') THEN
SET NEW.gender = 2;
ELSE
SET NEW.gender = 1;
END IF;
END$$
DELIMITER ;FOREIGN KEY constraint with student_id column and references it to students.id ALTER TABLE notes
ADD CONSTRAINT fk_student_note
FOREIGN KEY (student_id)
REFERENCES students(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
We can omit CONSTRAINT fk_student_note sub-clause.
The difference is that if we omit the sub-clause, MySQL names the constraint automaticallyALTER TABLE notes
ADD FOREIGN KEY (student_id)
REFERENCES students(id)
ON UPDATE CASCADE
ON DELETE RESTRICTFOREIGN KEY constraint protects data integrity.
Let's create students and note tablesUSE university;
DROP TABLE IF EXISTS students;
CREATE TABLE students (
id int AUTO_INCREMENT,
first_name varchar(50),
last_name varchar(50),
PRIMARY KEY(id)
);
Let's add FOREIGN KEY called fk_student_note to the notes tableDROP TABLE IF EXISTS notes;
CREATE TABLE notes (
id int AUTO_INCREMENT,
student_id int,
title varchar(50),
description varchar(50),
PRIMARY KEY(id),
INDEX(student_id),
CONSTRAINT fk_student_note
FOREIGN KEY (student_id)
REFERENCES students(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
);