将外键约束从 SQL Server 转换为 Oracle
Convert foreign key constraint from SQL Server to Oracle
我想将以下脚本(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”!
看看这是否有帮助。
A master table - 它的主键列将从 tdistribucion
table.
中引用
SQL> create table tdocumentos (id number primary key);
Table created.
A detail - tdistribucion
- table(只有一栏,只是为了演示问题):
SQL> create table tdistribucion (id number);
Table created.
外键约束,使用正确的语法。如果没有记录违反约束,它将成功,如果相反,则失败。 Oracle 立即完成,您无需运行 另一次“检查”。由于两个 table 都是空的,它会成功:
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>
我想将以下脚本(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”!
看看这是否有帮助。
A master table - 它的主键列将从 tdistribucion
table.
SQL> create table tdocumentos (id number primary key);
Table created.
A detail - tdistribucion
- table(只有一栏,只是为了演示问题):
SQL> create table tdistribucion (id number);
Table created.
外键约束,使用正确的语法。如果没有记录违反约束,它将成功,如果相反,则失败。 Oracle 立即完成,您无需运行 另一次“检查”。由于两个 table 都是空的,它会成功:
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>