简体   繁体   English

克隆表MYSQL并添加唯一的ID?

[英]Cloning a table MYSQL and adding a unique ID?

The code I have been using to clone a table is as follows: 我一直在用来克隆表的代码如下:

CREATE TABLE new_table LIKE original_table;
INSERT INTO new_table SELECT * FROM original_table;

However, there is a problem that in the new table I want a unique, auto incrementing id column. 但是,存在一个问题,在新表中,我需要一个唯一的自动递增的id列。

But I run into the column count doesn't match value count at row 1 error . 但是我遇到了column count doesn't match value count at row 1 error

I know that you can use the DEFAULT keyword , or leave the id out and it will auto increment but I am unsure as how the query should be structured? 我知道您可以使用DEFAULT keyword ,也可以不使用ID,它会自动递增,但是我不确定查询的结构如何?

After you added the PK auto increment column (assuming its the first column) try this: 在添加PK自动增量列(假设其为第一列)之后,请尝试以下操作:

INSERT INTO new_table 
SELECT NULL,[all column names from original_table] FROM original_table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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