我怎样才能修改外键? [英] how can i modify foreign key?

查看:130
本文介绍了我怎样才能修改外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FOREIGN KEY(成员)REFERENCES分数(级别) ON DELETE CASCADE,

我想将其更改为:

  FOREIGN KEY(member,subject)REFERENCES scores(level,subject)ON DELETE set null,

是否有可能?

解决方案

单一语句,请参阅 ALTER TABLE 语法,其中没有可用的ALTER CONSTRAINT。



您必须使用2个ALTER TABLE语句来完成您想要的操作。

删除第一个使用ALTER TABLE DROP FOREIGN KEY。
使用ALTER TABLE ADD CONSTRAINT FOREIGN KEY在第二列中重新创建它。



您可以在一个事务中封装这两个事务原子修改。

I'm wondering if it's possible to modify a Foreign Key?

FOREIGN KEY (member) REFERENCES scores (level) ON DELETE CASCADE,

And I would like to change it to:

FOREIGN KEY (member, subject) REFERENCES scores (level, subject) ON DELETE set null,

Is it possible?

解决方案

You cannot modify the key in a single statement, see the ALTER TABLE syntax, in which there is no ALTER CONSTRAINT available.

You must use 2 ALTER TABLE statements to accomplish what you want.

Delete the key in the first one using an ALTER TABLE DROP FOREIGN KEY. Re-create it with the new columns in the second, using an ALTER TABLE ADD CONSTRAINT FOREIGN KEY.

You can encapsulate both within a single transaction to make an atomic modification.

这篇关于我怎样才能修改外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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