9

I recently encountered a problem caused by a typo in the database creation script, whereby a column in the database was created as varchar(0) instead of varchar(20).

I expected that I would have gotten an error for 0-length string field, but I didn't. What is the purpose of varchar(0) or char(0) as I wouldn't be able to store any data in this column anyway.

5
  • 1
    What DBMS are you using?
    – Kermit
    Feb 4, 2014 at 14:43
  • @FreshPrinceOfSO This specific case was mysql - updating the question.
    – Aleks G
    Feb 4, 2014 at 14:44
  • 2
    Why do you assume it has to be useful to be legal?
    – Wooble
    Feb 4, 2014 at 14:46
  • @Wooble I didn't ask anything about legal. Clearly, it's legal. I'm asking what the purpose of this is.
    – Aleks G
    Feb 4, 2014 at 14:48
  • MySQL is known for being non-compliant with SQL standards.
    – Kermit
    Feb 4, 2014 at 14:50

5 Answers 5

15

It's not allowed per the SQL-92 standard, but permitted in MySQL. From the MySQL manual:

MySQL permits you to create a column of type CHAR(0). This is useful primarily when you have to be compliant with old applications that depend on the existence of a column but that do not actually use its value. CHAR(0) is also quite nice when you need a column that can take only two values: A column that is defined as CHAR(0) NULL occupies only one bit and can take only the values NULL and '' (the empty string).

6

Just checked MySQL, it's true that it allows zero-length CHAR and VARCHAR.

Not that it can be extremely useful but I can think of a situation when you truncate a column to 0 length when you no longer need it but you don't want to break existing code that writes something there. Anything you assign to a 0-length column will be truncated and a warning issued, but warnings are not errors, they don't break anything.

4
  • then why not to delete that column? I think deleting the column may required to change in application's code... Feb 4, 2014 at 14:49
  • 2
    I already told "you don't want to break existing code that writes something there" Feb 4, 2014 at 14:50
  • I had same guess so I + your answer, I think add this line in your answer if you wish. Feb 4, 2014 at 14:51
  • Rather that allow it, it just throws a warning and truncate the value depending on your SQL mode. Oct 5, 2015 at 14:26
3

As they're similar types, char and varchar, I'm going to venture to guess that the use-case of varchar(0) is the same as char(0).

From the documentation of String Types:

MySQL permits you to create a column of type CHAR(0). This is useful primarily when you have to be compliant with old applications that depend on the existence of a column but that do not actually use its value. CHAR(0) is also quite nice when you need a column that can take only two values: A column that is defined as CHAR(0) NULL occupies only one bit and can take only the values NULL and '' (the empty string).

0

It's useful in combination with a unique index for if you want to mark one specific row in your table (for instance, because it serves as a default). The unique index ensures that all other rows have to be null, so you can always retrieve the row by that column.

0

You can use it to store boolean values.

Look this code:

mysql> create table chartest(a char(0));
Query OK, 0 rows affected (0.26 sec)

mysql> insert into chartest value(NULL);
Query OK, 1 row affected (0.01 sec)

mysql> insert into chartest value('');
Query OK, 1 row affected (0.00 sec)

mysql> select 'true' from chartest where a is null;
+------+
| true |
+------+
| true |
+------+
1 row in set (0.00 sec)

mysql> select 'false' from chartest where a is not null;
+-------+
| false |
+-------+
| false |
+-------+
1 row in set (0.00 sec)

We can use NULL to represent true and '' (empty string) to represent false!

According to MySQL reference manual, only NULL occupies one bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.