Solved

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

Posted on 2004-10-18
7
2,964 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
  • 2
  • 2
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
Yeah, that's a better explanation than posting a help reference for the ALTER TABLE command  :)

Bob
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:amhinkel
Comment Utility
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
Comment Utility
I requested to have the points transferred to grant300 through Customer Support.  Thanks again!
0
 
LVL 19

Expert Comment

by:grant300
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A safe way to clean winsxs folder from your windows server 2008 R2 editions
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now