954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Relocate PK to another field

I have an existing table:

CREAT TABLE Course
(CourseNo       int    NOT NULL    PRIMARY KEY,
CourseName   varchar NOT NULL
StudentNo       int    NOT NULL    REFERENCES Student (StudentNo))


Is it possible to code the following change in T-SQL:
Introduce a new column called CourseID (int NOT NULL) and make it the PK? In other words, remove CourseNo from being the PK and make CourseID the new PK without dropping the column CourseNo?

Let's just put aside the business case for this change and assume it to be a theoretical pursuit.

ntk
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

You can do it using alter table table1 add (courseId numeric(10))
then
update table1 set courseid=courseno

then alter table table1 drop "pkoldname"

then alter table table1 add primary key pknewname (courseid)

You can also user enterprise manager to do above operation using gui.

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You