SQL Update statement

free php tutorials, php classes, php utilities, free tools

We can update existing records in a tables using UPDATE statement.

Example:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Note: if you dont use where clause, it will update all records in a table.

Update Example:

Now we will update records in following table

ID Category Author Tutorial
1 PHP Hassan what is php
2 MySql James what is sql
3 Ajax Andrew what is ajax
4 ASP James ASP Introduction
5 C++ Andrew C++ Basics
7 CSS Williams  

update tutorials set tutorial='css introduction' where id =7

It will show following results.

ID Category Author Tutorial
1 PHP Hassan what is php
2 MySql James what is sql
3 Ajax Andrew what is ajax
4 ASP James ASP Introduction
5 C++ Andrew C++ Basics
7 CSS Williams css introduction

Note: I have hilight 'css introduction' in red color because it has updated with above mentioned query.

Be careful when updating records. If we had omitted the WHERE clause in the example above, like this:

update tutorials set tutorial='css introduction'

It will show following results.

ID Category Author Tutorial
1 PHP Hassan css introduction
2 MySql James css introduction
3 Ajax Andrew css introduction
4 ASP James css introduction
5 C++ Andrew css introduction
7 CSS Williams css introduction

We can also use update statement with multiple columns like

update tutorials set tutorial='css introduction', author='kashif', where id =7

Bookmark This Page