Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Equivalent to MySQL INSERT IGNORE?

I need to update a query so that it checks that a duplicate entry does not exist before insertion. In MySQL I can just use INSERT IGNORE so that if a duplicate record is found it just skips the insert, but I can't seem to find an equivalent option for Oracle. Any suggestions?

like image 427
Bad Programmer Avatar asked Feb 17 '12 16:02

Bad Programmer


People also ask

How do I ignore insert in SQL query?

Syntax - INSERT IGNORE INTO table_name (column1, column2, …, columnN) VALUES (value1, value2, …, valueN); column1, column2, …, columnN - Specifies the column names that are used to insert data. table_name – Specifies the name of the table.

What is mysql insert ignore?

The INSERT IGNORE command keeps the first set of the duplicated records and discards the remaining. The REPLACE command keeps the last set of duplicates and erases out any earlier ones. Another way to enforce uniqueness is to add a UNIQUE index rather than a PRIMARY KEY to a table.

Is insert ignore faster than insert?

Similar to deletes and replace into, with this scheme, “insert ignore” can be two orders of magnitude faster than insertions into a B-tree.

What is Ignore_row_on_dupkey_index?

When ignore_row_on_dupkey_index hint is used in a SQL insert on a table with a unique key index, all duplicates will be silently ignored, rather than causing the traditional error, ORA-00001 unique constraint violated.


2 Answers

Check out the MERGE statement. This should do what you want - it's the WHEN NOT MATCHED clause that will do this.

Do to Oracle's lack of support for a true VALUES() clause the syntax for a single record with fixed values is pretty clumsy though:

MERGE INTO your_table yt USING (    SELECT 42 as the_pk_value,            'some_value' as some_column    FROM dual ) t on (yt.pk = t.the_pke_value)  WHEN NOT MATCHED THEN     INSERT (pk, the_column)    VALUES (t.the_pk_value, t.some_column); 

A different approach (if you are e.g. doing bulk loading from a different table) is to use the "Error logging" facility of Oracle. The statement would look like this:

 INSERT INTO your_table (col1, col2, col3)  SELECT c1, c2, c3  FROM staging_table  LOG ERRORS INTO errlog ('some comment') REJECT LIMIT UNLIMITED; 

Afterwards all rows that would have thrown an error are available in the table errlog. You need to create that errlog table (or whatever name you choose) manually before running the insert using DBMS_ERRLOG.CREATE_ERROR_LOG.

See the manual for details

like image 78
a_horse_with_no_name Avatar answered Sep 23 '22 13:09

a_horse_with_no_name


If you're on 11g you can use the hint IGNORE_ROW_ON_DUPKEY_INDEX:

SQL> create table my_table(a number, constraint my_table_pk primary key (a));  Table created.  SQL> insert /*+ ignore_row_on_dupkey_index(my_table, my_table_pk) */   2  into my_table   3  select 1 from dual   4  union all   5  select 1 from dual;  1 row created. 
like image 34
Jon Heller Avatar answered Sep 20 '22 13:09

Jon Heller