Solved

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

Posted on 2004-10-18
7
2,980 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Code not seeing ODBC driver for Sybase 1 1,038
sybase optimizer statistics 2 38
SQL Query Help! 11 116
sybase 3 37
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

22 Experts available now in Live!

Get 1:1 Help Now