SQL Server設置主鍵自增長列(使用sql語句實現(xiàn))
更新時間:2013年01月24日 16:47:53 作者:
主鍵自增長列在進行數(shù)據(jù)插入的時候,很有用的,如可以獲取返回的自增ID值,接下來將介紹SQL Server如何設置主鍵自增長列,感興趣的朋友可以了解下,希望本文對你有所幫助
1.新建一數(shù)據(jù)表,里面有字段id,將id設為為主鍵
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )
2.新建一數(shù)據(jù)表,里面有字段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.已經(jīng)建好一數(shù)據(jù)表,里面有字段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)
復制代碼 代碼如下:
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )
2.新建一數(shù)據(jù)表,里面有字段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.已經(jīng)建好一數(shù)據(jù)表,里面有字段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)
您可能感興趣的文章:
相關文章
SSB(SQLservice Service Broker) 入門實例介紹
前兩天用了 MSsql里的 SSB委托機制,做了一個消息分發(fā)的小功能,在這里簡單跟大家分享一下方法跟實例2013-04-04
SQL Server存儲過程同時返回分頁結果集和總數(shù)
這篇文章主要為大家詳細介紹了SQL Server存儲過程同時返回分頁結果集和總數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
t-sql清空表數(shù)據(jù)的兩種方式示例(truncate and delete)
這篇文章主要介紹了t-sql使用truncate and delete清空表數(shù)據(jù)的兩種方法,大家參考使用2013-11-11

