如何更改外键的操作 [英] How to change action for a foreign key

查看:41
本文介绍了如何更改外键的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个外键,它的作用是:

Let's suppose that I have a foreign key whose actions are:

on delete set default, on update cascade

如果我想更改操作,如何在更改表选项中指定?
我试过这个:

If I want to change the action, how do I specify this in the alter table options?
I have tried this:

change my_table add on delete cascade my_key;

但它给出了一个语法错误:

But it gives a syntax error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on delete cascade matricola' at line 1

我想更改删除引用的主键时触发的默认操作.

I want to change the default action triggered when the primary key referenced is deleted.

推荐答案

ALTER TABLE my_table DROP FOREIGN KEY my_key;
ALTER TABLE my_table ADD CONSTRAINT my_key FOREIGN KEY ('some_id') 
        REFERENCES some_table ('id') ON UPDATE CASCADE ON DELETE CASCADE;

据我所知,您必须放下钥匙并使用新规则创建新钥匙.

As far as i know you have to drop your key and create new one with new rules.

这篇关于如何更改外键的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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