且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将行中的所有列从日期转换为时间戳MySQL

更新时间:2023-10-22 16:17:04

使用 ALTER TABLE 命令:

  ALTER TABLE table_name CHANGE old_date new_timestamp TIMESTAMP 

此过程可能需要一段时间如果你有大量的数据和大量的索引,那么就完成了。



我不知道你为什么要将它们切换到 TIMESTAMP 类型,因为它的范围比 DATETIME 的范围要大得多。这些区别是记录的,并且很重要。


I have a database with 13000 rows. Each row contains the column 'date' which is now in this format (example): 2012-09-01 17:53:28, but I want them to be a TIMESTAMP.

What query should I run to update all columns named 'date' from date to timestamp on all rows?

Thanks

It's pretty straightforward to convert from one type to another using the ALTER TABLE command:

ALTER TABLE table_name CHANGE old_date new_timestamp TIMESTAMP

This process may take a while to complete if you have a lot of data and a large number of indexes.

I'm not sure why you'd want to switch these to the TIMESTAMP type as that has a much more limited range than DATETIME. The differences are documented and important to know.