I haven't used CHAR much in the past as I seemed to use VARCHAR too much; I'm trying to use CHAR when appropriate lately & from what I understand you use it when the data is all the same length in that certain column (else it is padded with spaces).
Because all the data is supposed to be the same length I was wondering can a CHAR field be NULL or an empty string? Such for cases when that specific field doesn't have a valu开发者_高级运维e whilst others do.
The answer to your question(s) is Yes.
Yes, the CHAR type can be NULLable. I believe every column type can allow for NULL.
Yes, the CHAR type can be an empty string. The DB will not see an empty string as any different from any other string. I wouldn't suggest ever using empty string though because, in almost every case, an empty string (lack of ANY characters) is trying to represent a lack of data, which is what NULL is for.
If you want to know more specific details around the difference of CHAR vs. VARCHAR in MySQL specifically...
MySQL 5.0 Reference - The CHAR and VARCHAR Types
Yes, both NULL and empty string can be used. The data doesn't have to be the same length in the column, as you noted, anything smaller is just padded with spaces to fill the column.
精彩评论