简体   繁体   English

错误 1451 (23000) - MySQL 触发器中的触发器错误

[英]Error 1451 (23000) - Trigger Error in MySQL trigger

    mysql> create database lib;
    Query OK, 1 row affected (0.31 sec)
    mysql> use lib;
    Database changed

mysql> create table library_2
    -> (id int AUTO_INCREMENT primary key,
    -> Book_name varchar(20),
    -> Details varchar(50));
Query OK, 0 rows affected (2.24 sec)

mysql> insert into library_2 values
    -> (1,'aaa','bbb'),
    -> (2,'ccc','ddd'),
    -> (3,'eee','fff'),
    -> (4,'ggg','hhh');
Query OK, 4 rows affected (0.46 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select*from library_2;
+----+-----------+-------------------+
| id | Book_name | Details           |
+----+-----------+-------------------+
|  1 | aaa       | bbb               |
|  2 | ccc       | ddd               |
|  3 | eee       | fff               |
|  4 | ggg       | hhh               |
+----+-----------+-------------------+
4 rows in set (0.00 sec)

mysql> create table library_audit2 
    -> (id int AUTO_INCREMENT primary key,
    -> Book_Name varchar(20) not null,
    -> Details varchar(50) default null,
    -> change_date date,
    -> library_id int,
    -> foreign key(library_id) REFERENCES library_2(id));
Query OK, 0 rows affected (2.33 sec)

mysql> insert into library_audit2 values
    -> (10,'aaa','bbb','2011-9-1',1),
    -> (20,'ccc','ddd','2012-8-2',2),
    -> (30,'eee','fff','2013-7-3',3),
    -> (40,'ggg','hhh','2014-6-4',4);
Query OK, 4 rows affected (0.20 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select*from library_audit2;
+----+-----------+---------+-------------+------------+
| id | Book_Name | Details | change_date | library_id |
+----+-----------+---------+-------------+------------+
| 10 | aaa       | bbb     | 2011-09-01  |          1 |
| 20 | ccc       | ddd     | 2012-08-02  |          2 |
| 30 | eee       | fff     | 2013-07-03  |          3 |
| 40 | ggg       | hhh     | 2014-06-04  |          4 |
+----+-----------+---------+-------------+------------+
4 rows in set (0.00 sec)

    mysql> create trigger BeforeLibraryDelete1
    -> BEFORE DELETE
    -> ON library_audit2 FOR EACH ROW
    -> BEGIN
    -> declare id1 int;
    -> select library_id into id1 from library_audit2 where change_date=OLD.change_date;
    -> delete from library_2 li where li.id=id1;
    -> END $$
Query OK, 0 rows affected (0.45 sec)

mysql> DELIMITER ;
mysql> Delete from library_audit2 where change_date='2011-09-01';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`abc`.`library_audit2`, CONSTRAINT `library_audit2_ibfk_1` FOREIGN KEY (`library_id`) REFERENCES `library_2` (`id`))

I know what this error means, but i need a different trigger query to rectify this problem.我知道这个错误意味着什么,但我需要一个不同的触发器查询来纠正这个问题。 This seems to ending up wrong no matter what I try.无论我尝试什么,这似乎都以错误告终。 Kindly provide me with a query that works.请向我提供一个有效的查询。 Also because MYSQL doesn't work with INSTEAD OF, don't provide me with a query that has INSTEAD OF DELETE in it.也因为 MYSQL 不能与 INSTEAD OF 一起使用,所以不要向我提供包含 INSTEAD OF DELETE 的查询。 But a replacement for it in MYSQL would be highly appreciated.但是在 MYSQL 中替换它会受到高度赞赏。

You are running in an endless loop.你在无限循环中运行。

You have a delete trigger on the table library_audit2 and in that trigger you delete from the same table which invokes another trigger and so on.您在表library_audit2上有一个删除触发器,在该触发器中,您从调用另一个触发器的同一个表中删除,依此类推。

The DB won't allow that and returns that error message. DB 不允许这样做并返回该错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM