Solved

How do I add a surrogate key to a table?

Posted on 2011-09-22
8
742 Views
Last Modified: 2012-06-27
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
Comment
Question by:rhservan
  • 6
  • 2
8 Comments
 
LVL 2

Accepted Solution

by:
marappan earned 500 total points
ID: 36583100
Below is the example for surrogate key:

CREATE NONCLUSTERED INDEX IndexName
    ON TableName (ColumnName);
GO
0
 

Author Comment

by:rhservan
ID: 36583190
The script ran.  How can I verify in the column.  Is there supposed to be another key?
0
 

Author Comment

by:rhservan
ID: 36583319
Please respond to above question and I will award the points. Thanks marappan.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:rhservan
ID: 36583410
Please respond to above question and I will award the points. Thanks marappan.
0
 

Author Comment

by:rhservan
ID: 36583411
Please respond to above question and I will award the points. Thanks marappan.
0
 

Author Comment

by:rhservan
ID: 36583414
Please respond to above question and I will award the points. Thanks marappan.
0
 

Author Comment

by:rhservan
ID: 36583425
Marappan,

Please reengage!!
0
 
LVL 2

Expert Comment

by:marappan
ID: 36587634
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question