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

