How to use mysql triggers

Source: Internet
Author: User

Mysql triggers are frequently used in the use of mysql trigger databases. The following describes the entire process from creation to use of mysql triggers for your reference.

1. After the latest version is installed, you must run:

Mysql_fix_privilege_tables or mysql_install_db

(You only need to run one of them) -- otherwise, the stored procedure will not work. I also enable an informal SQL script after the root identity, as shown below:

Mysql> source/home/pgulutza/mysql-5.0/scripts/mysql-prepare-privilege-tables-for-5. SQL

2. Verify the version:

Show variables like 'version'; or SELECT version ();

When you see the number '5. 0. x', you can determine that the stored procedure can work properly on this client.

3. Open the database: use database-name

4. Run the delimiter command to change the delimiter before creating a mysql trigger. For example:

Delimiter //

5 | create a mysql trigger

 
 
  1. mysql>create trigger trig1 after insert on test1 for each row  
  2.  
  3.       ->BEGIN  
  4.  
  5.        ->insert into test2(tvalue) values(NEW.tname);  
  6.  
  7.        ->END;  
  8.  
  9.        ->//  

Here is another example of deletion:

 
 
  1. mysql>create trigger book_trig before delete on lib_book for each row  
  2.          ->begin  
  3.          ->delete from lib_borrow where book_id=OLD.id;  
  4.         ->end;  
  5.  
  6.        ->//  

6. OK. The trigger is created successfully.
 

Create MySQL trigger syntax

Three examples of mysql triggers

How to Create a master-slave server for mysql

Mysql Aggregate functions

Use of MySQL concat Functions

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.