[英]Sql Query Create a new Table
Can anyone offered syntax for a sql query that takes an already created table and inserts some of the data into a new table with a new id(primary key)? 任何人都可以为sql查询提供语法,该查询采用已创建的表并将一些数据插入到具有新id(主键)的新表中? So far I tried this 到目前为止我试过这个
SELECT ((ID INT IDENTITY(1,1) PRIMARY KEY), column1, column2, column3)
INTO table_name
FROM table_name
I keep coming up with a syntax error. 我一直想出一个语法错误。
SELECT column1, column2, column3
INTO table_name_new
FROM table_name_old
use like this 像这样用
INSERT INTO newtable (column1, column2, column3)
SELECT (column1, column2, column3)
FROM table_name
INSERT INTO new_table
(Column1, Column2 . . . )
SELECT Column1, Column2 FROM old_table
Don't include the identity column (the primary key with the auto-generated numbers) in the insert list. 不要在插入列表中包含标识列(具有自动生成的数字的主键)。
只需按下面所示,插入记录时将自动创建一个新的ID
insert into table1 (col1,col2,col3) select col1,col2,col3 from table2;
According to the documentation on MSDN , the IDENTITY property will transfer through with a SELECT INTO
clause, but constraints will not. 根据MSDN上的文档 ,IDENTITY属性将通过SELECT INTO
子句传输,但约束不会。 That means you can create a new table with an IDENTITY column if you include the original IDENTITY column from your source table, but the column in the new table won't be a primary key. 这意味着如果包含源表中的原始IDENTITY列,则可以使用IDENTITY列创建新表,但新表中的列不是主键。
The sane way to do this is to use ALTER TABLE after the table is created, as @Kai has suggested. 这样做的理智方法是在创建表后使用ALTER TABLE,如@Kai所建议的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.