使用 Oracle 将树从一个表插入到另一个表的最佳方法是什么

发布于 2024-11-09 18:43:53 字数 846 浏览 4 评论 0 原文

我有一张存放树木的桌子。有一个node_id和parent_id。

当我尝试以下操作时:

insert into table1 select * from table2 start with node_id = 1 connect by prior node_id = parent_id order by parent_id nulls first

我收到此错误:

Error starting at line 6 in command:
insert into table1 select * from table2 start with node_id = 1 connect by prior node_id = parent_id order by parent_id nulls first
Error report:
SQL Error: ORA-02291: integrity constraint (XVTEST.REGIONAL_DEFAULT_DELETE) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause:    A foreign key value has no matching primary key value.
*Action:   Delete the foreign key or add a matching primary key.

我明白为什么会收到此错误。我只是想知道是否有一种方法可以在不进行递归 pl/sql 过程的情况下做到这一点。想法?如果不可能,是否有人有类似的程序可以用作示例?

I have a table that stores trees. There is a node_id and parent_id.

When I try the following:

insert into table1 select * from table2 start with node_id = 1 connect by prior node_id = parent_id order by parent_id nulls first

I get this error:

Error starting at line 6 in command:
insert into table1 select * from table2 start with node_id = 1 connect by prior node_id = parent_id order by parent_id nulls first
Error report:
SQL Error: ORA-02291: integrity constraint (XVTEST.REGIONAL_DEFAULT_DELETE) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause:    A foreign key value has no matching primary key value.
*Action:   Delete the foreign key or add a matching primary key.

I understand why I am getting this error. I was just wondering if there was a way to do this with out making a recursive pl/sql procedure. Thoughts? If it is not possible does anyone have a procedure that like this that I could use as a sample?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2024-11-16 18:43:53

一种选择是将 FOREIGN KEY 约束创建为 DEFERRABLE,然后为您的事务将它们设置为 DEFERRED,这样外键约束的执行就会推迟到 COMMIT。

请注意,当表的内容违反约束时,您不想执行任何 SELECT。在某些情况下,某些 SELECT 语句将返回意外或不一致的结果。 (请务必考虑触发器执行的 SELECT 语句。)


-- to create foreign key constraints as deferrable
ALTER TABLE my_table ADD CONSTRAINT my_table_fk1 
FOREIGN KEY (other_table_id) REFERENCES other_table (id)
DEFERRABLE INITIALLY IMMEDIATE;

-- defer all deferrable constraints until the next commit
ALTER SESSION SET CONSTRAINTS=DEFERRED;
-- or
SET CONSTRAINTS ALL DEFERRED;

-- dml operations may now temporarily violate constraints
INSERT ... ; 
UPDATE ... ;

-- you can check the constraints before the commit
SET CONSTRAINTS ALL IMMEDIATE;

-- all deferred constraints will be enforced at the next commit
-- (a fk violation exception will be raised here, rather than by the DML)
COMMIT;

一些有用的参考:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:914629004506

http://download.oracle.com/docs/cd /B10500_01/server.920/a96524/c22integ.htm#4666


附录:

此方法专门适用于 Oracle 数据库,可能不适用于其他关系数据库引擎。

在延迟约束时针对表执行 SELECTS 的警告仅适用于延迟约束的会话。其他会话将看到一致的状态,因为它们不会看到任何未提交的更改。

使用 DEFERRED 约束优于禁用和重新启用约束,因为禁用约束会影响所有会话,并且重新验证约束会消耗大量资源(对于大型表)。

One option is to create the FOREIGN KEY constraints as DEFERRABLE, and then set them DEFERRED for your transaction, so the enforcement of the foreign key constraints is deferred until the COMMIT.

Note that you don't want to perform any SELECT while the contents of the tables violate the constraints. In some cases, some SELECT statements will return unexpected or inconsistent results. (Be sure to consider SELECT statements performed by triggers.)


-- to create foreign key constraints as deferrable
ALTER TABLE my_table ADD CONSTRAINT my_table_fk1 
FOREIGN KEY (other_table_id) REFERENCES other_table (id)
DEFERRABLE INITIALLY IMMEDIATE;

-- defer all deferrable constraints until the next commit
ALTER SESSION SET CONSTRAINTS=DEFERRED;
-- or
SET CONSTRAINTS ALL DEFERRED;

-- dml operations may now temporarily violate constraints
INSERT ... ; 
UPDATE ... ;

-- you can check the constraints before the commit
SET CONSTRAINTS ALL IMMEDIATE;

-- all deferred constraints will be enforced at the next commit
-- (a fk violation exception will be raised here, rather than by the DML)
COMMIT;

Some helpful references:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:914629004506

http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm#4666


ADDENDA:

This approach applies specifically to Oracle database, and may not be applicable to other relational database engines.

The warning against performing SELECTS against the tables while the constraints are deferred applies only to the session that defers constraints. Other session will see a consistent state, since they will not see any uncommitted changes.

Using DEFERRED constraints is preferred over disabling and re-enabling the constraints, since disabling constraints would affect all sessions, and re-validating the constraints can consume significant resources (for large tables).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文