Quantcast
Channel: How can I do 'insert if not exists' in MySQL? - Stack Overflow
Browsing all 13 articles
Browse latest View live

Answer by Abdelrhman Mohamed for How can I do 'insert if not exists' in MySQL?

INSERT INTO table_name (columns) VALUES (values) ON CONFLICT (id) DO NOTHING;

View Article



Answer by Yeti for How can I do 'insert if not exists' in MySQL?

Update or insert without known primary keyIf you already have a unique or primary key, the other answers with either INSERT INTO ... ON DUPLICATE KEY UPDATE ... or REPLACE INTO ... should work fine...

View Article

Answer by Gilly for How can I do 'insert if not exists' in MySQL?

Something worth noting is that INSERT IGNORE will still increment the primary key whether the statement was a success or not just like a normal INSERT would. This will cause gaps in your primary keys...

View Article

Answer by wortwart for How can I do 'insert if not exists' in MySQL?

There are several answers that cover how to solve this if you have a UNIQUE index that you can check against with ON DUPLICATE KEY or INSERT IGNORE. That is not always the case, and as UNIQUE has a...

View Article

Answer by Jeb's for How can I do 'insert if not exists' in MySQL?

Try the following:IF (SELECT COUNT(*) FROM beta WHERE name = 'John'> 0) UPDATE alfa SET c1=(SELECT id FROM beta WHERE name = 'John')ELSEBEGIN INSERT INTO beta (name) VALUES ('John') INSERT INTO alfa...

View Article


Answer by Rocio for How can I do 'insert if not exists' in MySQL?

REPLACE INTO `transcripts`SET `ensembl_transcript_id` = 'ENSORGT00000000001',`transcript_chrom_start` = 12345,`transcript_chrom_end` = 12678;If the record exists, it will be overwritten; if it does not...

View Article

Answer by Jrm for How can I do 'insert if not exists' in MySQL?

Here is a PHP function that will insert a row only if all the specified columns values don't already exist in the table.If one of the columns differ, the row will be added.If the table is empty, the...

View Article

Answer by Server for How can I do 'insert if not exists' in MySQL?

Solution:INSERT INTO `table` (`value1`, `value2`) SELECT 'stuff for value1', 'stuff for value2' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `table` WHERE `value1`='stuff for value1' AND `value2`='stuff...

View Article


Answer by Zed for How can I do 'insert if not exists' in MySQL?

In MySQL, ON DUPLICATE KEY UPDATE or INSERT IGNORE can be viable solutions.An example of ON DUPLICATE KEY UPDATE update based on mysql.com:INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY...

View Article


Answer by knittl for How can I do 'insert if not exists' in MySQL?

Use INSERT IGNORE INTO table.There's also INSERT … ON DUPLICATE KEY UPDATE syntax, and you can find explanations in 13.2.6.2 INSERT ... ON DUPLICATE KEY UPDATE Statement.Post from bogdan.org.ua...

View Article

Answer by KLE for How can I do 'insert if not exists' in MySQL?

Any simple constraint should do the job, if an exception is acceptable. Examples:primary key if not surrogateunique constraint on a columnmulti-column unique constraintSorry if this seems deceptively...

View Article

How can I do 'insert if not exists' in MySQL?

I started by googling and found the article How to write INSERT if NOT EXISTS queries in standard SQL which talks about mutex tables.I have a table with ~14 million records. If I want to add more data...

View Article

Answer by Salman A for How can I do 'insert if not exists' in MySQL?

MySQL does not support the MERGE statement otherwise that would have been the most versatile solution. The INSERT IGNORE hack requires unique key constraints which is not always an option.A simple...

View Article

Browsing all 13 articles
Browse latest View live




Latest Images