douhu8851 2010-01-05 23:35
浏览 39
已采纳

这是Mysql外键的工作吗?

In the past few years I have read plenty of articles on Foreign Keys in MySQL, nothing recent though. I know they are good to use on something like a forum topic that has child post's under it, if I delete a topic with 100 post's connected to that topic, foreign keys will make it delete those 100 topics for me, am I correct so far?

I'm working on a social network in php/mysql, there is a user table with a user ID then there is about 10 other tables that are connected to that table by that User ID field, should I be using foreign keys on this? I won't ever need to update all the tables but for example if a user deletes there account, in the past I would have the PHP run like 10 delete queries, 1 on each table for the user. Does this sound like a good job to be using Foreign Keys on?

  • 写回答

5条回答 默认 最新

  • dpa89292 2010-01-05 23:41
    关注

    Foreign keys don't do the work of deleting data. They ensure that you don't enter data into a field that isn't valid from the parent table. i.e. you wouldn't be able to enter a record with UserId 100 in a child table if you don't have a UserId 100 in the users table.

    That being said using Cascading Deletes with a foreign key will do what you are asking, but be sure that is what you really want to happen. Do you really want all posts by a given user to be deleted if they delete their account, and all reply posts to the parent posts all the way down the thread.

    You really need to know what you are doing to use cascading deletes properly and not really mess up your data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等