Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of varchar(0)

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.

like image 939
Aleks G Avatar asked Feb 04 '14 14:02

Aleks G


People also ask

What is the purpose of varchar?

The VARCHAR function returns a varying-length character string representation of a character string.

What does 0 mean in SQL Server?

“Every [SQL] data type includes a special value, called the null value,”0 “that is used to indicate the absence of any data value”.1.

What does varchar number mean?

varchar(n) means a varying length character data type, and where n is the number of characters it can store.

Why varchar is used in SQL?

You can use SQL varchar when the sizes of the column vary considerably, use varchar(max) when there are chances that string length might exceed 8000 bytes, use char when the sizes of the column are fixed and use nvarchar if there is a requirement to store Unicode or multilingual data.


1 Answers

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).

like image 171
Kermit Avatar answered Oct 27 '22 05:10

Kermit