SQL - Data modification - Part 7 - Updating a table in SQL
Haziran 10, 2013 by sql tutorial
|
|
Article Information
This SQL Tutorial contains and tries to cover following subjects:
- Explanation of Updating a table in SQL
- Explanation of sql update in SQL
- Example to update a table in SQL
Articles tries to provide answer to following questions
- How to update a update a table in SQL
- How to update a row in SQL?
- How to change value of a column in table?
- How to use sql update?
Article covers followings indirectly:
- WHERE clause
Articles pre-requisites following information:
- General knowledge of SQL Server Management Studio
- General knowledge of WHERE clause
Updating a Table
In SQL, to update a table, sql update is used. SQL UPDATE command has a basic form, as well as advanced forms to update a table according to different need. In this article, we will cover its basic form, sql update syntax and important things about UPDATE command.
UPDATE command syntax is as follows:
Lets see basic sql update syntax:
update table1 set column1 = value
UPDATE customers SET customerName = 'Microsoft'
As above syntax indicates, table name is added after update command. After the table name, we provide the SET and the column name. That is basic form of SQL update syntax. That sql update command in its basic form updates entire column with the given value. If we use above command without a WHERE clause, all rows and customer names will be Microsoft.
However, in most cases, the update is applied with limiting. For example, we want to update a row in a column, we will need to add WHERE filter to update only the row we want. Lets see updating a specific column with adding WHERE filter. SQL update syntax is as follows:
UPDATE customers SET customerName = 'Microsoft' WHERE customerID = 1
In above sql update, sql server will look for the row where customerID is 1, and it will update the customerName column to given value.
Before using update command, it is essential to check a WHERE clause. Otherwise, without a WHERE, entire column and data can be wiped out.
Example of Updating a Table in SQL
As above sql update example demonstrates, a row in the table has been updated. To update only one row, update command has been limited with filtering out all records which are not match to WHERE condition.
In next sql tutorial, we will look into more advanced sql update commands.
Data Layers
Area: | programming \ languages \ tsql \ \ \ |
Ref: | |
Loc: | articles |
Tags: | tsql |
|