MySQL 外键关系与 mysql_insert_id 关联表
这是为了一种概念验证草案,以使事情正常运行,但不想拥有完全垃圾的代码。对于我的数据库,我尝试使用 innoDB 获得真正的外键关系,但无法获得。
我决定不使用外键,而是在插入后提取 mysql_insert_id(),将其保存为变量,然后将该变量放入相关表中。
这很可怕吗?一切似乎都运行良好,我能够根据需要连接和关联 ID。与我的方法相比,使用外键会给我带来什么好处(除了更新/删除级联)?
This is for a sort of proof of concept draft to get things working, but don't want to have completely crap code. For my database, I tried to get true foreign key relations going using innoDB, but couldn't get it.
Instead of using foreign keys, I decided to just pull mysql_insert_id() after inserts, saving it as a variable, then putting that variable into the related table.
Is this horrible? Everything seems to work well, and I'm able to connect and relate ID's as needed. What benefits would using foreign keys give me over my method (besides updates/deletes cascading)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要创建关系(主控->详细信息),您必须始终自己提供密钥,可以使用 mysql_insert_id、自然键或应用程序生成的密钥。
FOREIGN KEY
不会让那个为您工作。FOREIGN KEY
的作用是FOREIGN KEY
的情况下执行此操作)添加
FOREIGN KEY
约束语句的成本与其好处相比很小。To create a relation (master->detail), you have to always supply the keys by yourself, either using
mysql_insert_id
, natural keys or key generated by your applications. TheFOREIGN KEY
is not going to make that work for you.What
FOREIGN KEY
does isFOREIGN KEY
)The cost of adding the
FOREIGN KEY
constraint statement is small compared to its benefits.