SQL Server 설정 메 인 키 자체 성장 열 (sql 문 구 를 사용 하여 구현)

원래 주소:http://www.jb51.net/article/33539.htm
1. 새 데이터 시트 를 만 듭 니 다. 필드 id 가 있 습 니 다. id 를 메 인 키 로 설정 합 니 다. 
 
   
create table tb(id int,constraint pkid primary key (id)) 
create table tb(id int primary key ) 

2. 새 데이터 시트 에 필드 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. 데이터 시트 를 만 들 었 습 니 다. 안에 필드 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) 

좋은 웹페이지 즐겨찾기