MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • SQL & NoSQL Databases
  • 1. Relational Database Fundamentals
  • 2. SQL: Basic Data Manipulation (DML)
    • Introduction to SQL
    • CRUD Introduction
    • Common Operations Cheat Sheet
    • Read Data (SELECT)
    • Create Data (INSERT)
    • Update Data (UPDATE)
    • Delete Data (DELETE)
    • Transactions (COMMIT, ROLLBACK)
  • 3. SQL: Filtering and Sorting
  • 4. SQL: Aggregation and Relations
  • 5. SQL: Schema Management (DDL)
  • 6. Python and SQL
  • 7. NoSQL Databases
  • References

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
Privacy Policy | Terms & Conditions