SQL: Update

This entry is part 14 of 20 in the series SQL Tutorial

Update allow us to modify data in a table.

In the achronym CRUD, we have looked at Create (insert), Retrieve (select).  Next in the list will be Update (update).

In sqlfiddle.com, we put insert statement on the left box.  The same goes to update and delete statement.

Syntax: update table set column = {value} where condition

create table artiste
 id int auto_increment primary key, 
 artiste varchar(30), 
 album varchar(50),
 year int(10) 

insert into artiste 
(artiste, album, year)
('Rod Stewart', 'Maggie May', 1973),
('New Order', 'Substance', 1987),
('Nirvana', 'Smell Like Teen Spirit', 1992),
('Rod Stewart', 'Out of Order', 1988);

update artiste set album='Every Picture Tells A Story' where artiste='Rod Stewart';


If the where clause is omitted in the update command, ALL ROWS will be updated.

Hence, it is a good practice to issue a select query, using the same criteria, to check the result set before issuing the update. This also applies to the delete statement in the upcoming posts.

The SQL fiddle without setting the where clause can be seen here.

Series Navigation<< SQL: char data type
SQL: Delete >>

Leave a comment

Leave a Reply