Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

How do I add a surrogate key to a table?

I want to manintain the current Primary Key as below:
 
CONSTRAINT [PK_Time] PRIMARY KEY CLUSTERED
(
      [PK_Date] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
)ON [PRIMARY]
 GO

What is the script to add a surrogate key to this table?
0
rhservan
Asked:
rhservan
  • 6
  • 2
1 Solution
 
marappanCommented:
Below is the example for surrogate key:

CREATE NONCLUSTERED INDEX IndexName
    ON TableName (ColumnName);
GO
0
 
rhservanAuthor Commented:
The script ran.  How can I verify in the column.  Is there supposed to be another key?
0
 
rhservanAuthor Commented:
Please respond to above question and I will award the points. Thanks marappan.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
rhservanAuthor Commented:
Please respond to above question and I will award the points. Thanks marappan.
0
 
rhservanAuthor Commented:
Please respond to above question and I will award the points. Thanks marappan.
0
 
rhservanAuthor Commented:
Please respond to above question and I will award the points. Thanks marappan.
0
 
rhservanAuthor Commented:
Marappan,

Please reengage!!
0
 
marappanCommented:
Sorry for the delay. I have to modify my solution. Sorry for the confusion.


ALTER TABLE TableName ALTER COLUMN ColumnName IDENTITY (1,1);

Normally surrogate key will be int. So the column you have to choose for surrogate key will be int in the table. If the column does not already exist then add a new column.

I hope this is useful for you. Please let me know if you need more assistance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now