Home > Database > Mysql Tutorial > body text

【原创】关于自身表的外键触发器实现

WBOY
Release: 2016-06-07 17:39:55
Original
1264 people have browsed it

目前遇到这样一个外键定义:`t_girl`.`tb1`CONSTRAINT`fk_1`FOREIGNKEY(`r_id`)REFERENCES`tb1`(`id`)ONDELETECASCADE目的是对于自己的另外一个字段进行约束,其

 

 

目前遇到这样一个外键定义:

`t_girl`.`tb1`

  CONSTRAINT `fk_1` FOREIGN KEY (`r_id`) REFERENCES `tb1` (`id`) ON DELETE CASCADE

 

目的是对于自己的另外一个字段进行约束,香港服务器, 其实这样看来, 后面的级联删除就没有必要了,因为针对的是同一张表的同一条记录。

/* Trigger structure for table `tb1` */

 

DELIMITER $$

 

/*!50003 DROP TRIGGER*//*!50032 IF EXISTS */ /*!50003 `tr_tb1_before_insert` */$$

 

/*!50003 CREATE */ /*!50017 DEFINER = 'root'@'localhost' */ 

/*!50003 TRIGGER `tr_tb1_before_insert` 

         BEFORE INSERT ON `tb1` 

         FOR EACH ROW 

         BEGIN

           set new.r_id = new.id;

         END 

*/$$

 

 

DELIMITER ;

 

我们在除了INNODB之外的其他引擎可以直接用触发器来实现。

本文出自 “上帝,咱们不见不散!” 博客,香港服务器,请务必保留此出处

,虚拟主机
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!