91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

數據庫中批量插入數據時主鍵沖突怎么辦

發布時間:2021-11-11 13:35:44 來源:億速云 閱讀:1382 作者:小新 欄目:關系型數據庫

小編給大家分享一下數據庫中批量插入數據時主鍵沖突怎么辦,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

客戶有這樣一個需求:有一個大表A,有主鍵,經常需要往這個表中批量插入大量數據,但插入的數據可能自身重復或者跟表A重復。

下面測試幾種插入的方法:

DB:ORACLE 11.2.0.4

新建測試表:

新建表scott.tb_01

create table scott.tb_01

as

SELECT level c1,level c2,level c3

FROM DUAL

CONNECT BY LEVEL <= 100000;

在表scott.tb_01上增加主鍵

create unique index scott.pk_tb_01 on scott.tb_01(c1); --這一步不是必需的,因為下一步會自動建索引,但這種建索引再加主鍵的方式可以在建索引的時候加并行。

alter table scott.tb_01 add constraint pk_tb_01 primary key (c1)   using index;

新建表scott.tb_02

create table scott.tb_02

as

SELECT level+100000 c1,level c2,level c3

FROM DUAL

CONNECT BY LEVEL <= 100000;

在表scott.tb_02上插入表scott.tb_01的100條數據做為重復數據

insert into scott.tb_02

SELECT c1,c2, c3

FROM scott.tb_01

where rownum <= 100;

commit;

現在需要將表scott.tb_02中和表scott.tb_01非重復的數據插入到scott.tb_01

方法1:關聯插入

INSERT INTO SCOTT.TB_01

SELECT A.* --這里如果表SCOTT.TB_02自身有重復數據,還要加上distinct

FROM SCOTT.TB_02 A

LEFT JOIN SCOTT.TB_01 B

ON A.c1 = B.c2

WHERE B.c1 IS  NULL;

100000 rows created.

上面一種常見的插入方法,這個方法的的問題在于,如果表SCOTT.TB_02和表SCOTT.TB_01都很大,兩個表關聯查詢的成本會很高,影響性能。

方法2:使用HINT:IGNORE_ROW_ON_DUPKEY_INDEX

不使用HINT:

INSERT

INTO SCOTT.TB_01

SELECT * FROM SCOTT.TB_02 ;

ERROR at line 1:

ORA-00001: unique constraint (SCOTT.PK_TB_01) violated

從上面可以看出直接將表SCOTT.TB_02插入到表SCOTT.TB_01會出現主鍵沖突,報ORA-00001錯誤。

使用HINT:

INSERT /*+IGNORE_ROW_ON_DUPKEY_INDEX(a PK_tb_01)*/

INTO SCOTT.TB_01 A

SELECT * FROM SCOTT.TB_02 ;

100000 rows created.

從上面可以看出,加上HINT后,表SCOTT.TB_02中和表SCOTT.TB_01沒有主鍵沖突的記錄插入到表SCOTT.TB_01中,沖突的100條記錄沒有插入,也沒有報錯。

方法3:使用impdp的skip_constraint_errors選項

創建dump目錄

create directory dump_home as '/home/oracle';

導出表scott.tb_02

expdp system tables=scott.tb_02  directory=dump_home dumpfile=expdp_tb_02.dmp logfile=expdp_tb_02.log

使用expdp導出表scott.tb_02,用于后續導入到表scott.tb_01中。

導入表scott.tb_02到scott.tb_01,不加skip_constraint_errors選項

impdp system tables=scott.tb_02 remap_table=scott.tb_02:tb_01  content=data_only directory=dump_home dumpfile=expdp_tb_02.dmp logfile=impdp_tb_02.log

Import: Release 11.2.0.4.0 - Production on Tue Dec 5 23:18:49 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "SYSTEM"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded

Starting "SYSTEM"."SYS_IMPORT_TABLE_01":  system/******** tables=scott.tb_02 remap_table=scott.tb_02:tb_01 content=data_only directory=dump_home dumpfile=expdp_tb_02.dmp logfile=impdp_tb_02.log

Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

ORA-31693: Table data object "SCOTT"."TB_01" failed to load/unload and is being skipped due to error:

ORA-00001: unique constraint (SCOTT.PK_TB_01) violated

Job "SYSTEM"."SYS_IMPORT_TABLE_01" completed with 1 error(s) at Tue Dec 5 23:19:02 2017 elapsed 0 00:00:09

從上面可以看出,如果不加skip_constraint_errors選項就會報主鍵沖突錯誤,導入失敗。

導入表scott.tb_02到scott.tb_01,加skip_constraint_errors選項

impdp system tables=scott.tb_02 remap_table=scott.tb_02:tb_01  content=data_only directory=dump_home dumpfile=expdp_tb_02.dmp logfile=impdp_tb_02.log data_options=skip_constraint_errors

Import: Release 11.2.0.4.0 - Production on Tue Dec 5 23:21:29 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "SYSTEM"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded

Starting "SYSTEM"."SYS_IMPORT_TABLE_01":  system/******** tables=scott.tb_02 remap_table=scott.tb_02:tb_01 content=data_only directory=dump_home dumpfile=expdp_tb_02.dmp logfile=impdp_tb_02.log data_options=skip_constraint_errors

Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

. . imported "SCOTT"."TB_01"                             1.796 MB 100000 out of 100100 rows

100 row(s) were rejected with the following error:

ORA-00001: unique constraint (SCOTT.PK_TB_01) violated

Rejected rows with the primary keys are:

Rejected row #1:

column C1: 1

Rejected row #99:

column C1: 99

Rejected row #100:

column C1: 100

Job "SYSTEM"."SYS_IMPORT_TABLE_01" successfully completed at Tue Dec 5 23:21:56 2017 elapsed 0 00:00:23

從上面可以看出,導入順利完成,并跳過了主鍵沖突的行,并在impdp的log中顯示行的信息。

看完了這篇文章,相信你對“數據庫中批量插入數據時主鍵沖突怎么辦”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

咸阳市| 建阳市| 饶阳县| 田林县| 茌平县| 商城县| 东阳市| 伊川县| 商丘市| 沙湾县| 兰州市| 叶城县| 遂平县| 民权县| 通榆县| 灵丘县| 太保市| 常山县| 甘洛县| 元氏县| 衡东县| 鄱阳县| 万安县| 九江市| 金乡县| 舞钢市| 平定县| 阜城县| 玛纳斯县| 盐源县| 水城县| 隆昌县| 镇远县| 湄潭县| 汕尾市| 深州市| 宁晋县| 昌图县| 礼泉县| 奉节县| 义乌市|