从父表中选择是否存在奇怪的外键约束违规

时间:2014-02-17 22:08:52

标签: mysql sql foreign-key-relationship

注意:该书的ID是其标题

insert ignore into category_book 
select 1 as user_id, 'name' as category_name, b.id as book_id 
from book b 
where b.id regexp('word') AND b.id IS NOT NULL

我明白了:

  

MySQL错误1452:无法添加或更新子行:外键约束失败(bookcategory_book,CONSTRAINT fk_category_book_book1 FOREIGN KEY(book_id)REFERENCES { {1}}(book)ON DELETE CASCADE ON UPDATE CASCADE)

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试将记录插入到category_book中,其book_id在表格书中不存在。可能是order_book列的顺序与您在select中提供的列不同。尝试相同的查询,但指出insert子句中的列。