Solved

add an auto-increment field and populate to an existing table with data

Posted on 2004-10-18
7
3,019 Views
Last Modified: 2010-05-18
How do I add an auto-increment field, or identity seed to an existing table?  Does it have to be a primary key?
0
Comment
Question by:amhinkel
[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
  • 2
  • 2
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12342046
Not really an "expert", but :

http://manuals.sybase.com/onlinebooks/group-as/asg1200e/whatsup/@Generic__BookTextView/859;pt=460

Identity columns are by there very nature primary keys.  Each value must be unique, but the value is unimportant.

Bob
0
 
LVL 19

Accepted Solution

by:
grant300 earned 125 total points
ID: 12342488
The ALTER TABLE command will do it for you.  It not only adds the column, but when defined with the IDENTITY qualifier also populates it.

Below is a complete example.  The line you want to pay attention to is "ALTER TABLE ADD <column_name> <datatype> IDENTITY"
Remember that you must use a numeric or decimal data type with for identity columns with no decimal points.  These are the only "exact" datatypes in Sybase.

==============

create table fubar (col1 int, col2 varchar(32))
go

insert into fubar values (1,'one')
insert into fubar values (2,'two')
insert into fubar values (3,'three')
insert into fubar values (4,'four')
insert into fubar values (5,'five')
insert into fubar values (6,'six')
commit
go

select * from fubar
go

alter table fubar add col0 numeric(12) identity
go

select * from fubar
go
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12346273
Yeah, that's a better explanation than posting a help reference for the ALTER TABLE command  :)

Bob
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:amhinkel
ID: 12347109
Thank you both.  I meant to accept the one from grant300 as the Accepted Answer.  How do I fix this?
0
 

Author Comment

by:amhinkel
ID: 12347202
I requested to have the points transferred to grant300 through Customer Support.  Thanks again!
0
 
LVL 19

Expert Comment

by:grant300
ID: 12348124
amhinkel,

You are very welcome, and thank you for going out of your way to fix the points.  I appreciate it.

Regards,
Bill
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Here's how to start interacting with our community through Post.
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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