创建为选择并添加一个额外的列 [英] Create as Select and add an extra column

查看:103
本文介绍了创建为选择并添加一个额外的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个MySQL表作为另一个表的副本,如下所示:

I want to create a MySQL table as a copy of another table like this:

CREATE TABLE new_tbl SELECT * FROM orig_tbl;

我的意思是,如果可能的话,我想在创建时添加另一个空列,

The twist is that I want, if possible, to add at the time of creation another empty column, that will be populated at a later time.

我知道我可以像上面那样创建它,然后再使用ALTER TABLE,但是我的想法是,鉴于大量数据,ALTER将花费很长时间(如果这是错误的,请与我矛盾),如果我想要的是可以保存的。

I know that I can just create it as above and use ALTER TABLE afterwards, but my thinking is that, given a large amount of data, the ALTER is gonna take a long time (please contradict me if this is wrong), that can be saved if what I want is possible.

所以,说我想要一个额外的 extra_col-varchar(64),我的原始查询是什么?

So, say I want an extra extra_col - varchar(64), what would my original query be?

谢谢。

推荐答案

创建表... SELECT 语法


您可以通过在 CREATE TABLE SELECT 语句从另一个表创建一个表>语句:

You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement:

CREATE TABLE new_tbl SELECT * FROM orig_tbl;

MySQL为 SELECT 中的所有元素创建新列。

MySQL creates new columns for all elements in the SELECT.

[删除]

注意, SELECT 语句中的列被附加到

Notice that the columns from the SELECT statement are appended to the right side of the table, not overlapped onto it.

[删除]

在由 CREATE TABLE ... SELECT 生成的表中,仅在 CREATE TABLE 部分中命名的列首先。在这两个部分中或仅在 SELECT 部分中命名的列之后。可以通过在 CREATE TABLE 部分中指定列来覆盖 SELECT 列的数据类型。

In a table resulting from CREATE TABLE ... SELECT, columns named only in the CREATE TABLE part come first. Columns named in both parts or only in the SELECT part come after that. The data type of SELECT columns can be overridden by also specifying the column in the CREATE TABLE part.

因此:

CREATE TABLE new_tbl (
  extra_col VARCHAR(64)
) SELECT * FROM orig_tbl

这篇关于创建为选择并添加一个额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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