[Solved] MySQL prevents the insertion of duplicate data

2023/04/28 4:46

Below is not recommended

  1. insert ignore into
  2. on duplicate key update
  3. replace into
  4. insert if not exists

Better use this one

It works in any cases and you don’t have to change your data table structure.

INSERT INTO dbo.Customer (firstname, lastname, phone)
SELECT 'Mitch''Valenta''555-867-5309'
WHERE NOT EXISTS
    (SELECT firstname, lastname
     FROM dbo.Customer
     WHERE firstname = 'Mitch' AND lastname = 'Valenta')

Leave a Reply

Back to top