SQL Server設(shè)置主鍵自增長列(使用sql語句實現(xiàn))
更新時間:2013年01月24日 16:47:53 作者:
主鍵自增長列在進(jìn)行數(shù)據(jù)插入的時候,很有用的,如可以獲取返回的自增ID值,接下來將介紹SQL Server如何設(shè)置主鍵自增長列,感興趣的朋友可以了解下,希望本文對你有所幫助
1.新建一數(shù)據(jù)表,里面有字段id,將id設(shè)為為主鍵
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )
2.新建一數(shù)據(jù)表,里面有字段id,將id設(shè)為主鍵且自動編號
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設(shè)為主鍵
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)
復(fù)制代碼 代碼如下:
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )
2.新建一數(shù)據(jù)表,里面有字段id,將id設(shè)為主鍵且自動編號
復(fù)制代碼 代碼如下:
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設(shè)為主鍵
復(fù)制代碼 代碼如下:
alter table tb alter column id int not null
alter table tb add constraint pkid primary key (id)
4.刪除主鍵
復(fù)制代碼 代碼如下:
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)
您可能感興趣的文章:
- sqlserver2005自動創(chuàng)建數(shù)據(jù)表和自動添加某個字段索引
- SQL Server 打開或關(guān)閉自增長
- SqlServer Mysql數(shù)據(jù)庫修改自增列的值及相應(yīng)問題的解決方案
- SQL Server 2008怎樣添加自增列實現(xiàn)自增序號
- SQL Server修改標(biāo)識列方法 如自增列的批量化修改
- Oracle 實現(xiàn)類似SQL Server中自增字段的一個辦法
- SQL SERVER 自增列
- SQL Server 中調(diào)整自增字段的當(dāng)前初始值
- SQL Server數(shù)據(jù)表字段自定義自增數(shù)據(jù)格式的方法
相關(guān)文章
SSB(SQLservice Service Broker) 入門實例介紹
前兩天用了 MSsql里的 SSB委托機制,做了一個消息分發(fā)的小功能,在這里簡單跟大家分享一下方法跟實例2013-04-04
SQL Server存儲過程同時返回分頁結(jié)果集和總數(shù)
這篇文章主要為大家詳細(xì)介紹了SQL Server存儲過程同時返回分頁結(jié)果集和總數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
t-sql清空表數(shù)據(jù)的兩種方式示例(truncate and delete)
這篇文章主要介紹了t-sql使用truncate and delete清空表數(shù)據(jù)的兩種方法,大家參考使用2013-11-11
sql注入數(shù)據(jù)庫修復(fù)的兩種實例方法
這篇文章介紹了sql注入數(shù)據(jù)庫修復(fù)的兩種實例方法,有需要的朋友可以參考一下2013-09-09

