Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are differences between INSERT and UPDATE in MySQL?

It seems INSERT and UPDATE do the same things to me.

Is there any occasions where I should use INSERT instead of UPDATE and vice versa?

like image 707
shin Avatar asked Jan 04 '10 21:01

shin


People also ask

Which is faster insert or update MySQL?

Insertion is inserting a new key and update is updating the value of an existing key. If that is the case (a very common case) , update would be faster than insertion because update involves an indexed lookup and changing an existing value without touching the index.

Which one is faster insert or update?

Insert is more faster than update because in insert there's no checking of data.

What is the difference between insert and alter in SQL?

Insert command is used to insert a new row to an existing table, Update is a SQL command that is used to update existing records in a database, while alter is a SQL command that is used to modify, delete or add a column to an existing table in a database.

Can I insert with update MySQL?

When you insert a new row into a table if the row causes a duplicate in UNIQUE index or PRIMARY KEY , MySQL will issue an error. However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will update the existing row with the new values instead.


2 Answers

In CRUD operations, the INSERT is the 'C' and the UPDATEis the 'U'. They are two of the four basic functions of persistent storage. The other two are SELECT and DELETE. Without at least these four operations, a typical database system cannot be considered complete.

Use INSERT to insert a new record.

Use UPDATE to update an existing record.

like image 81
Daniel Vassallo Avatar answered Sep 19 '22 06:09

Daniel Vassallo


You cannot UPDATE a row that's not in a table.

You cannot INSERT a row that's already in a table.

like image 38
slebetman Avatar answered Sep 19 '22 06:09

slebetman