【发布时间】:2015-04-06 11:57:59
【问题描述】:
我正在尝试将unique constraint 添加到两个外键:
CREATE TABLE tagsInBlog(
id_tag int(10) not null auto_increment,
id_word int(10) not null,
id_page int(11),
PRIMARY KEY(id_tag),
FOREIGN KEY (id_page) REFERENCES archive(id),
FOREIGN KEY (id_word) REFERENCES tagwords(id_word)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
ALTER TABLE tagsinblog
ADD UNIQUE tagBlogConstraint (id_word, id_page);
创建时我没有收到任何错误,但是当我尝试插入时我得到:
MYSQL ERROR 367421 (could not save new tag data into Mysql): Error 1452 (23000):无法添加或更新子行:外键 约束失败(
sqse_001.tagsinblog, CONSTRAINTtagsinblog_ibfk_2外键 (id_word) 引用tagwords(id_word))
当我尝试在没有唯一约束的情况下插入同一个表时,我没有任何问题。
【问题讨论】:
标签: mysql sql reference constraints unique