简单问题:用"select * into 新表 from 表1 "语句,怎么在生成的"新表"中加入主键列呢?

songfengerer 2003-10-16 03:44:06
我想用"select * into 新表 from 表1 "语句来生成新表,但不知怎么在生成的"新表"中加入主键列呢?
怎么修改一下上面的sql语句,使生成新表时,自动加入主键列(能自动加1,更好!!)?
谢谢!!!!!

...全文
96 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
aierong 2003-10-16
  • 打赏
  • 举报
回复
select * into newtable from tablename

alter table newtable
add ids int identity(1,1) constraint pk_aaa primary key(ids)

加入新列ids自动加1
zhbname 2003-10-16
  • 打赏
  • 举报
回复
up
welyngj 2003-10-16
  • 打赏
  • 举报
回复
select identity(int,1,1)as id,* into 新表 from 表1
sdhdy 2003-10-16
  • 打赏
  • 举报
回复
你最好新建一个表,
如:
create table aa(id int identity(1,1) primary key ,F1 varchar(20),F2 varchar(100))
go
insert aa(F1,F2) select * from 表1
pengdali 2003-10-16
  • 打赏
  • 举报
回复
alter table 新表 add constraint pk_1 primary key (编号)
pengdali 2003-10-16
  • 打赏
  • 举报
回复
ALTER TABLE 新表 ADD 编号 bigint identity(1,1) not null

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧