Create or update records Mysql/Sql

In this post, we are going to explain the SQL query that will create the row if already does not exist and update it if a duplicate unique key is found.

INSERT INTO UserDetail(UserId, About) 
VALUES (1,'This is about insert text') 
ON DUPLICATE KEY UPDATE About = 'This is about update text';

'UserId' must be the primary key or Unique key to make this query work. When you hit this first time it will insert records into the UserDetail table. Here we have made the UserId Unique key. When you again execute the above SQL query again it will update the records which have UserId 1.

Was this helpful?