如何在mysql中定义关系规则?
表1:bookId authorId bookName
表2:authorId authorName
这就是我希望Mysql服务器的行为方式:当我尝试使用在table2中不存在的authorId向table1中插入一行时,mysql会产生错误.
您必须将authorid定义为外键.你需要做这样的事情:
Alter Table Table1 Add Foreign Key (authorid) References Table2 (authorid);
Run Code Online (Sandbox Code Playgroud)
确保你的表是innodb,因为它不适用于myisam表.你可以在这里找到文档.