【问题标题】:How to Cascade on Delete with PhpMyAdmin如何使用 PhpMyAdmin 级联删除
【发布时间】:2017-10-09 14:58:14
【问题描述】:

我想在我的测试环境中清除一些表,然后用真实环境中的值填充它们。但是我不想清除多个表,太多了。

一个例子:
我有一张桌子appointment 和一张桌子client
在表appointment 中有一个client.id 列。
当我在 PhpMyAdmin 中单击 Empty the table (TRUNCATE) 以获取 appointment 时,client 中的所有条目都保持不变。

我可以在 PhpMyAdmin 中设置一些属性,这样当我在appointment 中点击Empty the table (TRUNCATE) 时,client 中与appointment 相关的所有条目也将被删除?

【问题讨论】:

  • 如果您的架构没有为您的表指定 FOREIGN KEY 约束,级联将不会自动工作。
  • 如果它之后会自动工作并且可以使用 PhpMyAdmin 来完成,那么如果你告诉我怎么做,你就会得到正确的答案! :)

标签: phpmyadmin


【解决方案1】:

在开始之前,两个表都必须有 innodb 存储引擎。 我提供的答案假设 clientid 是主键。您不能将约会.clientid 设置为主,因为它不是唯一的。您应该将 client.id 设置为主键。因此,当您从 clients 表中删除 clientid 时,将删除具有该 clientid 的所有约会。 选择约会表,点击表结构,打开“关系视图”链接。 Appointment table Structure

在关系菜单中,选择 clientid 列并使用 client.id 列设置外键约束。最后选择 CASCADE 选项 ON DELETE。 Relations menu

【讨论】:

    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 2011-11-23
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多