The previous lesson of MySql Lessons series was about MySql Triggers and History Table and in this lesson we will learn about MySql Upsert Query.
![MySql Upsert Query MySql Upsert Query](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbuE_86lr8dkQYqFCRbiBEy0CGxs5aqOY6HX9BIyhCjwqHYvA4wlAYhgqbvMYPjpivUG0BHtKFaLTgFnS6B2rAOysV0_7Y0t3XrE9lf8An_9uj4mMOf3bI4O83E9MxU8d4p90qFyUdjg/s1600/mysql-upsert.jpg)
MySql Upsert Statement is used to insert a new row in a table if a duplicate key does not exist and update an existing row if a duplicate key exists. To elaborate the functionality of MySql Upsert Statement consider the `user` table given in the image below:
![Upsert User Table Upsert User Table](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA-cgV6JUGoVoo8apwYGh-ClEHLw2FYqmVMgNhtoXQNClq6MtiWWTa3DU17MflzDgUU0u-GhNTGAzX8bHCO-LugSo40h0gAdrNlCT4F8Z1FYLhdCfbyOmEPAGPc3Z7ljgIID1dhIuTdg/s1600/upsert-user.jpg)
The above `user` table contains (id, name, username, email, password) of a user. We want to insert a new row in the `user` table if the `username` and `email` does not exist in the table already and update the password of the user if the `username` or `email` already exists in the table. The first thing we need to do to achieve this is make the `username` and `email` fields UNIQUE. MySql queries to make the `username` and `email` fields UNIQUE are given below:
Now that we have UNIQUE `username` and `email` fields in our `user` table, we can write MySql Upsert query to achieve our goal.
The above query will update the password of user (John Malcom) as the `user` table already contains `username` john123 and `email` john123@gmail.com
![Upsert Result Upsert Result](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0McCehNmvbOp_JcHjGXSHhGFaXNpEWaAP2x2PyxcNhxWjqK6giWUmdijrXpVBKwZYGEZ3G1rWOaBO-bF0djNYwvYjdYwxxtsxoUPAymAHrd9Mxb8E5fPGn4X9QdUlZ_wa8p9VsX15Zg/s1600/upsert-user-result.jpg)
Note that if the `username` and `email` fields are not UNIQUE for the `user` table, the above query will insert a new record and will not update the already existing data against user (John Malcom). Similarly the upsert query given below will insert a new record as the `username` john1234 and `email` john1234@gmail.com are UNIQUE in the table `user`.
Stay tuned for more lessons...
![MySql Upsert Query MySql Upsert Query](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbuE_86lr8dkQYqFCRbiBEy0CGxs5aqOY6HX9BIyhCjwqHYvA4wlAYhgqbvMYPjpivUG0BHtKFaLTgFnS6B2rAOysV0_7Y0t3XrE9lf8An_9uj4mMOf3bI4O83E9MxU8d4p90qFyUdjg/s1600/mysql-upsert.jpg)
MySql Upsert Statement is used to insert a new row in a table if a duplicate key does not exist and update an existing row if a duplicate key exists. To elaborate the functionality of MySql Upsert Statement consider the `user` table given in the image below:
![Upsert User Table Upsert User Table](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA-cgV6JUGoVoo8apwYGh-ClEHLw2FYqmVMgNhtoXQNClq6MtiWWTa3DU17MflzDgUU0u-GhNTGAzX8bHCO-LugSo40h0gAdrNlCT4F8Z1FYLhdCfbyOmEPAGPc3Z7ljgIID1dhIuTdg/s1600/upsert-user.jpg)
The above `user` table contains (id, name, username, email, password) of a user. We want to insert a new row in the `user` table if the `username` and `email` does not exist in the table already and update the password of the user if the `username` or `email` already exists in the table. The first thing we need to do to achieve this is make the `username` and `email` fields UNIQUE. MySql queries to make the `username` and `email` fields UNIQUE are given below:
ALTER TABLE `user` ADD UNIQUE(`username`);
ALTER TABLE `user` ADD UNIQUE(`email`);
Now that we have UNIQUE `username` and `email` fields in our `user` table, we can write MySql Upsert query to achieve our goal.
INSERT INTO `user` (`id`,`name`,`username`,`email`,`password`)
VALUES (4,'John Malcom','john123','john123@gmail.com','12345abc')
ON DUPLICATE KEY UPDATE `password` = 'newpass123';
The above query will update the password of user (John Malcom) as the `user` table already contains `username` john123 and `email` john123@gmail.com
![Upsert Result Upsert Result](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0McCehNmvbOp_JcHjGXSHhGFaXNpEWaAP2x2PyxcNhxWjqK6giWUmdijrXpVBKwZYGEZ3G1rWOaBO-bF0djNYwvYjdYwxxtsxoUPAymAHrd9Mxb8E5fPGn4X9QdUlZ_wa8p9VsX15Zg/s1600/upsert-user-result.jpg)
Note that if the `username` and `email` fields are not UNIQUE for the `user` table, the above query will insert a new record and will not update the already existing data against user (John Malcom). Similarly the upsert query given below will insert a new record as the `username` john1234 and `email` john1234@gmail.com are UNIQUE in the table `user`.
INSERT INTO `user` (`id`,`name`,`username`,`email`,`password`)
VALUES (4,'John Malcom','john1234','john1234@gmail.com','12345abc')
ON DUPLICATE KEY UPDATE `password` = 'newpass123'
Stay tuned for more lessons...
No comments:
Post a Comment