what does “delete from table where NULL = NULL” means?

青春壹個敷衍的年華 提交于 2019-12-05 12:11:59

问题


What does delete from table where NULL = NULL mean?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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

DELETE FROM TABLE WHERE 0



回答6:


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




回答7:


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.




回答8:


Not a single row is affected by this SQL command.



来源:https://stackoverflow.com/questions/3201192/what-does-delete-from-table-where-null-null-means

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!