How to write an SQL statement that changes the field type to "ID" and sets this field as the primary key?

Source: Internet
Author: User
Subject:   How to write an SQL statement that modifies the field type to "ID" and sets this field as the primary key ??
Author:   China_skag ()
Level 1:    
Reputation value:   100
Forum:   MS-SQL server problems
Problem points:   20
Replies:   11
Posting time:   2006-7-28 14:26:56
     
 
     
As the question!
 
  Reply:Louisxiv)() Credit: 100 2006-7-28 14:38:54 Score: 0
 
 
?
Unable to alter a column into identity Field
Top
 
  Reply:Liangpei2008 (Xiaojie)() Credit: 100 2006-7-28 14:50:59 Score: 0
 
 
?
Deleted and rebuilt Fields

Top
 
  Reply:China_skag ()() Credit: 100 2006-7-28 14:56:23 Score: 0
 
 
?
Waiting online .....
Top
 
  Reply:Hellowork (one or two breeze)() Credit: 100 2006-7-28 15:06:53 Score: 0
 
 
?
Columns to be identified as identity columns can only be deleted first and then added with the same name.
---- Delete a column
Alter table tablename drop column ID
Go
---- Add the identity column
Alter table tablename add ID int identity (1, 1)
Go
---- Set the identity column as the primary key
Alter table tablename add constraint [pk_tablename] primary key clustered ([ID])
Top
 
  Reply:China_skag ()() Credit: 100 2006-7-28 15:39:34 Score: 0
 
 
?
What does [pk_tablename] mean?
If there is another table: Title
Column name: ID name type content addtime

Where ID is int type

I want to change the ID to identity and primary key. How can I write an SQL statement?

I wrote the following, right? Please advise.

Alter table title drop column ID
Go
Alter table title add ID int identity (1, 1)
Go
Alter table title add constraint [pk_tablename] primary key clustered ([ID])

Top
 
  Reply:Hellowork (one or two breeze)() Credit: 100 2006-7-28 15:45:07 Score: 0
 
 
?
Replace tablename in pk_tablename with title. For example:
Alter table title add constraint [pk_title] primary key clustered ([ID])
Tablename is only a placeholder. It is replaced with a specific table object name.
Top
 
  Reply:Louisxiv)() Credit: 100 2006-7-28 15:45:26 Score: 0
 
 
?
What does [pk_tablename] mean?
-----------------------
It just a name for the primary key, you can name it as any string you can imagine.
Top
 
  Reply:China_skag ()() Credit: 100 2006-7-28 15:48:55 Score: 0
 
 
?
Thank you very much, hellowork (one or two breeze), louisxiv (night god )!
Top
 
  Reply:Hellowork (one or two breeze)() Credit: 100 2006-7-28 15:51:11 Score: 0
 
 
?
[Pk_title] is the name of the primary key constraint.
In sqlserver, primary keys, foreign keys, default values, triggers, stored procedures, and functions are all objects. You must name these objects. sqlserver can use these names to manage them.
Generally, their class abbreviations are used as the prefix of the object name when naming objects, so that you can know where the object is through this prefix. therefore, [pk_title] is the name of the primary key, and PK is the abbreviation of primarykey.
Top
 
  Reply:China_skag ()() Credit: 100 2006-7-28 15:57:08 Score: 0
 
 
?
Oh! It turns out like this. Thank you!
Top
 
  Reply:China_skag ()() Credit: 100 2006-7-28 15:58:35 Score: 0
 
 
?
Alter table title drop column ID
Go
Alter table title add ID int identity (1, 1)
Go
Alter table title add constraint [pk_title] primary key clustered ([ID])

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.