Update Data (UPDATE)
The Update operation in CRUD is performed using the UPDATE statement in SQL. It is used to modify existing records in a table.
Database Preparation
Run this block to set up the Authors table.
DROP TABLE IF EXISTS Authors;
CREATE TABLE Authors (
AuthorID INTEGER PRIMARY KEY,
AuthorName VARCHAR(100),
Nationality VARCHAR(50)
);
INSERT INTO Authors (AuthorID, AuthorName, Nationality)
VALUES (101, 'J.R.R. Tolkien', 'British'),
(102, 'George Orwell', 'British');
SELECT * FROM Authors;
UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Caution: The WHERE clause is critical. If you omit it, all records in the table will be updated!
Example
Operation: Correct George Orwell's nationality to reflect that he was born in British India.
Authors Table Before:
| AuthorID | AuthorName | Nationality |
|---|---|---|
| 102 | George Orwell | British |
| Query: |
UPDATE Authors
SET Nationality = 'British (Born in India)'
WHERE AuthorID = 102;
-- Check the result
SELECT * FROM Authors;
Authors Table After:
| AuthorID | AuthorName | Nationality |
|---|---|---|
| 102 | George Orwell | British (Born in India) |
Hands-on Exercise
Task: Update Author 101's name to 'John Ronald Reuel Tolkien'.
Your Query:
-- Write your query here
Expected Results:
| AuthorID | AuthorName | Nationality |
|---|---|---|
| 101 | John Ronald Reuel Tolkien | British |
| 102 | George Orwell | British |