Does mysql include UPDATE values ​​if they do not exist?

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

I am wondering if the update also inserts values ​​if the where clause does not work. I read about w3schools that only update updates to existing data in the database, but in my script it automatically inserts rows with data. I am wondering if this might be a bug in the script or just how UPDATE works on mysql.

+8
mysql
source share
4 answers

Not. If in your example there is no record with id = 12 in the database, the query will return "no rows are affected." An update will never create a new record in MySQL.

EDIT: although the update will not create a new record, it may include the default values ​​/ automatic values ​​set in your database schema (for example, the current timestamp).

+12
source share

NOT. The update does not insert a value if the value does not exist in the table. Check if the script checks for update status and makes another DB call to insert data.

+3
source share

Your SQL should do the following -

Update all entries in the table of elements that have an identifier of 12 by setting their name to 'haha'

The update will not insert records, if they do not exist, it will only update existing records in the table.

+1
source share

Short answer: No.

Long answer: if your column does not exist, you will receive an error message. If your condition column does not exist, you also get an error. If your condition value does not exist, it does nothing.

0
source share

All Articles