SQL UPDATE Statement

The SQL commands (UPDATE and DELETE) are used to modify the data that is already in the database. The SQL DELETE command uses a WHERE clause.

SQL UPDATE statement is used to change the data of the records held by tables. Which rows is to be update, it is decided by a condition. To specify condition, we use WHERE clause.

The UPDATE statement can be written in following form:

UPDATE table_name SET [column_name1= value1,... column_nameN = valueN] [WHERE condition]

Let's see the Syntax:


					 UPDATE table_name



                     SET column_name = expression



                     WHERE conditions

					

Syntax for Loop in C


	for(initialization;condition;incr/decr){ 

	//code to be executed

	}         

					

Updating Multiple Fields:

If you are going to update multiple fields, you should separate each field assignment with a comma.

SQL UPDATE statement for multiple fields:

 UPDATE students



 SET User_Name = 'beserious', First_Name = 'Johnny'



 WHERE Student_Id = '3'

MYSQL SYNTAX FOR UPDATING TABLE:


					UPDATE table_name

					SET field1=new value1, field2=new-value2,

					[WHERE CLAUSE]

					

We can use SELECT statement to update records through UPDATE statement.


					 UPDATE tableDestination



 SET tableDestination.col = value



 WHERE EXISTS (



 SELECT col2.value



 FROM tblSource



 WHERE tblSource.join_col = tblDestination. Join_col



 AND tblSource.Constraint = value)

					

My SQL SYNTAX:

If you want to UPDATE with SELECT in My SQL, you can use this syntax:

Let's take an example having two tables. Here,

First table contains -

Cat_id, cat_name,

And the second table contains -

Rel_cat_id, rel_cat_name

SQL UPDATE COLUMN:

We can update a single or multiple columns in SQL with SQL UPDATE query.