如何在表上添加ON DELETE约束? [英] How can I add ON DELETE constraint on the table?

查看:99
本文介绍了如何在表上添加ON DELETE约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在表上添加ON DELETE约束?

How can I add ON DELETE constraint on the table?

推荐答案

使用ALTER TABLE + ADD CONSTRAINT.例如.如果要通过member_id链接表成员和概要文件,并在每次删除成员时级联删除概要文件,则可以编写如下内容:

Use ALTER TABLE+ADD CONSTRAINT. E.g. if you want to link tables members and profiles by member_id and cascade delete profiles each time the member is deleted, you can write something like this:

ALTER TABLE profiles
   ADD CONSTRAINT `fk_test`
   FOREIGN KEY (`member_id` )
   REFERENCES `members` (`member_id` )
   ON DELETE CASCADE

如果您需要更新该约束-您必须先删除它,然后再次创建,就没有直接的方法来更改它.

If you will need to update that constraint - you'll have to remove it at then create again, there's no direct way to alter it.

ALTER TABLE profiles DROP FOREIGN KEY `fk_test`

这篇关于如何在表上添加ON DELETE约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆