Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

后端 未结 5 2273
自闭症患者
自闭症患者 2021-02-13 16:47

MySQL provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 17:02

    Who really needs an operator that is effectively undefined with built in language types?

    you also need it for relations inside your database. specially if you are using foreign key constraints.

    for example if you have a table for tasks (in your company). then you assign these tasks to employees. so you have a relation from your tasks-table to your employees-table.
    and there will always be some unassigned tasks. in this case the field in your tasks-table you use for the relation to the employees table will contain NULL. this will make sure, that this task is unassigned. which means: there is no possibility that there is a relation to the employees table.

    if NULL = NULL would be true, then in my example there would be always the possibility that the foreign key in the employees table also is NULL. thus the task would be assigned to one or some employees. and you never would be able to know for sure wheter a task is assigned to some employee or not.

提交回复
热议问题