Solved

How do I add a surrogate key to a table?

Posted on 2011-09-22
8
749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

624 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