![]() You can do so by using the SQL UPDATE command. We hope this article helped you with insert or update related queries.There may be a requirement where the existing data in a MySQL table needs to be modified. We already have the row with customer_name = “Rasmus,” Hence, we get an action output message.Īction Output Message : 17:22:35 INSERT INTO customer_data (customer_id, customer_name, customer_place) SELECT * FROM (SELECT 6, “Rasmus”,”TestPlace”) AS tmp_name WHERE NOT EXISTS ( SELECT customer_name FROM customer_data WHERE customer_name = “Rasmus” ) LIMIT 1 0 row(s) affected Records: 0 Duplicates: 0 Warnings: 0 0.00047 sec.Ġ rows affected, so nothing new gets inserted. To have a better understanding of the concept, let us re-run the same query, this time. Since we do not yet have a row with customer_id = 6 the query will insert a new row on running this query. SELECT customer_name FROM customer_data WHERE customer_name = "Rasmus" SELECT * FROM (SELECT 6, "Rasmus","TestPlace") AS tmp_name INSERT INTO customer_data (customer_id, customer_name, customer_place) Example: We want to add one more row to our table customer_data ( refer figure 1.1) or update based on customer_name. In this section, we will be using the NOT EXISTS keyword. MySQL Insert or Update conditional : NOT EXISTS Read more about REPLACE in the official documentation. Also, the action message output said 2 rows affected, which means one got deleted before inserting the other. It shows that customer_name and customer_place got changed in the row with customer_id=2. REPLACE INTO customer_data(customer_id, customer_name, customer_place) VALUES(2, "Hevika","Atlanta") Īction Output Message : 18:27:57 REPLACE INTO customer_data (customer_id, customer_name, customer_place) VALUES(2, “Hevika”,”Atlanta”) 2 row(s) affected 0.0023 secĭoing a select * again on customer_data gives the data as shown in figure 1.5. Observe the below query with an action output message received after running it. We will be using the same example from customer_data table. The difference is: If the new row to be inserted has the same value of the PRIMARY KEY or the UNIQUE index as the existing row, in that case, the old row gets deleted first before inserting the new one. Insert or Update into MySQL Table : using REPLACE INTO It says 2 rows affected as ON DUPLICATE KEY UPDATE displays the affected-rows value: VALUES(2, "Vaani","Denver") ON DUPLICATE KEY UPDATE customer_name = "Hevika", customer_place = "Denver" Īction Output Message : 14:26:12 INSERT INTO customer_data (customer_id, customer_name, customer_place) VALUES(2, “Vaani”,”Denver”) ON DUPLICATE KEY UPDATE customer_name = “Hevika”, customer_place = “Denver” 2 row(s) affected 0.0037 sec Notice the query and the action output message. Let us now see what difference it makes using ON DUPLICATE KEY UPDATE. Duplicate entry ‘2’ for key ‘customer_data.customer_id’ 0.00047 sec INSERT INTO customer_data (customer_id, customer_name, customer_place) VALUES (2, "Vaani","Denver") Īction Output Message : 23:39:39 INSERT INTO customer_data (customer_id, customer_name, customer_place) VALUES(2, “Vaani”,”Denver”) Error Code: 1062. ![]() Using the classic insert statement, we will be getting an error, observe the query and the action output message. Figure 1.1 shows that this already exists. ![]() Now let’s say we want to insert the row with customer_id = 2. Insert or Update into MySQL Table : using On Duplicate Key Update Remove First and Last elements from Python List.Remove elements from List using pop() method in Python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |