在mysql中将日期存储为字符串是否安全? [英] Is it safe to store dates as a string in mysql?

查看:58
本文介绍了在mysql中将日期存储为字符串是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须允许我的用户进行更新,且与之前的更新相比要延迟24小时,因此,由于在这种情况下,日期操作将在后端进行,但是在Web服务器中,我可以将Javascript日期存储为数据库中的一个字符串,当我必须计算两个日期之间的差值时,我可以从数据库中查询lastUpdateDate并将其解析为JS Date对象,这样做安全吗?

I have to allow my users to make an update with 24 hours of delay from their previous update, so because date operations in this case are going to be on the backend, but in the web server, i can store Javascript Date as a string in the database and when i have to calculate the difference between 2 dates i can query the lastUpdateDate from the database and parse it to a JS Date Object, is this safe to do?

推荐答案

在MySQL中将日期存储为字符串是否安全?

Is it safe to store dates as a string in mysql?

只要您用来表示日期的格式是明确的(即,每个值都映射到一个唯一的日期),它是安全.

It is safe as long as the format that you use to represent your dates is unambiguous (that is, each value maps to a unique date).

但是总是使用无效的数据类型来存储值是低效率的.迟早,您将需要在数据库中进行一些日期计算(排序,过滤,添加等):将日期存储为字符串会使这种操作更加复杂(开销因格式),效率要低得多(通常需要先将所有字符串转换为日期,然后才能对其进行操作).

But it is always inefficient not to use the proper datatype to store a value. Sooner or later, you will face the need to do some date computation in the database (sorting, filtering, adding, ...): storing your dates as strings will make such operation more complex that it has to (the overhead varies depending on the format your choose), and much less efficient (you would typically need to translate all the strings to dates before you can operate on them).

另一方面,从一开始就使用正确的数据类型不会使前端的事情变得更复杂-尤其是在MySQL中.您只需要在将字符串传递到数据库之前正确格式化字符串('YYYY-MM-DD HH:MI:SS'),MySQL就会很高兴将它们视为日期.

On the other hand, using the proper datatype from the start does not makes things more complicated on the frontend - especially in MySQL. You just need to format your strings properly ('YYYY-MM-DD HH:MI:SS') before passing them to the database, and MySQL will happily treat them as dates.

这篇关于在mysql中将日期存储为字符串是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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