varchar(0)的用途是什么 [英] What's the purpose of varchar(0)

查看:687
本文介绍了varchar(0)的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个由于数据库创建脚本中的错字引起的问题,即数据库中的一列被创建为varchar(0)而不是varchar(20).

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

我希望0长度字符串字段会出错,但我没有. varchar(0)char(0)的目的是什么,因为我仍然无法在此列中存储任何数据.

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.

推荐答案

SQL-92 标准,但在MySQL中允许.从 MySQL手册:

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

MySQL允许您创建类型为CHAR(0)的列.这主要在您必须与依赖于列存在但实际上不使用其值的旧应用程序兼容时很有用.当您需要一个只能接受两个值的列时,CHAR(0)也非常好:定义为CHAR(0) NULL的列仅占一位,并且只能接受值NULL''(空字符串) ).

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

这篇关于varchar(0)的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆