Update Table SQL

Galaxy Glossary

How do you modify existing data in a SQL table?

The UPDATE statement in SQL is used to modify existing data within a table. It allows you to change values in specific rows based on conditions. This is a fundamental operation for maintaining and updating data in a database.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The UPDATE statement is a crucial part of any SQL developer's toolkit. It allows you to change existing data in a table. This is essential for keeping your database accurate and up-to-date. You can update individual columns or multiple columns in a single statement. The power of UPDATE lies in its ability to target specific rows using WHERE clauses, ensuring that only the desired data is modified. This targeted approach prevents unintended changes to other parts of your database. For instance, you might update customer addresses, product prices, or order statuses using UPDATE. The WHERE clause is critical for specifying which rows to update. Without it, you'd risk updating every row in the table, which is usually not the desired outcome.

Why Update Table SQL is important

Updating data is a fundamental aspect of database management. It allows you to reflect changes in the real world in your database. Without UPDATE, databases would quickly become outdated and inaccurate, hindering their usefulness for reporting and decision-making.

Example Usage


-- Example table: Customers
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    City VARCHAR(50)
);

-- Insert some sample data
INSERT INTO Customers (CustomerID, FirstName, LastName, City) VALUES
(1, 'John', 'Doe', 'New York'),
(2, 'Jane', 'Smith', 'Los Angeles'),
(3, 'Peter', 'Jones', 'Chicago');

-- Update the city for customer with CustomerID 2
UPDATE Customers
SET City = 'San Francisco'
WHERE CustomerID = 2;

-- Verify the update
SELECT * FROM Customers;

Common Mistakes

Want to learn about other SQL terms?