hujingwei

测试(从头开始走一遍流程):

1)创建Person表:

[sql] view plaincopy
 
  1. drop table person;  
  2. create table person(  
  3. id int NOT NULL AUTO_INCREMENT primary key,  
  4. name varchar(8) not null,  
  5. sex bit,  
  6. age int check(age > 0 and age < 200),  
  7. addr varchar(100) default \'\' not null  
  8. );  
  9. select * from person;  
此时Person表为空

2)创建countPerson表

[sql] view plaincopy
 
  1. drop table if exists countPerson;  
  2. create table countPerson(  
  3. totalnum int default 0  
  4. );  
  5. insert into countPerson(totalnum) values(0);  
  6. select * from countPerson;  

此时初始totalNum = 0

 

3) 创建触发器

[sql] view plaincopy
 
  1. DROP TRIGGER IF EXISTS insertTrigger;  
  2. delimiter //  
  3. CREATE TRIGGER insertTrigger  
  4. AFTER INSERT ON person  
  5. FOR EACH ROW  
  6. BEGIN  
  7.   update countPerson set totalNum = totalNum + 1;  
  8. END;//  

4)在Person表中添加记录insert
[sql] view plaincopy
 
  1. insert into person(name, sex, age, addr) values(\'Jeremy\', 1, 20, \'SH\');  
  2. insert into person(name, sex, age, addr) values(\'Hellon\', 1, 22, \'SH\');  
  3. select * from person;  
此时Person表中记录如下,

 

5)看一下触发器有没有起作用

[sql] view plaincopy
 
  1. select * from countPerson;  

发现有作用了。

 

附:

此外使用下面语句可以看到数据库的触发器信息

[sql] view plaincopy
 
  1. SELECT * FROM information_schema.`TRIGGERS`;  

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-09-05
  • 2021-07-07
  • 2022-01-28
  • 2021-07-19
  • 2021-12-05
  • 2022-12-23
猜你喜欢
  • 2022-01-07
  • 2021-08-22
  • 2022-12-23
  • 2021-11-24
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案