Database.Beginner.How would you update all rows in a table to set a column value to a default?

You can use a simple UPDATE statement without a WHERE clause.

General Syntax

UPDATE table_name
SET column_name = default_value;

Without WHERE → it affects every row in the table.

With WHERE → it affects only rows matching the condition.

Example

Imagine you have a Users table:

user_idnamestatus
1AliceNULL
2BobNULL
3CharlieNULL

Now, you want to set the status column to 'Active' for all users.

✅ Here’s the SQL:

UPDATE Users
SET status = 'Active';

Result:

user_idnamestatus
1AliceActive
2BobActive
3CharlieActive

Important Points

  • Be careful: Without WHERE, it affects all rows — no filtering!
  • You can set constants, functions, or default expressions.
    • E.g., set a timestamp:
UPDATE Orders SET order_date = CURRENT_DATE;

Bonus: Reset to Default Value (if defined)

If your column has a DEFAULT value defined in the table schema, you can:

  • In PostgreSQL:
UPDATE Users
SET status = DEFAULT;

But in MySQL and SQL Server, DEFAULT in UPDATE is not supported — you have to explicitly set the default value manually.

In Short

To update all rows, just omit the WHERE clause:

All rows will be updated.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.