An article to give you a deep understanding of Mysql triggers

An article to give you a deep understanding of Mysql triggers

Based on the student, course, and sc tables in the school database, create triggers that meet the following requirements:

數據庫navicat cmd界面運行

1. When inserting or modifying the SC table, if the test score is not in the range of 0-100, the insertion or modification operation is canceled.

DELIMITER ;
DROP TRIGGER IF EXISTS `tri_update_grade`;
DELIMITER ;;
CREATE TRIGGER `tri_update_grade` BEFORE UPDATE ON `sc` FOR EACH ROW begin
if new.grade > 100 then
delete from sc;
elseif new.grade < 0 then
delete from sc;
end if;
end
;;
DELIMITER ;
DROP TRIGGER IF EXISTS `tri_insert_grade`;
DELIMITER ;;
CREATE TRIGGER `tri_insert_grade` BEFORE INSERT ON `sc` FOR EACH ROW begin
if new.grade > 100 then
delete from sc;
elseif new.grade < 0 then
delete from sc;
end if;
end
;;

2. When inserting into the SC table, if the total credits of the student's course selection exceed 30, an error will be reported and the insertion will be canceled.

DELIMITER ;
DROP TRIGGER IF EXISTS `tri_insert_credit`;
DELIMITER ;;
CREATE TRIGGER `tri_insert_credit` BEFORE INSERT ON `sc` FOR EACH ROW begin
SELECT sum(Ccredit) INTO @sumcred from Student join SC on SC.Sno=Student.Sno join Course on Course.Cno=SC.Cno WHERE sc.Sno=new.Sno;
if (@sumcred>30)
then
delete from sc;
end if;
end
;;

3. When modifying the SC table, it is not allowed to modify the course selection records of students who have failed the examination.

DELIMITER ;
DROP TRIGGER IF EXISTS `tri_grade`;
DELIMITER ;;
CREATE TRIGGER `tri_grade` BEFORE UPDATE ON `sc` FOR EACH ROW begin
select grade INTO @gra from sc where sno=new.sno and sno=new.sno;
if (@gra >= 60)
then
set new.grade=20;
else
delete from sc;
end if;
end
;;

4. When inserting into the STUDENT table, if the age is empty, the student's age is set to the average age of all students.

DELIMITER ;
DROP TRIGGER IF EXISTS `tri_student`;
DELIMITER ;;
CREATE TRIGGER `tri_student` BEFORE INSERT ON `student` FOR EACH ROW begin
select avg(Sage) INTO @avgage from student;
SET @old_age = new.Sage;
if @old_age IS NULL
then
set new.Sage=@avgage;
end if;
end
;;

5. Add two columns to the STUDENT table to store the student's average score and grade, and automatically maintain the student's average score and grade after inserting into the SC table (85-100, grade is "excellent"; 60-85, grade is "good", below 60, grade is "poor")

DELIMITER ;
DROP TRIGGER IF EXISTS `tri_comment`;
DELIMITER ;;
CREATE TRIGGER `tri_comment` BEFORE UPDATE ON `student` FOR EACH ROW begin
select avg(Grade) INTO @avggra from sc where sno=new.sno;
if @avggra > 85 then
update sc set new.Savg=@avggra,new.Scomment="you";
else if @avggra > 60 and @avggra <= 85 then
update sc set new.Savg=@avggra,new.Scomment="you";
else
update sc set new.Savg=@avggra,new.Scomment='cha';
end if;
end if;
end
;;
DELIMITER ;

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL events and triggers topic refinement
  • MySQL database triggers from beginner to proficient
  • Getting Started Guide for MySQL Stored Procedures, Triggers, and Event Schedulers
  • Detailed explanation of MySQL trigger trigger example
  • Introduction to the use and advantages and disadvantages of MySQL triggers
  • Detailed explanation of the idea of ​​MySQL trigger detecting a statement in real time for backup and deletion
  • Use of MySQL trigger
  • Use of MySQL triggers
  • MySQL trigger usage in simple terms

<<:  Solution to the bug that IE6 select cannot be covered by div

>>:  Use of SerialPort module in Node.js

Recommend

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

Detailed explanation of binary and varbinary data types in MySQL

Preface BINARY and VARBINARY are somewhat similar...

TABLE tags (TAGS) detailed introduction

Basic syntax of the table <table>...</tab...

Sample code for installing ASPNET.Core3.0 runtime in Linux

# The following examples are for x64-bit runtime ...

How to set the page you are viewing to not allow Baidu to save its snapshot

Today, when I searched for a page on Baidu, becaus...

React's method of realizing secondary linkage

This article shares the specific code of React to...

Implementation of proxy_pass in nginx reverse proxy

The format is simple: proxy_pass URL; The URL inc...

Detailed explanation of virtual DOM and diff algorithm in react

The role of virtual DOM First of all, we need to ...

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...

Vue + element to dynamically display background data to options

need: Implement dynamic display of option values ...

Detailed explanation of Nginx version smooth upgrade solution

Table of contents background: Nginx smooth upgrad...

JavaScript to display hidden form text

This article shares the specific code of JavaScri...