Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I add a surrogate key to a table?

Posted on 2011-09-22
8
Medium Priority
?
752 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 2000 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

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.

Question has a verified solution.

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

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.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

927 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