0

由我们支持的各种客户运行的脚本的一部分添加了外键,但在运行这部分脚本时我不想添加外键之一,因为有些客户在他们的设置中没有这个。如何从下面的代码中删除 FK6。我在下面的外键 FK6 上尝试了 DROP,但出现语法错误。

IF OBJECT_ID('FK2') IS NULL
BEGIN
ALTER TABLE [table_1] ADD
  CONSTRAINT [FK2] FOREIGN KEY
  (
     [reason_gen]
  ) REFERENCES [table_1] (
     [field_1]
  ) NOT FOR REPLICATION,
  CONSTRAINT [FK3] FOREIGN KEY
  (
     [field_2],
     [field_3]
  ) REFERENCES [table_2] (
     [field_4],
     [field_5]
  ) NOT FOR REPLICATION,
  CONSTRAINT [FK4] FOREIGN KEY
  (
     [res_code_gen]
  ) REFERENCES [table_3] (
     [field_6]
  ) NOT FOR REPLICATION,
  CONSTRAINT [FK5] FOREIGN KEY
  (
     [field_7],
     [field_8]
  ) REFERENCES [table_4] (
     [field_7],
     [field_8]
  ) NOT FOR REPLICATION,

 ALTER TABLE [table_1] DROP
 CONSTRAINT [FK6] FOREIGN KEY
  (
     [field_9]
  ) REFERENCES [ftable_5] (
     [field_10]
  ) NOT FOR REPLICATION
END
4

2 回答 2

1

删除外键的语法只是:

ALTER TABLE [table_1] DROP CONSTRAINT [FK6] 
于 2013-07-02T18:17:22.260 回答
0

这是哪个数据库?甲骨文、MYSQL、SQL Server?

对于 Oracle,语法是:

ALTER TABLE table_name
drop CONSTRAINT constraint_name;
于 2013-07-02T18:18:34.217 回答