用于协调的 SQL 语句
给定以下架构:
create table TBL1 (ID varchar2(100) primary key not null, MATCH_CRITERIA varchar2(100));
create table TBL2 (ID varchar2(100) primary key not null, MATCH_CRITERIA varchar2(100));
create table TBL_RESULT (ID varchar2(100) primary key not null, TBL1_ID varchar2(100), TBL2_ID varchar2(100));
create unique index UK_TBL_RESULT_TBL1_ID on TBL_RESULT(TBL1_ID);
create unique index UK_TBL_RESULT_TBL2_ID on TBL_RESULT(TBL2_ID);
create sequence SEQ_TBL_RESULT;
insert into TBL1 VALUES('1', '1');
insert into TBL1 VALUES('2', '1');
insert into TBL1 VALUES('3', '1');
insert into TBL2 VALUES('4', '1');
insert into TBL2 VALUES('5', '1');
insert into TBL2 VALUES('6', '1');
我需要一个 SQL 语句,该语句将通过协调 TBL1 和 TBL2 中的相等 MATCH_CRITERIA 来生成结果。
如果不存在 UNIQUE 约束,则以下内容将起作用。然而,我们的应用程序中需要独特的约束。
insert into TBL_RESULT (ID, TBL1_ID, TBL2_ID)
select SEQ_TBL_RESULT.nextval, TBL1.ID, TBL2.ID
from TBl1, TBL2
where TBL1.MATCH_CRITERIA = TBL2.MATCH_CRITERIA;
TBL_RESULT 的输出示例
| ID | TBL1_ID | TBL2_ID |
| '1' | '1' | '5' |
| '2' | '2' | '4' |
| '3' | '3' | '6' |
注意:“1”与“4”或“6”匹配并不重要。只要两个表中的 MATCH_CRITERIA 相等并且生成的表具有唯一的 TBL1_ID 和 TBL2_ID。
请注意,我们从 TBL1 插入一百万条记录,从 TBL2 插入另外一百万条记录。因此,使用 PL/SQL 的顺序插入是不可接受的,除非它运行得非常快(少于 15 分钟)。
Given the schema below:
create table TBL1 (ID varchar2(100) primary key not null, MATCH_CRITERIA varchar2(100));
create table TBL2 (ID varchar2(100) primary key not null, MATCH_CRITERIA varchar2(100));
create table TBL_RESULT (ID varchar2(100) primary key not null, TBL1_ID varchar2(100), TBL2_ID varchar2(100));
create unique index UK_TBL_RESULT_TBL1_ID on TBL_RESULT(TBL1_ID);
create unique index UK_TBL_RESULT_TBL2_ID on TBL_RESULT(TBL2_ID);
create sequence SEQ_TBL_RESULT;
insert into TBL1 VALUES('1', '1');
insert into TBL1 VALUES('2', '1');
insert into TBL1 VALUES('3', '1');
insert into TBL2 VALUES('4', '1');
insert into TBL2 VALUES('5', '1');
insert into TBL2 VALUES('6', '1');
I need an SQL statement that will produce the result by reconciling equal MATCH_CRITERIA from TBL1 and TBL2.
The following would work given that the UNIQUE constraint is not present. However, we need the unique constraints in our application.
insert into TBL_RESULT (ID, TBL1_ID, TBL2_ID)
select SEQ_TBL_RESULT.nextval, TBL1.ID, TBL2.ID
from TBl1, TBL2
where TBL1.MATCH_CRITERIA = TBL2.MATCH_CRITERIA;
Example output of TBL_RESULT
| ID | TBL1_ID | TBL2_ID |
| '1' | '1' | '5' |
| '2' | '2' | '4' |
| '3' | '3' | '6' |
Note: it doesn't matter if '1' is matched with '4' or '6'. As long as the MATCH_CRITERIA from both tables are equal and the resulting table has unique TBL1_ID and TBL2_ID.
Note that we are inserting a million records from TBL1 and another million records from TBL2. Thus, sequential insert using PL/SQL is not acceptable unless it can run really fast (less than 15 minutes).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
注意:它假设两个表中每个匹配集中的行数相等。
how about this:
Note: It assumes that there are an equal number of rows in each matching set in both tables.
像这样的事情怎么样:
这应该可以防止违反您的独特约束。
What about something like this:
This should prevent violation of your unique constraints.