【问题标题】:Convert foreign key constraint from SQL Server to Oracle将外键约束从 SQL Server 转换为 Oracle
【发布时间】:2021-06-06 10:35:31
【问题描述】:

我想将以下脚本(SQL SERVER)转换为 Oracle:

ALTER TABLE [dbo].[TDistribucion]  WITH CHECK ADD  CONSTRAINT [FK_TDistribucion_TDocumentos] FOREIGN KEY([Id_Documento])
REFERENCES [dbo].[TDocumentos] ([Id])
GO
ALTER TABLE [dbo].[TDistribucion] CHECK CONSTRAINT [FK_TDistribucion_TDocumentos]
GO

我尝试在 Oracle 中运行相同的脚本:

ALTER TABLE TDISTRIBUCION  WITH CHECK ADD CONSTRAINT FK_TDistribucion_TDocumentos FOREIGN KEY(ID_DOCUMENTO)
REFERENCES TDOCUMENTOS (ID);
 
ALTER TABLE TDISTRIBUCION CHECK CONSTRAINT FK_TDistribucion_TDocumentos;

我得到了这个错误:

Error starting at line : 3 in command - 
ALTER TABLE TDISTRIBUCION  WITH CHECK ADD CONSTRAINT FK_TDistribucion_TDocumentos FOREIGN KEY(ID_DOCUMENTO) 
REFERENCES TDOCUMENTOS (ID) 
Error report - 
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 -  "invalid ALTER TABLE option"
*Cause:    
*Action:

我相信“WITH CHECK ADD”是问题所在!

【问题讨论】:

  • 没有WITH CHECK ADDADD CONSTRAINT 添加一个约束。 WITH CHECK 告诉服务器根据新约束检查任何现有数据
  • “WITH CHECK”有什么替代品吗?
  • 在约束名称之后添加ENABLE VALIDATE。顺便说一句,如果您想在数据库之间转换查询,您必须了解两者。您不能只运行脚本并期望它们工作
  • 不工作!不幸的是,我对 Oracle 有基本的了解。
  • 什么不起作用?你尝试了什么? I have basic knowledge of Oracle. 和 SQL Server。同样,除非您确实了解它们的作用,否则您无法迁移 SQL 脚本。这包括实际阅读文档。否则,您可能会造成严重损害并创建一个行为与原始数据库不同的数据库,不会执行相同的检查。或者您可能正在尝试转换不必要的语句。如果数据库为空,则根本不需要验证现有数据

标签: sql sql-server oracle database-migration


【解决方案1】:

看看这是否有帮助。

master 表 - 其主键列将从 tdistribucion 表中引用。

SQL> create table tdocumentos (id number primary key);

Table created.

detail - tdistribucion - 表格(只有一列,只是为了演示问题):

SQL> create table tdistribucion (id number);

Table created.

外键约束,使用正确的语法。如果没有记录违反约束,它将成功,如果相反,则失败。 Oracle 立即执行此操作,您不必运行另一个“检查”。由于两个表都是空的,它会成功:

SQL> alter table tdistribucion add constraint
  2    fk_dis_doc foreign key (id)
  3    references tdocumentos (id);

Table altered.

但是,如果有一些行违反了约束,您将无法创建它:

SQL> alter table tdistribucion drop constraint fk_dis_doc;

Table altered.

SQL> insert all
  2    into tdocumentos   (id) values (1)     --> 1 can't be referenced ...
  3    into tdistribucion (id) values (2)     --> ... by 2
  4  select * from dual;

2 rows created.

SQL> alter table tdistribucion add constraint
  2    fk_dis_doc foreign key (id)
  3    references tdocumentos (id);
  fk_dis_doc foreign key (id)
  *
ERROR at line 2:
ORA-02298: cannot validate (SCOTT.FK_DIS_DOC) - parent keys not found

但是,如果您将其创建为 enable novalidate,它将“丢弃”违反约束的行,但会检查任何后续插入:

SQL> alter table tdistribucion add constraint
  2    fk_dis_doc foreign key (id)
  3    references tdocumentos (id)
  4    enable novalidate;

Table altered.

只是为了表明违反约束的行确实存在:

SQL> select * from tdocumentos;

        ID
----------
         1

SQL> select * from tdistribucion;

        ID
----------
         2

没关系:

SQL> insert all
  2    into tdocumentos   (id) values (100)
  3    into tdistribucion (id) values (100)
  4  select * from dual;

2 rows created.

这不行:

SQL> insert all
  2    into tdocumentos   (id) values (300)
  3    into tdistribucion (id) values (400)
  4  select * from dual;
insert all
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK_DIS_DOC) violated - parent key not
found


SQL>

【讨论】:

  • 为我工作,删除“WITH CHECK”并在语句末尾添加“启用 novalidate”。
猜你喜欢
  • 2017-06-17
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
相关资源
最近更新 更多