MySQL: INSERTing a row IF NOT EXISTS — Yes, it’s possible!

Here’s how:

INSERT INTO mdr_school_new
(PID, PPID, NAME, MSTREET, MCITY, MZIPCODE, id, entity_creation_timestamp, entity_version, last_updated_timestamp) 
SELECT *
FROM (SELECT '00000011', '00000000', 'NGSP School', '5 Maxwell Drive', 'Clifton Park', '12065', UUID(), NOW(), 0, CURRENT_TIMESTAMP()) as T
WHERE NOT EXISTS(SELECT * FROM mdr_school_new WHERE NAME = 'NGSP School');

Author: Alex T. Silverstein

A developer who wants to help you and himself by writing down good tidbits for free

Leave a Reply

Your email address will not be published. Required fields are marked *