186

I am using mysql and need to update a column with a null value. I have tried this many different ways and the best I have gotten is an empty string.

Is there a special syntax to do this?

linuxbuild
  • 15,843
  • 6
  • 60
  • 87
jim
  • 1,863
  • 2
  • 11
  • 4

14 Answers14

282

No special syntax:

CREATE TABLE your_table (some_id int, your_column varchar(100));

INSERT INTO your_table VALUES (1, 'Hello');

UPDATE your_table
SET    your_column = NULL
WHERE  some_id = 1;

SELECT * FROM your_table WHERE your_column IS NULL;
+---------+-------------+
| some_id | your_column |
+---------+-------------+
|       1 | NULL        |
+---------+-------------+
1 row in set (0.00 sec)
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 1
    Thanks Daniel. I'm kind of convinced that this issue may lie in my application code – jim Oct 06 '10 at 08:19
59

NULL is a special value in SQL. So to null a property, do this:

UPDATE table SET column = NULL;
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Now, let me clarify. Through php I am using PDO and updating through a query. – jim Oct 06 '10 at 08:13
  • 2
    The MySQL manual states that if the column does not allow NULL values, setting it to NULL will result in the default value for the data type (e.g. an empty string). Are you 100% that your column allows NULL? Can you show us the CREATE TABLE? –  Oct 06 '10 at 08:15
  • I am 1000% sure. I've checked it twice. Let me look one more time. – jim Oct 06 '10 at 08:16
  • Yes, it is in fact null. I can see the NULL produced by my frnt end software in the data that should be null. – jim Oct 06 '10 at 08:17
27

Use IS instead of = This will solve your problem example syntax:

UPDATE studentdetails
SET contactnumber = 9098979690
WHERE contactnumber IS NULL;
General Failure
  • 2,421
  • 4
  • 23
  • 49
13

In the above answers, many ways and repetitions have been suggested for the same. I kept looking for an answer as mentioned is the question but couldn't find here.

Another way to put the above question "update a column with a null value" could be "UPDATE ALL THE ROWS IN THE COLUMN TO NULL"

In such a situation following works

update table_name
set field_name = NULL
where field_name is not NULL;

is as well is not works in mysql

Nitish Kumar Pal
  • 2,738
  • 3
  • 18
  • 23
  • You don't need the "where ..." clause. If a row already has null, and just use the first two lines of your show code, it will run just as fast, probably faster. Rows that already have null, simply won't be written back to DB, because there is no change. – ToolmakerSteve Aug 31 '20 at 23:35
8

Remember to look if your column can be null. You can do that using

mysql> desc my_table;

If your column cannot be null, when you set the value to null it will be the cast value to it.

Here a example

mysql> create table example ( age int not null, name varchar(100) not null );
mysql> insert into example values ( null, "without num" ), ( 2 , null );
mysql> select * from example;
+-----+-------------+
| age | name        |
+-----+-------------+
|   0 | without num |
|   2 |             |
+-----+-------------+
2 rows in set (0.00 sec)

mysql> select * from example where age is null or name is null;
Empty set (0.00 sec)
Thiago Mata
  • 2,825
  • 33
  • 32
4

For those facing a similar issue, I found that when 'simulating' a SET = NULL query, PHPMyAdmin would throw an error. It's a red herring.. just run the query and all will be well.

Phiter
  • 14,570
  • 14
  • 50
  • 84
  • Thanks, I was here looking because PHPMyAdmin claimed there was an unrecognised keyword near NULL. After reading your answer, I ran the query and it executed as intended. – dading84 Aug 02 '16 at 10:06
2

If you want to set null value using update query set column value to NULL (without quotes) update tablename set columnname = NULL

However, if you are directly editing field value inside mysql workbench then use (Esc + del) keystroke to insert null value into selected column

1

use is instead of =

Eg: Select * from table_name where column is null

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
1

Another possible reason for the empty string, rather than a true null is that the field is an index or is part of an index. This happened to me: using phpMyAdmin, I edited the structure of a field in one of my tables to allow NULLs by checking the "Null" checkbox then hitting the "Save" button. "Table pricing has been altered successfully" was displayed so I assumed that the change happened -- it didn't. After doing an UPDATE to set all of those fields to NULL, they were, instead, set to empty strings, so I took a look at the table structure again and saw that the "Null" column for that field was set to 'no'. That's when I realized that the field was part of the Primary key!

ReverseEMF
  • 506
  • 7
  • 10
1

if you set NULL for all records try this:

UPDATE `table_name` SET `column_you_want_set_null`= NULL

OR just set NULL for special records use WHERE

UPDATE `table_name` SET `column_you_want_set_null`= NULL WHERE `column_name` = 'column_value' 
mamal
  • 1,791
  • 20
  • 14
0

if you follow

UPDATE table SET name = NULL

then name is "" not NULL IN MYSQL means your query

SELECT * FROM table WHERE name = NULL not work or disappoint yourself

0

On insert we can use

$arrEntity=$entity->toArray();        
    foreach ($arrEntity as $key => $value) {    
        if (trim($entity->$key) == '' && !is_null($entity->$key) && !is_bool($entity->$key)){
        unset($entity->$key);
        }
    }

On update we can use

$fields=array();
foreach ($fields as $key => $value) {
        if (trim($value) == '' && !is_null($value) && !is_bool($value)){
            $fields[$key] = null;
        }
    }
Balasaheb Bhise
  • 235
  • 1
  • 7
0

To set as NULL on a column you need to have the column to allow NULL first, otherwise you'll definitely get error.

To adit the column just run this syntax

ALTER TABLE [table_name] CHANGE [column_name] [column_name] VARCHAR(10) NULL DEFAULT NULL
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '23 at 05:18
-1

I suspect the problem here is that quotes were entered as literals in your string value. You can set these columns to null using:

UPDATE table SET col=NULL WHERE length(col)<3;

You should of course first check that these values are indeed "" with something like:

SELECT DISTINCT(col) FROM table WHERE length(col)<3;
Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
Dylan
  • 1