【发布时间】:2009-05-15 15:45:37
【问题描述】:
如何在mysql中定义关系规则?
表 1: bookId authorId bookName
表2: authorId 作者姓名
这就是我希望 Mysql 服务器的行为方式:当我尝试在 table1 中插入一行时,其 authorId 在 table2 中不存在,mysql 会产生错误。
【问题讨论】:
如何在mysql中定义关系规则?
表 1: bookId authorId bookName
表2: authorId 作者姓名
这就是我希望 Mysql 服务器的行为方式:当我尝试在 table1 中插入一行时,其 authorId 在 table2 中不存在,mysql 会产生错误。
【问题讨论】:
您必须将 authorid 定义为外键。你需要做这样的事情:
Alter Table Table1 Add Foreign Key (authorid) References Table2 (authorid);
确保您的表是 innodb,因为它不适用于 myisam 表。您可以找到文档here。
【讨论】: