如何一次删除多个约束(Oracle、SQL)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5859106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:22:20  来源:igfitidea点击:

How to drop more than one constraint at once (Oracle, SQL)

sqldatabaseoracleconstraints

提问by John Manak

I'm changing constraints in my database and I need to drop some of them. I know that for a single constraint, the command is following:

我正在更改我的数据库中的约束,我需要删除其中的一些。我知道对于单个约束,命令如下:

ALTER TABLE tblApplication DROP CONSTRAINT constraint1_name;

However, when I try

但是,当我尝试

ALTER TABLE tblApplication DROP (
  CONSTRAINT constraint1_name,
  CONSTRAINT constraint2_name,
  CONSTRAINT constraint3_name
);

it doesn't work and I need to do:

它不起作用,我需要这样做:

ALTER TABLE tblApplication DROP CONSTRAINT constraint1_name;
ALTER TABLE tblApplication DROP CONSTRAINT constraint2_name;
ALTER TABLE tblApplication DROP CONSTRAINT constraint3_name;

Is there a way to remove more than one constraint in a single command? I'd like to avoid repeating ALTER TABLE tblApplication, just like with the ADDcommand:

有没有办法在单个命令中删除多个约束?我想避免重复ALTER TABLE tblApplication,就像使用ADD命令一样:

ALTER TABLE tblApplication ADD (
  CONSTRAINT contraint1_name FOREIGN KEY ... ENABLE,
  CONSTRAINT contraint2_name FOREIGN KEY ... ENABLE,
  CONSTRAINT contraint3_name FOREIGN KEY ... ENABLE
);

回答by Sodved

Yes you can. You just need to repeat 'drop constraint' per constraint. e.g.

是的你可以。您只需要为每个约束重复“放置约束”。例如

alter table t1
drop constraint fk1
drop constraint fk2
/

Edit: I tested this against Oracle 11, and it worked fine. Don't know about older versions.

编辑:我在 Oracle 11 上对此进行了测试,效果很好。不知道旧版本。

回答by Jordi M.

There is an alternative form to drop constraints related to a column in a table, also dropping the column with CASCADE:

有一种替代形式可以删除与表中列相关的约束,也可以使用 CASCADE 删除列:

ALTER TABLE table1 DROP (columnName) CASCADE CONSTRAINTS;

It is tested on Oracle 11g

它在 Oracle 11g 上测试