[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I add PRIMARY KEY/IDENTITY attributes to existing column in MS SQL 2000?

Posted on 2007-07-23
6
Medium Priority
?
5,931 Views
Last Modified: 2008-07-09
Hello all,

I'm having a problem with Microsoft SQL Server 2000 and am hoping to benefit from the wisdom of the experts out there.  Here's what's going on...

After making a stupid mistake in a script I wrote and having to restore one of the tables from previous backups using EMC Retrospect, I realized that, while Retrospect restores all the data in a given table without a problem, attributes such as default values, primary key, etc. are stripped from all columns.

I was able to add the default values back to all columns using the query analyzer.  But one of the columns in the restored table, called "acctid", is an identity column and primary key, and I do not know how to add those attributes back to the column, so that it will continue to auto increment its value every time a record is added to the table.

I'm not terribly familiar with Microsoft SQL Server, so if anyone can provide me with insight on how to write a SQL query that will set this column back to the primary key/identity column, I will be extremely grateful.

- Tristan
0
Comment
Question by:alphact
6 Comments
 
LVL 8

Expert Comment

by:k_rasuri
ID: 19551541
alter table and add colum with identity constraint

ALTER TABLE Yourtable
ADD Yourcolumn IDENTITY(1,1) NOT NULL
0
 
LVL 8

Expert Comment

by:k_rasuri
ID: 19551568
or to make it a primary key

ALTER TABLE Yourtable
ADD Yourcolumn IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED

when you add this column, it will automatically assingn the unique keys for each column. in this way you can restore your primary key...but make sure that the data is same as before in your new table. even if the order is changed..you wont get teh same primary keys for rows
0
 
LVL 35

Expert Comment

by:David Todd
ID: 19551784
Hi,

When recently commenting on a similar question
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_22703487.html#a19519262
I found that SQL wont allow adding not null columns without a default to an existing table.

I wasn't trying this with identity's so I'm not sure if the above will work or not.

Regards
  David
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 19552033
You will have to have exclusive use of the table for a while.

First, script out the existing table.  Keep that script.

Then rename the table:
EXEC sp_rename 'tableName', 'tableNameTemp'

Edit the script to make the "acctid" column an IDENTITY column (and a PK if desired).  Run the script.

Now, reload the table *but keeping the existing acctid values*.

SET IDENTITY_INSERT tableName ON
INSERT INTO tableName (acctid, ...rest of columns...)
SELECT *
FROM tableNameTemp
SET IDENTITY_INSERT tableName OFF

-- if PK designation has not been done yet
ALTER TABLE tableName
ADD PRIMARY KEY (acctid)

DROP TABLE tableNameTemp


You can then use the table as usual, with the identity in place.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 19552046
>> First, script out the existing table. <<
Just the table definition, not the rows themselves :-) .  [Just to make sure no misunderstandings :-) .]
0
 

Author Comment

by:alphact
ID: 19557536
Scott, thank you very much for your help.  I greatly appreciate it.  Thank you, also, to everyone else who commented.  I now have the table back to normal (as in, how it was before the Retrospect backup software messed it up), and I've learned a couple new things in the process.

Thanks again.

- Tristan
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.
Suggested Courses

872 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