简体   繁体   English

使用CHAR(6)表示年+月字段是一个坏主意吗?

[英]Is it a bad idea using CHAR(6) to represent year+month field?

I am using MySQL and I have a field in a table that needs to store a year+month value. 我正在使用MySQL,并且在表中有一个需要存储年+月值的字段。 The field doesn't need the day, minute and second info. 该字段不需要日期,分钟和秒信息。 I am thinking to create the field as CHAR(6) because it seems to be fine using the > , = and < to compare the string. 我正在考虑将字段创建为CHAR(6)因为使用>=<来比较字符串似乎很好。

SELECT '201108' < '201109'
>1

I want to use this format because I can insert the same string to Lucene index engine. 我想使用这种格式,因为我可以将相同的字符串插入Lucene索引引擎。 Is it a good idea or I should stick with DATE ? 这是个好主意还是我应该坚持DATE

That will work fine, right up to the point where you have to implement your own code for working out the difference between two values, or figuring out what value you need for a time six months into the future. 这样就可以很好地工作,直到您必须实现自己的代码来计算两个值之间的差异,或者找出未来六个月内需要的值。

Use the date type, that's what it's for (a) . 使用日期类型,这就是(a)的含义。 If is has too much resolution, enforce the constraint that the day will always be set to 1. Or force that with an insert/update trigger. 如果分辨率太大,请强制将日期始终设置为1。或者使用插入/更新触发器强制执行该约束。

Then you can use all the fancy date manipulation code that your DBMS vendor has already written, code that's probably going to be much more efficient since it will be dealing with a native binary type rather than a character string. 然后,您可以使用DBMS供应商已经编写的所有花式日期操作代码,由于它们将处理本机二进制类型而不是字符串,因此该代码可能会更加高效。

And you'll save space in this particular case as well since a MySQL date type is actually shorter than a char(6) . 而且在这种特殊情况下,您还将节省空间,因为MySQL date类型实际上比char(6) It's not often that a database decision gives you both space and time advantages (it's usually a trade-off), so you should seize them whenever you can. 数据库决策通常不会同时给您带来空间时间上的优势(通常是权衡取舍),因此您应该尽可能地抓住它们。


(a) This applies to all of those types, such as date , time and datetime . (a)这适用于所有这些类型,例如datetimedatetime

You'd want to use a date, but not store anything in the Day field. 您想使用日期,但不希望在“日期”字段中存储任何内容。 The database is more efficient at searching than your code will ever be because the database is optimized to handle lookups such as this one. 该数据库比您的代码具有更高的搜索效率,因为该数据库经过优化以处理诸如此类的查找。 You'd want to store a dummy value in the Day field to make this work. 您需要在“日期”字段中存储一个虚拟值以使此工作有效。

好吧,由于MySQL仅用3个字节来存储日期 (警告:该链接适用于MySQL 5.0版。请检查您所用版本的文档以确保使用该日期),从存储角度和性能角度来看最好比较时-使用date

You can also use the Date Field for that and then while selecting the values you can use DATE_FORMAT function with that Date for selecting the year and month. 您也可以为此使用“日期”字段,然后在选择值时,可以将带有该日期的DATE_FORMAT函数用于选择年和月。

having field as Date type then 然后将字段作为日期类型

like the Date you entered is '2011-08-30' 就像您输入的日期是“ 2011-08-30”

Now you want the result as 201108 the write 现在您想要将结果作为201108写入

select DATE_FORMAT('2011-08-30','%Y%m'); 选择DATE_FORMAT('2011-08-30','%Y%m');

it will give result as 201108 结果将为201108

for more detailed information for DATE_FORMAT please visit 有关DATE_FORMAT的更多详细信息,请访问

http://davidwalsh.name/format-date-mysql-date_format http://davidwalsh.name/format-date-mysql-date_format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM