Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting value for one column of all records in table

Tags:

sql

mysql

People also ask

How do you update a column to all rows in SQL?

Syntax: UPDATE table_name SET column_name1 = new_value1, column_name2 = new_value2 ---- WHERE condition; Here table_name is the name of the table, column_name is the column whose value you want to update, new_value is the updated value, WHERE is used to filter for specific data.

Could I make a column in a table only allows one true value and all other rows should be false?

Yep. And it would be possible to use a check constraint to ensure that only one row exists (using Celko's technique here sqlmonster.com/Uwe/Forum.aspx/ms-sql-server/3453/…)


UPDATE your_table SET likes = NULL

or if your likes column does not allow NULL:

UPDATE your_table SET likes = ''

Some SQL tools that are used for executing DB queries prevent updates on ALL records (queries without a where clause) by default. You can configure that and remove that savety setting or you can add a where clause that is true for all records and update all anyway like this:

UPDATE your_table 
SET likes = NULL
WHERE 1 = 1

If you compare with NULL then you also need the IS operator. Example:

UPDATE your_table 
SET likes = NULL
WHERE likes IS NOT NULL

because comparing NULL with the equal operator (=) returns UNKNOWN. But the IS operator can handle NULL.