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_NO | TITLE | AUTHORFIRSTNAME | AUTHORLASTNAME |
---|---|---|---|
181 | The Castle | Franz | Kalka |
191 | Animal Farm | George | Orwell |
205 | Madhushala | Thuglaq | Rai |
209 | Historica | Herodotus | Herodotus |
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_NO | TITLE | AUTHORFIRSTNAME | AUTHORLASTNAME |
---|---|---|---|
181 | The Castle | Franz | Kalka |
191 | Animal Farm | George | Orwell |
205 | Madhushala | Girish | Karnad |
209 | Historica | Herodotus | Herodotus |