SQL SELECT INSERT INTO生成唯一标识 [英] SQL SELECT INSERT INTO Generate Unique Id
问题描述
我正在尝试选择一个数据表,并将此数据插入具有类似列名的另一个文件(它本质上是重复的数据)。当前语法如下:
I'm attempting to select a table of data and insert this data into another file with similar column names (it's essentially duplicate data). Current syntax as follows:
INSERT INTO TABLE1 (id, id2, col1, col2)
SELECT similiarId, similiarId2, similiarCol1, similiarCol2
FROM TABLE2
我遇到的问题是生成唯一的关键字段整数)。我不能使用table2的键,因为table1具有现有的数据,并且会在重复的键值上出现错误。
The problem I have is generating unique key fields (declared as integers) for the newly inserted records. I can't use table2's key's as table1 has existing data and will error on duplicate key values.
我无法更改表模式,这些是不自动生成的自定义id列由数据库。
I cannot change the table schema and these are custom id columns not generated automatically by the DB.
推荐答案
table1在其id字段有自动增量吗?如果是,您能否从插入中丢失similiarId并让自动递增处理唯一键?
Does table1 have an auto-increment on its id field? If so, can you lose similiarId from the insert and let the auto-increment take care of unique keys?
INSERT INTO TABLE1 (id2, col1, col2) SELECT similiarId2, similiarCol1, similiarCol2
FROM TABLE2
这篇关于SQL SELECT INSERT INTO生成唯一标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!