In my INNODB mySQL database I have some columns named like active, verified, disabled among others like name, surname.
To explain it better
Column Type Null Default
expires int(10) Yes NULL
verified tinyint(1) Yes NULL
disabled tinyint(1) Yes NULL
When a user logins into my page, I use PHP and check for example
if ($row['disabled']) { }
to know if he is disabled ( NULL or 1 ). (there are only these 2 possibilities).
For now, I set them as NULL but I thought if it is better to use 0 instead, knowing that 0 is empty in PHP too.
Concluding, my questions are two.
- Does thousands NULL records grow without a reason the DB instead of thousands 0 records?
- Does NULL or 0 affects PHP execution’s performance differently ? If so, what is the best combination of mySQL and PHP to have for the requested checks as above?
Update
On my question number 1, my question is if NULL is actually null of size, otherwise is +3 bytes right?
In mysql, NULL values mean that a particular field has no value in it or it is empty. Ideally, if you have a field like Name and Address and don’t want to assign values to them all the time, you can state that it’s default value is NULL.
But if you’re talking about only two possibilities like a True or False item, it’s best to go for the Boolean field that is most space efficient.
Going further, for fields like Booleans (eg: disabled) and dates (eg: DateJoined) it’s not advisable to keep them as null or unassigned. You can bump into problems in the future if you had to interface with another language like VB. For example, if you try to read an assigned date field via VB, you’ll see that your app crashes. But through PHP, this is not a problem. To be on the safe side, it’s best to assign a default true or false (1 or 0) value for such fields.
And finally, to CHECK for null values, the normal relational operators are not suitable. Rather, you should use the IS NULL or IS NOT NULL operator instead.
More info here: http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html