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)
您可能感興趣的文章:
相關文章
sql將一個表中的數(shù)據(jù)插入到另一個表中的方法
這篇文章主要介紹了sql將一個表中的數(shù)據(jù)插入到另一個表中的方法,需要的朋友可以參考下2014-03-03簡析SQL Server數(shù)據(jù)庫用視圖來處理復雜的數(shù)據(jù)查詢關系
本文我們主要介紹了SQL Server數(shù)據(jù)庫用視圖來處理復雜的數(shù)據(jù)查詢關系的相關知識,以及視圖的優(yōu)缺點和創(chuàng)建方式以及注意事項的相關知識,需要的朋友可以參考下2015-08-08SQL update select結(jié)合語句詳解及應用
這篇文章主要介紹了SQL update select結(jié)合語句詳解及應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03SQL Server 數(shù)據(jù)庫索引其索引的小技巧
關于索引的常識:影響到數(shù)據(jù)庫性能的最大因素就是索引。由于該問題的復雜性,我只可能簡單的談談這個問題,不過關于這方面的問題,目前有好幾本不錯的書籍可供你參閱。我在這里只討論兩種SQL Server索引,即clustered索引和nonclustered索引2012-06-06在sqlserver數(shù)據(jù)庫中導入Excel數(shù)據(jù)的全過程
在SQL Server中導入Excel數(shù)據(jù)可以通過使用導入/導出向?qū)硗瓿?下面這篇文章主要給大家介紹了關于在sqlserver數(shù)據(jù)庫中導入Excel數(shù)據(jù)的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-01-01一個查看MSSQLServer數(shù)據(jù)庫空間使用情況的存儲過程 SpaceUsed
一個查看MSSQLServer數(shù)據(jù)庫空間使用情況的存儲過程 SpaceUsed...2007-02-02