Mysql learning record (12) -- trigger _ MySQL

Source: Internet
Author: User
I. theory: 1. the trigger cannot use the dynamic SQL statement of call, you cannot directly return data to client 2. you cannot use statements to start or end a transaction in a trigger. 3. the trigger can only roll back the transaction in a table that supports transactions. 2. practice: mysqlusetest1; Readingtablein 1. theory:
1. the trigger cannot use a dynamic SQL statement using call, and cannot directly return data to the client.
2. you cannot use the start or end transaction statement in the trigger.
3. triggers can only be rolled back in tables that support transactions.

II. practice:

mysql> use test1;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> create table film_text(    -> film_id int(11),    -> title varchar(20),    -> description varchar(180)    -> ) engine = innodb charset = utf8 ;Query OK, 0 rows affected (0.05 sec)mysql> create table film(    -> film_id int(11),    -> title varchar(20),    -> description varchar(180)    -> ) engine = innodb charset = utf8 ;Query OK, 0 rows affected (0.02 sec)mysql> CREATE TABLE `tri_demo` (    ->   `id`  int(11) auto_increment not null primary key,    ->   `note` varchar(50) DEFAULT NULL    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;Query OK, 0 rows affected (0.03 sec)mysql> create trigger ins_film_bef    -> before insert on film for each row begin    -> insert into tri_demo(note) values ('before insert');    -> end;    -> $$Query OK, 0 rows affected (0.02 sec)mysql> create trigger ins_film_aft    -> after insert on film for each row begin    -> insert into film_text(title) values ('after insert');    -> end;    -> $$Query OK, 0 rows affected (0.02 sec)mysql> create trigger upd_film_bef    -> before update on film for each row begin    -> insert into tri_demo(note) values ('before update');    -> end;    -> $$Query OK, 0 rows affected (0.00 sec)mysql> create trigger upd_film_aft    -> after update on film for each row begin    -> insert into tri_demo(note) values ('after update');    -> end;    -> $$Query OK, 0 rows affected (0.02 sec)mysql> delimiter ;mysql> insert into film values ( 1,'film_name','film_description');Query OK, 1 row affected (0.00 sec)mysql> select * from film;+---------+-----------+------------------+| film_id | title     | description      |+---------+-----------+------------------+|       1 | film_name | film_description |+---------+-----------+------------------+mysql> select * from tri_demo;+----+---------------+| id | note          |+----+---------------+|  1 | before insert |+----+---------------+mysql> update film set title='film_update' where film_id = 1;Query OK, 1 row affected, 1 warning (0.01 sec)Rows matched: 1  Changed: 1  Warnings: 1mysql> select * from film;+---------+--------------------+--------------------------+| film_id | title              | description              |+---------+--------------------+--------------------------+|       1 | film_update        | film_description         |+---------+--------------------+--------------------------+1 rows in set (0.00 sec)mysql> select * from tri_demo;+----+---------------+| id | note          |+----+---------------+|  1 | before insert ||  2 | before update ||  3 | after update  |+----+---------------+3 rows in set (0.00 sec)mysql> show triggers \G;*************************** 1. row ***************************             Trigger: ins_film_bef               Event: INSERT               Table: film           Statement: begininsert into tri_demo(note) values ('before insert');end              Timing: BEFORE             Created: NULL            sql_mode:              Definer: root@localhostcharacter_set_client: utf8collation_connection: utf8_general_ci  Database Collation: utf8_general_ci*************************** 2. row ***************************             Trigger: ins_film_aft               Event: INSERT               Table: film           Statement: begin     insert into film_text(title) values ('after insert');     end              Timing: AFTER             Created: NULL            sql_mode:              Definer: root@localhostcharacter_set_client: utf8collation_connection: utf8_general_ci  Database Collation: utf8_general_ci*************************** 3. row ***************************             Trigger: upd_film_bef               Event: UPDATE               Table: film           Statement: begininsert into tri_demo(note) values ('before update');end              Timing: BEFORE             Created: NULL            sql_mode:              Definer: root@localhostcharacter_set_client: utf8collation_connection: utf8_general_ci  Database Collation: utf8_general_ci*************************** 4. row ***************************             Trigger: upd_film_aft               Event: UPDATE               Table: film           Statement: begininsert into tri_demo(note) values ('after update');end              Timing: AFTER             Created: NULL            sql_mode:              Definer: root@localhostcharacter_set_client: utf8collation_connection: utf8_general_ci  Database Collation: utf8_general_ci4 rows in set (0.01 sec)
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.