In Oracle database I have one table with primary key GAME_ID. I have to insert a copy of a row where game_name = 'Texas holdem' but it tells me:
An UPDATE or INSERT statement attempted to insert a duplicate key.
This is query I am using:
INSERT INTO GAME (SELECT * FROM GAME WHERE NAME = 'Texas Holdem');
Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.
If you attempt to insert a row with the same primary key as a previous row, you will get a SQL error (try it in the commented out code below). If you insert a row without specifying the primary key, then SQL will automatically pick one for you that's different from other values.
Using COUNT(*) = 0. To avoid duplicates, the COUNT or records returned by the subquery above should be zero.
Assuming your game_id
is generated by a sequence, you can get a new as part of the select statement:
INSERT INTO GAME (game_id, name, col_3)
SELECT seq_game_id.nextval, name, col_3
FROM GAME
WHERE NAME = 'Texas Holdem';
Let me just offer a slightly more abstract point of perspective...
You can insert a similar row through, as other answers have demonstrated.
1 Although practical DBMS (including Oracle) will typically allow you to create a table without any key, making identical duplicates physically possible. However, consider it a big red flag if you ever find yourself doing that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With