SQL Update

The SQL UPDATE statement is used when you want to update record(s) in your table.

SQL UPDATE Syntax

UPDATE “table_name”
SET “column_name” = “value”, “column_name1″ = “value1″
WHERE “condition”

SQL UPDATE Examples

To demonstrate the above example assume that we have the following table called Games

ID Title Release Date Genres Platform
1001 GTA IV April 29, 2008 Action-adventure PS3,XBOX360
1002 Gran Turismo 5 November 24, 2010 Racing PS3
1003 Assassin’s Creed November 14, 2007 Third person PS3,XBOX360
1004 Battlefield: Bad Company June 26, 2008 First person shooter PS3,XBOX360
1005 LittleBigPlanet October 01, 2008 Puzzle platformer PS3
1006 Fifa 11 October 01, 2010 Sports PS3,XBOX360,WII
1007 LittleBigPlanet 2 January 21, 2011 Adventure PS3,XBOX360
1008 Dead Space 2 January 28, 2011 Third person PS3,XBOX360
1009 Mindjack January 28, 2011 Third person XBOX360
1010 Mario Sports Mix Febuary 04, 2011 Adventure WII

To UPDATE a specific column for a specific record in the Games table we would use the following query:

UPDATE Games
SET Platform = “PS3,XBOX360,PC”
WHERE ID=1001

Once executed the resultset would look like this:

ID Title Release Date Genres Platform
1001 GTA IV April 29, 2008 Action-adventure PS3,XBOX360,PC
1002 Gran Turismo 5 November 24, 2010 Racing PS3
1003 Assassin’s Creed November 14, 2007 Third person PS3,XBOX360
1004 Battlefield: Bad Company June 26, 2008 First person shooter PS3,XBOX360
1005 LittleBigPlanet October 01, 2008 Puzzle platformer PS3
1006 Fifa 11 October 01, 2010 Sports PS3,XBOX360,WII
1007 LittleBigPlanet 2 January 21, 2011 Adventure PS3,XBOX360
1008 Dead Space 2 January 28, 2011 Third person PS3,XBOX360
1009 Mindjack January 28, 2011 Third person XBOX360
1010 Mario Sports Mix Febuary 04, 2011 Adventure WII

Now if you wanted to do an update all the records in the Games table you would simply omit the SQL WHERE statement on the SQL query like this:

UPDATE Games
SET Platform = “PS3,XBOX360,PC,WII”

Once executed the resultset would look like this:

ID Title Release Date Genres Platform
1001 GTA IV April 29, 2008 Action-adventure PS3,XBOX360,PC,WII
1002 Gran Turismo 5 November 24, 2010 Racing PS3,XBOX360,PC,WII
1003 Assassin’s Creed November 14, 2007 Third person PS3,XBOX360,PC,WII
1004 Battlefield: Bad Company June 26, 2008 First person shooter PS3,XBOX360,PC,WII
1005 LittleBigPlanet October 01, 2008 Puzzle platformer PS3,XBOX360,PC,WII
1006 Fifa 11 October 01, 2010 Sports PS3,XBOX360,PC,WII
1007 LittleBigPlanet 2 January 21, 2011 Adventure PS3,XBOX360,PC,WII
1008 Dead Space 2 January 28, 2011 Third person PS3,XBOX360,PC,WII
1009 Mindjack January 28, 2011 Third person PS3,XBOX360,PC,WII
1010 Mario Sports Mix Febuary 04, 2011 Adventure PS3,XBOX360,PC,WII

Give it a go and once you feel comfortable using the SQL UPDATE keyword move on to our next tutorial SQL DELETE.