SQL Update – Updating Single and Multiple Columns in a Table

SQL Update table examples for single and multiple columns – Learn how to Update existing records in a table in MySQL database with example.

SQL update used to update the existing records in a database table or a view’s base table.

SQL syntax for updating records in the database table

Syntax:

Update [table_name]

set [column1=expression1],

[column2= expression2],

[where condition]

Syntax for update statement when  updating one database table  from another database table

update table_name

set column1=(select expression1 from table2 where <conditions>)

[where < conditions>]]

syntax for update statement when updating multiple tables is.

Update table_name1, table_name2

set column1=expression1,

column2= expression2,

….

where table_name1.column= table_name2.column

and conditions.

UPDATING SINGLE COLUMN IN THE DATABASE TABLE

SQL query to update a single column in a BOOKS table.

Query example:

UPDATE BOOKS

SET   AUTHORFIRSTNAME=’Thuglaq’

WHERE ISBN_NO=’205′;

The example would update the  AUTHORFIRSTNAME to   ‘Thuglaq’   where   in the books table   where the ISBN_NO is 205′;

SELECT *    FROM BOOKS;

Output:

ISBN_NOTITLEAUTHORFIRSTNAMEAUTHORLASTNAME
181The CastleFranzKalka
191Animal FarmGeorgeOrwell
205MadhushalaThuglaqRai
209HistoricaHerodotusHerodotus

 

UPDATING MULTIPLE COLUMNS IN THE DATABASE TABLE

SQL query to update multiple columns in a database table.

UPDATE BOOKS

SET AUTHORFIRSTNAME=’Girish’,

AUTHORLASTNAME=’Karnad’

WHERE ISBN_NO=’205′;

The example would update the  AUTHORFIRSTNAME to   ‘Girish’  and   AUTHORLASTNAME to ‘    ‘Karnad’ where   in the books table   where the ISBN_NO is 205′;

output:

select *  from books;

ISBN_NOTITLEAUTHORFIRSTNAMEAUTHORLASTNAME
181The CastleFranzKalka
191Animal FarmGeorgeOrwell
205MadhushalaGirishKarnad
209HistoricaHerodotusHerodotus

Related Posts