SqlServer代碼設置主鍵並自動生長

新建一張數據表,將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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章