SQL Server設置主鍵自增長列(使用sql語句實現(xiàn))
發(fā)布日期:2022-01-06 12:38 | 文章來源:源碼中國
復制代碼 代碼如下:
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )
2.新建一數(shù)據表,里面有字段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.已經建好一數(shù)據表,里面有字段id,將id設為主鍵
復制代碼 代碼如下:
alter table tb alter column id int not null
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)
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
關注官方微信