合并和更新主键 [英] Merge and update primary key

查看:117
本文介绍了合并和更新主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了看,但没有找到解决看似简单问题的方法.

I looked, but didn't find a solution to what seems like a simple problem.

我有许多具有相同结构的表,并希望将它们合并.唯一的问题是它们都有一个id字段作为主键.主键中将有很多重复项. id最终是什么都无关紧要.如何合并所有表,以免丢失任何数据?

I have many tables with identical structure and want to merge them. The only problem is that they all have an id field as the primary key. There would be a lot of duplicates in the primary key. It doesn't matter what the id ends up being. How can I merge all the tables so I don't loose any data?

推荐答案

在新表中创建一个AUTO_INCREMENT字段,并从值列表中将其忽略:

Create an AUTO_INCREMENT field in the new table and omit it from the list of values:

CREATE TABLE NEW_TABLE (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, col1 …, col2 …, …);

INSERT
INTO    new_table (col1, col2, …)
SELECT  col1, col2
FROM    old_table_1
UNION ALL
SELECT  col1, col2
FROM    old_table_2
UNION ALL
…

这篇关于合并和更新主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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