Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set a CHAR to NULL or an empty string?

Tags:

null

char

mysql

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 value whilst others do.

like image 869
Brett Avatar asked May 13 '11 19:05

Brett


2 Answers

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

like image 125
Jesse Webb Avatar answered Sep 21 '22 03:09

Jesse Webb


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.

like image 39
Joe Stefanelli Avatar answered Sep 19 '22 03:09

Joe Stefanelli