Prevent MySQL Duplicate Insert - mysql

I have a mysql table with a auto incremented key. I need a way to only insert into that table if the table does not contain the row I am inserting. INSERT IGNORE and INSERT ON DUPLICATE KEY UPDATE won't work because the auto incremented key will cause the row to always be different. What is another way I can insert the following line only if there is no duplicate row? Thanks.
INSERT INTO TableName (column1, column2, column3) VALUES ("value1", "value2", "value3");

Set a UNIQUE constraint on whichever column you need to be unique, or a combination of columns.
For example:
ALTER TABLE `TableName`
ADD UNIQUE `constrain_me` (`column1`, `column2`);
If you want to ignore any error a duplicate insert might give, use INSERT IGNORE, although you may want to catch this error instead of brushing it under the rug.

You can create Unique Indexes on the fields that you don't want duplicated.
CREATE UNIQUE INDEX MyIndex ON column1
This way, if a duplicate value is added, the query will error out. It's also worth noting that this method does allow NULL values to be added (i.e. two rows with NULL column1 values won't count as duplicates)

Related

Ignore values on insert with ON DUPLICATE UPDATE

I have an Insert Statement like:
f"INSERT INTO `system_measurements`(`Global_irradiance_tilted_in_Wh_per_m2`, `a_id`, `subDate`) VALUES ('{temp}', '{temp_id}', '{i.date()}')"
And want it to ignore existing entries without checking the date everytime. So i thouhgt I could use
ON DUPLICATE KEY UPDATE a_id=a_id
But it still adds all values to the table.
I interpret your question as saying that a new row is inserted despite the on duplicate key.
In order for on duplicate key to work, you need a unique constraint or index. The update takes place when the query violates the unique constraint.
I am guessing that you want this on a_id, so be use you have something like:
alter table system_measurements add constraint unq_ system_measurements_a_id
unique (a_id);
INSERT IGNORE will do nothing other than discovering that it is a duplicate. "Duplicate" is checked via the PRIMARY KEY and any UNIQUE keys.
Simply stick IGNORE after INSERT in the SQL you have.

How to insert values in table after adding new column at once?

I have a mysql table. I have inserted a new column namely s_no (integer) at first position. Now I want to insert values from 1, 2, 3 at once and also want to make it primary key.
To make it primary key:
ALTER TABLE tableName
ADD PRIMARY KEY (s_no);
Read more at:
http://www.mysqltutorial.org/mysql-primary-key/
To insert rows:
INSERT INTO tableName
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
INSERT statements that use VALUES syntax can insert multiple rows. To
do this, include multiple lists of column values, each enclosed within
parentheses and separated by commas.
Read more at:
http://dev.mysql.com/doc/refman/5.5/en/insert.html

MySQL INSERT only if two fields don't match

I have 6 fields with the primary as ID and is set to auto_increment. I want to INSERT a new row if DATE and FROM do not match. I was thinking of REPLACE INTO or ON DUPLICATE KEY UPDATE but from what I know I have to have one of them as my Primary? I don't care how its done I just need some help with a query that would work.
ID
DATE
STORE
TOTAL
NPS
FROM
What you need is a unique index composed of both the DATE and FROM fields.
ALTER TABLE table ADD UNIQUE INDEX(DATE, FROM);
Then you can use this type of query:
INSERT IGNORE INTO table (columns) VALUES (...)
The IGNORE statement will skip any INSERT that would otherwise cause a duplicate key error.

In MySQL, how do I write a query to skip a duplicate row while inserting, when there's no unique field (primary key)?

I am trying to insert rows into a table that has no unique field or primary key. How can I write a query that will simply ignore the insert if there already exists a row with the exact same values on all fields -- a duplicate row?
Thanks.
You must have a primary key or unique key defined on some column or columns in the table for uniqueness to have any meaning. Every mechanism for detecting duplicates automatically relies on this being true.
You can't do the SELECT COUNT(*)... solution because it's subject to race conditions. That is, someone could insert a duplicate row in the moment after you select and before you insert. The only way around this is to lock the table with SELECT ... FOR UPDATE or LOCK TABLES.
Uh, why not make a primary key?
Otherwise, you have to basically do SELECT COUNT(*) FROM table WHERE field1=value AND ... AND fieldN=value for before EVERY insert.

MySQL 'UPDATE ON DUPLICATE KEY' without a unique column?

What are peoples' thoughts on the most performance efficient way to do the following query:
3 column table
if the combination of col_1 and col_2 values already exist UPDATE col_3
else INSERT new row
I assume i need some kind if UPDATE ON DUPLICATE KEY (which i've never used before), however I do not have a 'KEY' but instead a pair of two values (columns) to make a key...
You can create a PRIMARY or UNIQUE key out of multiple columns (called a composite key) in MySQL, which'll allow ON DUPLICATE KEY to work just fine.
// create a composite index
CREATE INDEX my_composite_index ON my_table (column1, column2);
// insert or update
INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2') ON DUPLICATE KEY UPDATE column3=column3+1;
Most efficient way is to create UNIQUE KEY and use ON DUPLICATE KEY UPDATE.
Slower way is to:
LOCK TABLE
SELECT TABLE (you need an index anyway for the best performance)
if exists, UPDATE
else INSERT
UNLOCK TABLES
Edit: Ignore my suggestions
You can use a composite key as ceejayoz said, however I think you need REPLACE INTO instead of UPDATE ON DUPLICATE KEY because REPLACE also inserts if no duplicate is found.
Note: I don't know the workings of UPDATE ON DUPLICATE KEY but it sounds like it doesn't perform inserts.