SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SET identity and primary key on existing column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

olibara
Yak Posting Veteran

84 Posts

Posted - 06/13/2012 :  02:55:35  Show Profile  Reply with Quote
Hello

I'm trying to find the ALTER syntax to set Primary Key and Identity to an existing Column
I'm looking around internet but this is the jungle to find pertinent or efficient SQL Syntax description !

Here is the DDL ogf my table I need to set MyKey as Primary Key and Identity ?

CREATE TABLE [dbo].[Test](
	[MyKey] [int] NULL,
	[Name] [varchar](50) NULL
)


I have another little question : I see that if SSMS generate the CREATe Sript for this Table there is an added command "ON Primary"

CREATE TABLE [dbo].[Testu](
	[MyKey] [int] NULL,
	[Name] [varchar](50) NULL
) ON [PRIMARY]


what is the purpose of that ON PRIMARY ?
It is also not clear !

Thanks for your help

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/13/2012 :  03:45:48  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Can't change an existing column to identity.
You can create a new column as identity, delete the old and rename the new.
That will change the values - yoou can create a new table with an identity column and copy the data over retaining the values then delete the old table and rename the new.

on primary is just the filegroup the table is on - I suspect you only have one so that will be the default anyway.
Have a look at the cretae table statement in bol.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

olibara
Yak Posting Veteran

84 Posts

Posted - 06/13/2012 :  04:37:48  Show Profile  Reply with Quote
Thank you nigelrivett

So when you ask SSMS to set a column identity it does all that cooking in backround ?
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/13/2012 :  04:52:51  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Yep - you can get the script of what it does.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000