1
我嘗試添加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錯誤367421(可能不是新的標籤數據保存到MySQL):錯誤 1452(23000):不能添加或更新子行:外鍵 約束失敗(
sqse_001
tagsinblog
,約束tagsinblog_ibfk_2
外鍵(id_word
)參考文獻tagwords
(id_word
))
當我試圖在同一個表中插入沒有唯一約束,我沒有任何問題。