views:

584

answers:

8

what does "delete from table where NULL = NULL" means ?

+32  A: 

That will delete nothing from the table. NULL does not equal NULL.

Now

delete from table where NULL is NULL

would delete all rows from the table.

Coxy
You just blew my mind.
Marc Bollinger
Ah, SQL... gotta love it.
Thanatos
+1 for NULL != NULL. A bit more explanation, since I like this distinction. In database terms, NULL can mean a lack of information -- i.e. not provided, for whatever reason -- or unknown information, but you don't know which. Therefore, no two NULLs are alike.
George Marian
Right, and two NULLs are also not unalike. If you don't know the values, you can't say for sure whether they're the same or whether they're different. Hence any comparison yields *unknown* which is not *false* but it's definitely not *true* either.
Bill Karwin
+1 But with one caveat. It is so only in standard SQL and not all RDBMS provide the same standard dialect of SQL. I didn't post it as an answer, because the question is tagged _mysql_, but in MS SQL Server if you first issue `set ansi_nulls off` then `delete from table when null = null` will delete all rows because with this switch `null = null` becomes true.
Tomek Szpakowicz
+2  A: 

I imagine it depends on the database, but to my knowledge, it shouldn't achieve anything, as NULL is never equal to NULL, at least in db theory.

Alex Humphrey
+3  A: 

Since NULL does not equal NULL, this statement will do nothing. It equals:

DELETE FROM TABLE WHERE 0
Konerak
+14  A: 

It means don't delete anything, because NULL is never equal to anything. Or maybe it means "don't delete anything unless the user's DBMS really sucks, in which case delete it all out of spite".

Seriously though, that kind of construct usually comes about when a WHERE clause is procedurally generated -- rather than creating a special case for "do nothing", sometimes it's simpler just to generate a WHERE clause that causes the database to do nothing. I've usually seen "WHERE 0 = 1" though, which is less ambiguous.

hobbs
+1 for the sucking DBMS comment
Anax
+7  A: 

In SQL, there are three logical values, namely TRUE, FALSE, and UNKNOWN. when we compare null to null, using null=null, the operation will return UNKNOWN. Moreover,In the WHERE clause all UNKNOWN values are filtered out.Hence the query does nothing.

DrakeVN
+1  A: 

Recap:

mysql> select null = null, null <> null, null is null, null = 1, null <> 1;
+-------------+--------------+--------------+----------+-----------+
| null = null | null <> null | null is null | null = 1 | null <> 1 |
+-------------+--------------+--------------+----------+-----------+
|        NULL |         NULL |            1 |     NULL |      NULL |
+-------------+--------------+--------------+----------+-----------+
1 row in set (0.00 sec)


mysql> select count(*) from table;
+----------+
| count(*) |
+----------+
|     10 |
+----------+
1 row in set (0.33 sec)


mysql> select * from table where null;
Empty set (0.00 sec)

Meaning, if a condition evaluates to null it is considered false by MySql so delete from table where NULL = NULL will in fact delete nothing.

NULL is a special beast as noted by Codd

Sam Saffron
isn't it a bit confusing to state that null is considered false (since NOT null would then have to be true, which isn't the case)?
potatopeelings
yes its confusing, but im specifically talking about conditions here. but the whole null thing is super confusing.
Sam Saffron
null is not considered false, but also it's not considered true. WHERE checks if expression evaluates to true.
Michał Piaskowski
Null represents the fact that you don't know a value. You can't say that something you don't know equals another thing that you don't know. Likewise, you can't say that that those two things don't equal one another. Strictly speaking NULL = NULL should evaluate to NULL because you don't know whether those two things are equal or not. False however is good enough for most cases.
Alex Humphrey
A: 

No one row is affected by this sql.

Sandeep Patle
+2  A: 

It punishes people who have ANSI_NULLS set to off in their database :)

Star