更改sql server 2008中的现有表,为现有主键添加AUTOINCREMENT [英] alter an existing table in sql server 2008, add AUTOINCREMENT for existing primary key

查看:114
本文介绍了更改sql server 2008中的现有表,为现有主键添加AUTOINCREMENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在sql server 2008中有一个表我需要更改它以为此表中已存在的主键字段添加AutoIncrement。 br />


我需要脚本才能执行此查询。



有人可以提供帮助吗?



谢谢

Hi everybodyy,

I have a table in sql server 2008 I need to alter it to add a AutoIncrement for already existing primary key field in this table.

Please I need the script to execute this query.

Can anybody help?

thank you

推荐答案

如果你有ms sql srvr mgmt studio,你可以做到。你只需要手动完成,而不是脚本或命令形式。







连接到你的db



在对象资源管理器窗口中显示表/列名称



右键单击列名称



选择修改


栏目属性标签中的




将身份规范/身份更改为是



根据需要设置增量/种子属性





您无法通过查询更改现有列的身份







您有2个选项,



1.创建一个带有身份和放大器的新表放弃现有的表格



2.创建一个带有标识符的新列。放弃现有栏目



查看此



http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/04d69ee6- d4f5-4f8f-a115-d89f7bcbc032 [ ^ ]
If you have ms sql srvr mgmt studio you can do it. You just have to do it manually, not in script or command form.



connect to your db

show the table/column names in object explorer window

right click on column name

select modify

in column properties tab

alter "identity specification"/"is identity" to YES

set increment/seed properties as desired


You can't alter the existing columns for identity through query



You have 2 options,

1. Create a new table with identity & drop the existing table

2. Create a new column with identity & drop the existing column

check this

http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032[^]


阅读以下主题:



http://social.msdn.microsoft.com / forums / zh-CN / transactsql / thread / 04d69ee6-d4f5-4f8f-a115-d89f7bcbc032 [ ^ ]
Read the following on the subject :

http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032[^]


CREATE TABLE Student
 (
  Id        INT NOT NULL,
  Name    VARCHAR(100) NOT NULL
  )
go
/*
Populating Customer table,
using same TEST_Sequence to generate the Id column,
which we used for Cutomer table in past
*/
INSERT Student(Id, Name)
VALUES
(NEXT VALUE FOR TEST_Sequence, 'Ram'),
(NEXT VALUE FOR TEST_Sequence, 'Rita'),
(NEXT VALUE FOR TEST_Sequence, 'Ron')
go
select * from Student





此代码可以帮助你....

thanku



by this code may help you....
thanku


这篇关于更改sql server 2008中的现有表,为现有主键添加AUTOINCREMENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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