Avatar of shwelosa
shwelosa
 asked on

how do i set the auto sequence number in a sql table?

I need to set the auto sequence or auto counter in a sql table to add to the counter every time a new record is added from the UI.  Thanks.
Oracle DatabaseDatabases

Avatar of undefined
Last Comment
JestersGrind

8/22/2022 - Mon
JestersGrind

Add an IDENTITY column.  Like:  

CREATE TABLE Table1(Table1ID INT IDENTITY(1,1))

Greg


Muhammad Noman Iqbal

Hi shwelosa,

if we are talking about oracle then we have sequence instead of identity column, and we get next value from the sequence, so we will do something like following

-- Sequence
create sequence identity_seq
minvalue 1
maxvalue 9999999
start with 1
increment by 1
cache 5
order;
 
-- insert in table
insert in to test_table (number_col) values (identity_seq.nextval);

Open in new window

shwelosa

ASKER
Hi Nouman,

I am talking abt identity column where i can choose to put a yes in indentity and increment by 1.  my problem is that after i do it in the design function in the table, i cannot see how to start the numbering the records in the table.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
JestersGrind

When you insert data in the table, don't insert data into the identity column.  It will automatically insert the next incremented number.

Greg


Muhammad Noman Iqbal

Greg is right, If you are working in sql server or access then when you insert in the table you will not pass any value in the identity column. It will be automatically filled. Thanks.
shwelosa

ASKER
I'm sorry, but the value in the field remains a zero.  It is now an int, should it be defined as something other than an integer?  Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Anthony Perkins

Why don't you start by telling us the database and post some relevant code you are using?  Few of us have crystal balls here.
shwelosa

ASKER
Sorry.... I'm using Microsoft sql server 2000.  I created a table and i want to make one of the fields (label entry#)  the unique and primary key and it should automatically add 1 when a new record is created.
JestersGrind

Setting the label entry# field as a unique and primary key will not auto-increment the field.  If you are doing this through the GUI, you have to set the IDENTITY specification to yes and set the increment and seed to 1.

Greg


Your help has saved me hundreds of hours of internet surfing.
fblack61
shwelosa

ASKER
Yes, i have done that.  However, when i look at the records in the table, the field entry# is still zero (0).  Should i delete all the records from the table and re-imported them?  what should i set the field, entry#, as (int, varchar, char, numeric, etc....?  Thnx again.
ASKER CERTIFIED SOLUTION
JestersGrind

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.