MySQL trigger and mysql trigger

Source: Internet
Author: User

MySQL trigger and mysql trigger

I have read a lot of blog materials on the Internet and I feel that everyone is copying each other. I will summarize it based on my understanding and experience;

First, create a trigger:

A friend who uses Navicat can create a trigger on it:

(I simply copy the preceding SQL statement)

Create trigger 'C _ time' before insert on 'student'
For each row;

C_time is the name of a trigger.

Before and after are the trigger times.

Trigger event triggered by insert updata delete

On Connection table name

For each row indicates each field.

The next step is the focus. How can we use it when we create it;

DELIMITER $

Create trigger t_time after insert on student for each row

Begin

Declare c int; set c = (select stu t from class where id = new. id );

Update class set stuCount = c + 1 where id = new. id;

End

$ DELIMITER;

The trigger can be regarded as a function. Use DELIMITER $ DELIMITER to pack the expression in the begin end, and declare to write the variable type, you can use this begin end expression to make full use of your imagination!

I believe many people have seen the role of the trigger.

Purpose:

1. Modify the data permission restriction. I can write the restriction id in begin end to modify my data.

2. Two or more associated tables when data in one table is cleared, the associated fields of other associated tables can be updated to null.

3. You can track data changes and so on.

Of course, this is more important than that. There are still many other functions that will not be written here.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.