1. Define the trigger:
语法:create trigger 触发器名
before|after 时间 on 表名 for each row 触发器语句
2. The role of the trigger:
When a statement in table a is executed, table b will be linked.
There are three main functions of triggers:
insert: new record, no old record
delete: delete old record, no new record
update: new record after change, and old record that has been changed
You can use old|new to refer to these records,
such as:
for each row
insert into the record table values(old.xxx,new.xxx);
#Create table
CREATE TABLE account_history(
-> hid INT(11) AUTO_INCREMENT PRIMARY KEY,
-> account_name VARCHAR(30),
-> changed_cash DECIMAL(9,2));
#When the account is updated, insert a statement in account_history
CREATE TRIGGER trig_account_his AFTER UPDATE ON account
FOR EACH ROW
INSERT INTO account_history (account_name,changed_cash)
VALUES('A',10000);
UPDATE account SET cash =cash+10000 WHERE NAME='A';
#When a statement is inserted in account, a statement is inserted in account_history
DELIMITER @@
CREATE TRIGGER trigger_a_insert AFTER INSERT ON account
FOR EACH ROW
BEGIN
INSERT INTO account_history(account_name,changed_cash)VALUES('cust','10000');
END@@
INSERT INTO account(NAME,cash)VALUES('cust',10000);@@
SELECT * FROM account;
SELECT * FROM account_history;
DROP TRIGGER trig_account_his;
DROP TRIGGER trigger_a_insert;
#Dynamic update
CREATE TRIGGER trigger_a_insert AFTER INSERT ON account
FOR EACH ROW
INSERT INTO account_history(account_name,changed_cash)
VALUES(new.name,new.cash);
INSERT INTO account(NAME,cash)VALUES('abc','100');
INSERT INTO account(NAME,cash)VALUES
('random',cash(RAND()*10000 DECIMAL(9,2)));
#When deleting a statement in account, update the status synchronously in account_history
CREATE TRIGGER trig_a_delete AFTER DELETE ON account
FOR EACH ROW
INSERT INTO account_history(account_name,changed_cash)
VALUES(old.name,old.cash*-1);
DELETE FROM account WHERE id=9;
#When updating a statement in account, update the status in account_history synchronously
CREATE TRIGGER trig_a_update AFTER UPDATE ON account
FOR EACH ROW
INSERT INTO account_history(account_name,changed_cash)
VALUES(CONCAT('old:',old.name,';new:',new.name),new.cash-old.cash)
UPDATE account SET cash=cash-10000, NAME='ICBC' WHERE NAME='B';
UPDATE TABLE account SET cash=10000000 WHERE NAME='c';
3. View:
#view
CREATE VIEW v_stu_sub_g AS
SELECT s.studentno,sub_no,s.studentname,subjectName,score,s.gradeid,gradename
FROM result r
JOIN student s ON r.stu_id=s.studentno
JOIN subjects ON sub_no=subjectNo
JOIN grades g ON s.gradeid=g.gradeid;
#Exercises:
Query and obtain the score tables of all students corresponding to all subjects, and form a view. Form a view
in the student table and grade table to display student id, student name, age id, and grade name
in the score table, student table and subject table to form a view, Display student names, subject names, and grades.
Combine the above two requirements and add the name of the grade on the basis of requirement 2.
Please obtain the top 5 students in each grade based on the above requirements.
CREATE VIEW v1 AS SELECT studentno ,studentname,gradeid,gradename
FROM student s
JOIN grades g ON s.gradeid=g.gradeid;
CREATE VIEW v2 AS SELECT studentname, subjectName,score
FROM result r
JOIN student s ON r.stu_id=s.studentno
JOIN subjects ON sub_no=subjectNo;
SELECT studentname ,subjectName,score
FROM v_stu_sub_g a
WHERE (SELECT COUNT(score) FROM v_stu_sub_g b WHERE
a.score>=b.score AND a.sub_no=b.sub_no)<=2
ORDER BY sub_no ,score DESC;
#Flow Control
DELIMITER@@
CREATE PROCEDURE testcase(IN VALUE INT(11))
BEGIN
CASE VALUE
WHEN 1 THEN SELECT 1+1;
WHEN 2 THEN SELECT 1+2;
WHEN 3 THEN SELECT 1+3;
ELSE SELECT 1+VALUE;
END CASE;
END@@
DELIMITER;
DELIMITER @@
CREATE PROCEDURE testwhile()
BEGIN
SET @i:=0;
SET @sum:=0;
WHILE@i<=10
DO
SET @sum:=@sum+@i;
SET @i:=@i+1;
END WHILE;
SELECT @sum;
END @@
DELIMITER ;