首頁  >  文章  >  資料庫  >  Sql Server代码设置主键并自动生长

Sql Server代码设置主键并自动生长

WBOY
WBOY原創
2016-06-07 16:20:091423瀏覽

新建一张数据表,将Userid设为主键并自动增长 create table T_UserLogin(UserId int not null primary key identity(1,1),UserName nvarchar(50) not null,Pass varchar(50) not null ) 修改已经建好的一张数据表,将Userid设为主键并自动生长 alter table t

   新建一张数据表,将Userid设为主键并自动增长

  create table T_UserLogin(UserId int not null primary key identity(1,1),UserName nvarchar(50) not null,Pass varchar(50) not null )

  修改已经建好的一张数据表,,将Userid设为主键并自动生长

  alter table tb alter column id int not null

  alter table tb add constraint pkid primary key (id)

  删除主键

  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)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn