快速将列的副本添加到MySQL表

时间:2022-09-16 10:09:03

I need a fast way duplicate a DATETIME column in a table and give it a new name.

我需要快速复制表中的DATETIME列并为其指定新名称。

I have a column named myDate in my table called myResults, I need a query to make a new column in the table called newDate which has the exact same data as the myDate column.

我的表中有一个名为myRate的名为myDate的列,我需要一个查询来在表中创建一个名为newDate的新列,该列与myDate列具有完全相同的数据。

Is there a faster way to do this than by doing the obvious 2 step approach of make a new column, and then copying all the data (it's a large table and I'm looking for the fastest approach)?

有没有比通过制作新列的明显的两步方法更快的方法,然后复制所有数据(这是一个大表,我正在寻找最快的方法)?

Obvious solution:

明显的解决方案:

ALTER TABLE `myResults` ADD `newDate` DATETIME;  
UPDATE `myResults` SET `newDate` = `myDate`;

4 个解决方案

#1


18  

The obvious solution is the only solution, unfortunately.

不幸的是,明显的解决方案是唯一的解决方案。

However note that in general you shouldn't be copying a column in relational databases.

但请注意,通常不应复制关系数据库中的列。

#2


11  

UPDATE `table_name` SET `new_column` = `existing_column` WHERE `id`=`id`

#3


2  

Why would your workload ever demand a new datetime column, that duplicates another columns data? This sounds like horrable practice? How about telling us what you're trying to achieve? You can pull a second column with the same data in a few different ways, without actually duplicating the data:

为什么您的工作负载需要一个新的日期时间列,它会复制另一个列数据?这听起来像是可怕的练习?告诉我们你想要达到的目标怎么样?您可以通过几种不同的方式使用相同的数据提取第二列,而无需实际复制数据:

SELECT date1 AS date_old, date1 AS date_new FROM table;

-or-, you can create a view

- 或 - ,您可以创建一个视图

CREATE VIEW virtual_table AS
    SELECT date1 AS date_old, date1 AS date_new FROM table
;
SELECT * FROM virtual_table;

#4


1  

If you just need a default in there, you can either choose what the default is statically or use a function call.

如果您只需要默认值,则可以选择静态默认值或使用函数调用。

ALTER TABLE `myResults` ADD `newDate` DATETIME DEFAULT '2010-01-01';

or

要么

ALTER TABLE `myResults` ADD `newDate` DATETIME DEFAULT current_timestamp;

#1


18  

The obvious solution is the only solution, unfortunately.

不幸的是,明显的解决方案是唯一的解决方案。

However note that in general you shouldn't be copying a column in relational databases.

但请注意,通常不应复制关系数据库中的列。

#2


11  

UPDATE `table_name` SET `new_column` = `existing_column` WHERE `id`=`id`

#3


2  

Why would your workload ever demand a new datetime column, that duplicates another columns data? This sounds like horrable practice? How about telling us what you're trying to achieve? You can pull a second column with the same data in a few different ways, without actually duplicating the data:

为什么您的工作负载需要一个新的日期时间列,它会复制另一个列数据?这听起来像是可怕的练习?告诉我们你想要达到的目标怎么样?您可以通过几种不同的方式使用相同的数据提取第二列,而无需实际复制数据:

SELECT date1 AS date_old, date1 AS date_new FROM table;

-or-, you can create a view

- 或 - ,您可以创建一个视图

CREATE VIEW virtual_table AS
    SELECT date1 AS date_old, date1 AS date_new FROM table
;
SELECT * FROM virtual_table;

#4


1  

If you just need a default in there, you can either choose what the default is statically or use a function call.

如果您只需要默认值,则可以选择静态默认值或使用函数调用。

ALTER TABLE `myResults` ADD `newDate` DATETIME DEFAULT '2010-01-01';

or

要么

ALTER TABLE `myResults` ADD `newDate` DATETIME DEFAULT current_timestamp;