Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Alter PK changing it to different field type

Author  Topic 

mparker
Starting Member

1 Post

Posted - 2008-12-08 : 16:31:57
I have a primary key in an existing table which is populated. The PK was previously integer, auto-incrementing identity. I would like to change the type to be a varchar(50). Can someone help me on the t-sql script i could use to do this?

I do not want to do this through the enterprise manager gui.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-08 : 16:55:14
Drop the constraint
Modify column's data type
Create the constraint

All 3 are done with the ALTER TABLE statement.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-09 : 03:22:59
why do you want PK to be varchar?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-09 : 12:49:45
Why wouldn't you want your PK to be varchar?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-09 : 13:02:05
nope. i was asking reason behind changing PK from identity column to varchar column. An integer identity column would have been much better choice for PK as they are fast to index, fast and convenient in joins with foreign keys.Also they will require minimal effort to maintain due to the as they are autoincrementing.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-09 : 13:36:26
I disagree. Varchars can be just as fast and easy to maintain, it just depends on the design and the business requirements. We just can't make blanket statements about ints being faster or easier to maintain without knowing more information.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -