Of Types and ComparisonsSubmitted by Matthew Turland on Wed, 01/14/2009 - 09:38
While in the #phpc IRC channel on the Freenode network recently, I got into a discussion with Maggie Nelson regarding database data types. In particular, she mentioned that the databases we work with on a regular basis, namely MySQL and Oracle, don't have an equivalent reference to the types comparison section of the PHP manual and that it handy to have cheat sheets for them.
I've discussed this topic a few times in various forms on my own blog including various data type systems, an offhand mention of lack of boolean support in Oracle functions, and treatment of NULL in Oracle and MySQL. While the Data Types and Type Conversion sections of the MySQL manual and the Datatypes and Datatype Comparison Rules sections of the Oracle manual are excellent equivalents for the Types and Type Juggling sections of the PHP manual, I think Maggie has a point: it would be nice to have a resource that makes it easier to find this information at a glance. So, I decided to give it a go in this blog post. One very handy reference to supplement my experimentation was a comparison of various SQL implementations.
These tests were performed on Oracle 10gR2 using queries of the form shown below. An "E" indicates that no result was returned because an error was encountered. Unlike the PHP type comparison table, this table excludes boolean values because Oracle doesn't have a boolean data type. And yes, I know the tables are symmetric; a table is simply easier to reference from a visual standpoint than a list.
SELECT COUNT(*) FROM DUAL WHERE X = Y
- Numbers and strings comply with the law of identity (0 = 0, 'oracle' = 'oracle').
- A number and the string equivalent of that number are equal. (1 = '1')
- NULL and '' are synonymous and not equal to anything, even themselves. (That is, they violate the law of identity.)
- Comparing a number to a non-empty string will result in an error. (ORA-01722: invalid number)
These tests were performed on MySQL 5.0.67 (Ubuntu package) using queries of the form shown below. As in the Oracle table, an "E" indicates that the operation resulted in an error. Unlike Oracle, MySQL will return NULL in some of the cases shown below. As such, an "N" result in the table below indicates that the operation had a NULL result.
SELECT X = Y
- As with Oracle, strings and numbers comply with the law of identity.
- As with Oracle, numbers and their string equivalents are equal.
- 0, '', and FALSE are equal.
- 0 and FALSE are equal to non-empty non-numeric strings, but such comparisons cause warnings to be emitted.
- 1 and TRUE are equal. Because 1 and '1' and equal, TRUE and '1' are also equal. By the same principle, FALSE and '0' are equal.
- Any comparison involving NULL results in NULL.