On the DUPLICATE KEY UPDATE Clause

The ON DUPLICATE KEY UPDATE clause allows us to specify how MariaDB should handle duplicates when inserting new records.

Here is an illustrated example of the ON DUPLICATE KEY UPDATE clause which will be used at the end of a query for the INSERT clause as shown below:

INSERT INTO Users (user_id, name, email) VALUES (1, 'ratnala', 'ratnala1@gmail.com')
ON DUPLICATE KEY UPDATE name = 'minal', email = 'minal1@gmail.com';

Output:

DUPLICATE KEY UPDATE clause

Explanation:

  • If a duplicate key is found in the input keys, MariaDB will run the UPDATE part of the statement and update existing record with specified values.
  • If the duplicate key is not identified, MariaDB will insert a new record which will be represented by the given values.

How to Insert if Not Exists in MariaDB

When managing a database, the need often arises to either insert a new record or update an existing one. MariaDB provides a powerful tool to handle this situation efficiently: the SQL IF NOT EXISTS clause. This clause allows us to perform an INSERT operation only if the record does not already exist, or an UPDATE operation if it does. In this article, we will explore how to use the SQL IF NOT EXISTS clause in MariaDB to insert a record if not present otherwise update the record with the help of examples and so on.

Similar Reads

How to INSERT Data if not Exist in MariaDB?

When working with databases, it’s common to encounter situations where we need to either insert a new record or update an existing one based on certain conditions. MariaDB provides the SQL IF NOT EXISTS clause, which allows us to perform these operations in a single statement, making our code more concise and efficient....

1. Using the INSERT IGNORE statement

INSERT IGNORE command can be used for inserting new records into a table, and duplicates won’t violate the unique constraints, thus they will be ignored....

2. On the DUPLICATE KEY UPDATE Clause

The ON DUPLICATE KEY UPDATE clause allows us to specify how MariaDB should handle duplicates when inserting new records....

3. Using Replace Statement

The REPLACE statement enables we to put a new record in a table. In case of the duplicate key, MariaDB gets rid of the old record and inserts a new one....

MariaDB Update IF NOT EXISTS

If we want to do a MariaDB update operation under such conditions (IF NOT EXISTS or WHERE NOT EXISTS), you cannot directly use the respective clauses with the UPDATE statement. On the other hand, using subqueries and conditional logic can achieve the same functionality....

Conclusion

Inserting or updating records in an early efficient is the most necessary thing in database management The inclusion of SQL IF NOT EXISTS and ON DUPLICATE KEY UPDATE by MariaDB for you to quickly to deal with tasks of inserting new records or updating the existing ones if certain condition is met. Harnessing these features, you will be able to effectively maintain data integrity, and optimize operation in your database applications....