在MySQL中使用varchar而不是date字段类型

时间:2022-03-30 16:54:37

Is there any reason to use a varchar field instead of a date field in MySQL? I'm looking at an existing site and I see the developer has done this. Is there any reason to?

有没有理由在MySQL中使用varchar字段而不是日期字段?我正在查看现有网站,我看到开发人员已经这样做了。有什么理由吗?

3 个解决方案

#1


9  

Unless it really is acting primarily as a string — say, it wants to preserve date formatting style, locale-named months, named timezones etc. and doesn't care about being computable — no, varchar would not be a good choice.

除非它真的主要是作为一个字符串 - 比如说,它想要保留日期格式化样式,区域设置命名的月份,命名时区等,并且不关心可计算性 - 不,varchar不是一个好的选择。

Handling real date datatypes can be problematic/non-portable across different DBMSs/data access layers, but the usual solution to that is just to use an integer (Unix-style) timestamp instead.

处理实时数据类型在不同的DBMS /数据访问层之间可能存在问题/不可移植,但通常的解决方案是使用整数(Unix风格)时间戳。

#2


1  

Because dates are a prickly subject that cause no end of confusion to developers? The example above seems like bad form to me.

因为日期是一个棘手的主题,导致开发人员不会混淆?上面的例子对我来说似乎是不好的形式。

#3


0  

I've seen lots of "Developer turned DBA" type people completely misuse database types, just chock it up to inexperience.

我已经看到很多“开发者变成DBA”类型的人完全滥用数据库类型,只是把它搞砸了。

#1


9  

Unless it really is acting primarily as a string — say, it wants to preserve date formatting style, locale-named months, named timezones etc. and doesn't care about being computable — no, varchar would not be a good choice.

除非它真的主要是作为一个字符串 - 比如说,它想要保留日期格式化样式,区域设置命名的月份,命名时区等,并且不关心可计算性 - 不,varchar不是一个好的选择。

Handling real date datatypes can be problematic/non-portable across different DBMSs/data access layers, but the usual solution to that is just to use an integer (Unix-style) timestamp instead.

处理实时数据类型在不同的DBMS /数据访问层之间可能存在问题/不可移植,但通常的解决方案是使用整数(Unix风格)时间戳。

#2


1  

Because dates are a prickly subject that cause no end of confusion to developers? The example above seems like bad form to me.

因为日期是一个棘手的主题,导致开发人员不会混淆?上面的例子对我来说似乎是不好的形式。

#3


0  

I've seen lots of "Developer turned DBA" type people completely misuse database types, just chock it up to inexperience.

我已经看到很多“开发者变成DBA”类型的人完全滥用数据库类型,只是把它搞砸了。