MySQL provides a nice operator <=>
that works with comparisons that could contain a null such as null <=> null
or null <=> 5
etc. giving back intuitive results as many programming languages. Whereas the normal equals operator always just returns null, which catches many new MySQL users such as myself awry.
Is there a reason MySQL has both and not JUST the functionality in <=>
? Who really needs an operator that is effectively undefined with built in language types?
Who really needs an operator that is effectively undefined with built in language types?
You asked for some real-world examples. Here's a spurious one. Let's say that you have a residential youth programme or similar, and one of the requirements is that the kids only share a room with someone of the same sex. You have a nullable M/F field in your database - nullable because your data feed is incomplete (you're still chasing down some of the data). Your room-matching code should definitely not match students where t1.Gender<=>t2.Gender, because it could end up matching two kids of unknown gender, who might be of opposite genders. Instead, you match where they're equal and not both null.
That's just one example. I admit that the behaviour of NULL
and the =
operator have caused a lot of confusion over the years, but ultimately the fault probably lies with the plethora of online MySQL tutorials that make no mention of how NULL
interacts with operators, nor of the existence of the <=>
operator.
The big difference between null in mySQL and in programming languages is that in mySQL, null means unknown value while in programming it means undefined value.
In mySQL, null does not equal null (unknown does not equal unknown). While in programming languages, null does equal null (undefined equals undefined).
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.
Is there a reason MySql has both and not JUST the functionality in <=> ? The operators are completely different from each other.
<=>
performs an equality comparison like the =
operator, but returns 1
rather than NULL
if both operands are NULL
, and 0
rather than NULL
if one operand is NULL
.
Who really needs an operator that is effectively undefined with built in language types?
This depends on case, just because you haven't encountered such cases, does not mean nobody needs it.
Yes.
This must be because relational databases use the theory of three-valued logic (TRUE
, NULL
, FALSE
).
And the three-valued logic must work so because it must be internally consistent.
It follows from the rules of mathematics.
来源:https://stackoverflow.com/questions/10037298/is-there-a-reason-not-to-use-null-safe-equals-operator-in-mysql-instead-of