SQLServer设置主键⾃增长列(使⽤sql语句实现)1.新建⼀数据表,⾥⾯有字段id,将id设为为主键
复制代码代码如下:
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )
2.新建⼀数据表,⾥⾯有字段id,将id设为主键且⾃动编号
复制代码代码如下:
create table tb(id int identity(1,1),constraint pkid primary key (id))
create table tb(id int identity(1,1) primary key )
3.已经建好⼀数据表,⾥⾯有字段id,将id设为主键
复制代码代码如下:
alter table tb alter column id int not null
增加字段的sql语句
alter table tb add constraint pkid primary key (id)
4.删除主键
复制代码代码如下:
Declare @Pk varChar(100);
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('tb') and xtype='PK';
if @Pk is not null
exec('Alter table tb Drop '+ @Pk)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。