• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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