Saturday, 16 March 2013

SQL UPDATE Statement Syntax and Example

SQL UPDATE Statement
The UPDATE is DML statement is used to update existing records in a table.

SQL UPDATE Syntax:
UPDATE Table_Name
SET Column1=Value1, Column2=Value2,...WHERE Some_Column=Some_Value

Example:

Let us consider the following 'Employee' Table.


Emp_ID
E_Name
Dept_ID
City
Salary
1001
Ravi
1
Hyderabad
20000
1002
David
2
Bangalore
35000
1003
Victory
1
Hyderabad
50000
1004
Tamatam
3
Bangalore
25000
1005
William
2
Hyderabad
40000
1006
King
6
Bangalore
30000

Here from the above , we want to update the table by replacing the city "Hyderabad" with "Chennai". This can be done by using the following SQL Query.


UPDATE Employee
SET City= 'Chennai" WHERE  City= 'Hyderabad"
Now the out put as follows


Emp_ID
E_Name
Dept_ID
City
Salary
1001
Ravi
1
Chennai
20000
1002
David
2
Bangalore
35000
1003
Victory
1
Chennai
50000
1004
Tamatam
3
Bangalore
25000
1005
William
2
Chennai
40000
1006
King
6
Bangalore
30000

Caution:
Be careful when updating records.If you omit the Where Clause in the above example then all the records in city column of the table are set to 'Chennai'.

Key Difference between Update and Alter Statements:


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.

Update is a DML statement whereas Alter is a DDL statement. Alter command modifies the database schema, while Update statement only modifies records in a database without modifying its structure.


Thanks.,

TAMATAM

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts