1. Create a new data table with the field id. Set the id as the primary key.
Copy codeThe Code is as follows:
Create table tb (id int, constraint pkid primary key (id ))
Create table tb (id int primary key)
2. Create a new data table with the field id. Set the id as the primary key and the id is automatically numbered.
Copy codeThe Code is as follows:
Create table tb (id int identity (1, 1), constraint pkid primary key (id ))
Create table tb (id int identity (1, 1) primary key)
3. A data table has been created with the field id, which sets the id as the primary key.
Copy codeThe Code is as follows:
Alter table tb alter column id int not null
Alter table tb add constraint pkid primary key (id)
4. Delete the primary key
Copy codeThe Code is as follows:
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)