简体   繁体   中英

Is it ok to insert NOW() into a VARCHAR column?

This works fine in MySQL 5.6 but is it ok to do this and is it future-proof?

create table test (`test_column` varchar(100));
insert into test values (now());

Although we cannot always predict the future, MySQL has always, and likely always will use the ISO-8601 date format which is very well specified.

Storing a date value in a string column is a rather odd thing to do, especially if you want it to be a date or date-time. Use the appropriate column type, either DATE or DATETIME as native representation provides a lot of benefits: Faster indexing, lighter storage requirements, automatic validation, and compatibility with the various date functions MySQL has built-in.

I can't think of a good reason to represent at date as some arbitrary VARCHAR column in MySQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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