Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does mysql UPDATE inserts the values if it doesn't exist?

Tags:

mysql

UPDATE items SET name = 'haha' WHERE id = '12'

I'm curious if update also inserts the values if the where condition fails. I've read on w3schools that update only updates existing data on the database but on my script it's automatically inserting rows with the data. I am wondering if it might be a bug in the script or that's just how UPDATE works on mysql.

like image 396
Zhianc Avatar asked Oct 27 '11 10:10

Zhianc


2 Answers

No. If, in your example, there's no entry with id = 12 in the database, the query will return "no rows affected". An update will never create a new entry in MySQL.

EDIT: although update won't create a new entry, it may include default/automatic values set up in your database schema (current timestamp, for instance).

like image 192
Rodrigo Ferreira Avatar answered Oct 19 '22 16:10

Rodrigo Ferreira


NO. Update does not insert a value if the value doesn't exist in table. Please check if the script checks if the status of the update and makes another call to DB to insert the data.

like image 26
Vivek Viswanathan Avatar answered Oct 19 '22 17:10

Vivek Viswanathan