Autonumber in a SQL Server table

Reply

Join Date: May 2005
Posts: 3
Reputation: Madmax is an unknown quantity at this point 
Solved Threads: 0
Madmax Madmax is offline Offline
Newbie Poster

Autonumber in a SQL Server table

 
0
  #1
May 17th, 2005
I know this is probably a stupid post but I cannot think of how to accomplish this. How do you do the equivilent to an autonumber in a SQL Server table? Please let me know my project is very close to deadline
Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Autonumber in a SQL Server table

 
0
  #2
May 17th, 2005
Ok, just to clarify? Replicate autonumber in SQL how? You mean within an SQL function/store procedure or in another coding lanuage (if so which one)?

IDENTITY(x,y) is how you use autonumber in SQL ( x = starting point, and y = increment by)
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: Madmax is an unknown quantity at this point 
Solved Threads: 0
Madmax Madmax is offline Offline
Newbie Poster

Re: Autonumber in a SQL Server table

 
0
  #3
May 17th, 2005
Originally Posted by Paladine
Ok, just to clarify? Replicate autonumber in SQL how? You mean within an SQL function/store procedure or in another coding lanuage (if so which one)?

IDENTITY(x,y) is how you use autonumber in SQL ( x = starting point, and y = increment by)

I just needed to know syntax on how to get an autonumber like column for MS SQL Server. So in your example if my field was "bill" lets say when I set it up in the design table area of SQL server I would enter "bill" in the column name field and then enter numeric in the data type field. Then set up the incrementation in a stored proc? Am I in the ball park here?
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Autonumber in a SQL Server table

 
0
  #4
May 18th, 2005
Well kind of.

Ok here is the syntax on creating a table in SQL and having an autonumber column
  1. CREATE TABLE NorthWindUsers
  2. (UserID INT IDENTITY(1,1) NOT NULL,
  3. UserName VARCHAR(50) NOT NULL,
  4. Password VARCHAR(50) NOT NULL)

The userID will start a 1, and increment each time you add a new user to the table; automatically


Hope this helps
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: relawson is an unknown quantity at this point 
Solved Threads: 0
relawson relawson is offline Offline
Newbie Poster

Re: Autonumber in a SQL Server table

 
0
  #5
Jun 11th, 2005
Originally Posted by Paladine
Well kind of.

Ok here is the syntax on creating a table in SQL and having an autonumber column
  1. CREATE TABLE NorthWindUsers
  2. (UserID INT IDENTITY(1,1) NOT NULL,
  3. UserName VARCHAR(50) NOT NULL,
  4. Password VARCHAR(50) NOT NULL)

The userID will start a 1, and increment each time you add a new user to the table; automatically


Hope this helps

You know, this feature isn't in Oracle as I recently discovered. Is it considered bad to use autoincrementing numbers? Certainly makes it easy to maintain a primary key with little effort. I am just trying to understand in what situation you wouldn't want to use that feature. Obviously Oracle doesn't seem to want you to use it at all.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Autonumber in a SQL Server table

 
0
  #6
Jun 12th, 2005
Originally Posted by relawson
You know, this feature isn't in Oracle as I recently discovered. Is it considered bad to use autoincrementing numbers? Certainly makes it easy to maintain a primary key with little effort. I am just trying to understand in what situation you wouldn't want to use that feature. Obviously Oracle doesn't seem to want you to use it at all.
I don't know that Oracle thinks it is a bad thing, they just choose to solve the problem a different way--with "sequences". In Oracle, as you may know, a sequence is it's own thing outside of any table. Personally, I wish the databases I work with had both an autonumber/identity and a feature like a sequence. They are both very handy. A sequence can be useful if you need a way to have an id unique across multiple tables or multiple databases.

It's easy enough to code your own sequence-like feature using a one column/one row table and a function, but I assume Oracle's sequence feature is highly optimized to do what it does.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: relawson is an unknown quantity at this point 
Solved Threads: 0
relawson relawson is offline Offline
Newbie Poster

Re: Autonumber in a SQL Server table

 
0
  #7
Jun 12th, 2005
Originally Posted by Troy
I don't know that Oracle thinks it is a bad thing, they just choose to solve the problem a different way--with "sequences". In Oracle, as you may know, a sequence is it's own thing outside of any table. Personally, I wish the databases I work with had both an autonumber/identity and a feature like a sequence. They are both very handy. A sequence can be useful if you need a way to have an id unique across multiple tables or multiple databases.

It's easy enough to code your own sequence-like feature using a one column/one row table and a function, but I assume Oracle's sequence feature is highly optimized to do what it does.
Perhaps I don't understand the functionality of the sequence clearly, how exactly are you to you use the sequence if it isn't attached to an individual entity? Or are you saying the sequence is like a counter maintained at the DB level which updates a regular identifying entity? If that is the case, that seems great.

Unfortunately the Oracle database I am working on at work for a supply chain application is so de-normalized I don't think uniqueness was too much of an issue when designing the schema.

I don't understand why people still design tables which uses multiple columns to create a unique primary key, each column by itself being non-unique. They must have been out back smoking pot during that class. :eek:

We have serious data integrity problems, as you can imagine.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Autonumber in a SQL Server table

 
0
  #8
Jun 12th, 2005
I found a page on techonthenet.com that explained an Oracle sequence better than I could have, so I plagerized it. Below is what it said. I think it makes it clear that a sequence object maintains a unique number but is not tied to a table entitity. Yes, in practice, you normally create a sequence for a specific table, and you then only use it on that one table, but there is nothing about the sequence that prevents you from using it anytime you want. You can even just "SELECT supplier_seq.nextval" to increment the counter arbitrarily.

Quoted from techonthenet.com:
----------------------------------------------------------
In Oracle, you can create an autonumber field by using sequences. A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.

The syntax for a sequence is:
  1. CREATE SEQUENCE sequence_name
  2. MINVALUE value
  3. MAXVALUE value
  4. START WITH value
  5. INCREMENT BY value
  6. CACHE value;

For example:
  1. CREATE SEQUENCE supplier_seq
  2. MINVALUE 1
  3. MAXVALUE 999999999999999999999999999
  4. START WITH 1
  5. INCREMENT BY 1
  6. CACHE 20;

This would create a sequence object called supplier_seq. The first sequence number that it would use is 1 and each subsequent number would increment by 1 (ie: 2,3,4,...}. It will cache up to 20 values for performance.

Now that you've created a sequence object to simulate an autonumber field, we'll cover how to retrieve a value from this sequence object. To retrieve the next value in the sequence order, you need to use nextval.

For example:

supplier_seq.nextval

This would retrieve the next value from supplier_seq. The nextval statement needs to be used in an SQL statement. For example:
  1. INSERT INTO suppliers
  2. (supplier_id, supplier_name)
  3. VALUES
  4. (supplier_seq.NEXTVAL, 'Kraft Foods');

This insert statement would insert a new record into the suppliers table. The supplier_id field would be assigned the next number from the supplier_seq sequence. The supplier_name field would be set to Kraft Foods.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: relawson is an unknown quantity at this point 
Solved Threads: 0
relawson relawson is offline Offline
Newbie Poster

Re: Autonumber in a SQL Server table

 
0
  #9
Jun 12th, 2005
I believe that would be superior DB design because not only do you have a unique PK for each identifying row, it is unique across the DB. Maybe that is 4 or 5 NF...I never read much past 3 :-) I only wish identifying columns would autoincrement the "master counter" so it was automated like MS SQL. A combination of the two if you will. That may present a locking issue...but I am sure they thought of that.

Thanks for the great response.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 121
Reputation: jwshepherd is an unknown quantity at this point 
Solved Threads: 5
jwshepherd's Avatar
jwshepherd jwshepherd is offline Offline
Junior Poster

Re: Autonumber in a SQL Server table

 
0
  #10
Jun 16th, 2005
Mad Max
colum name = id
data type = int


down at the colum properties box change
identity to yes
identity seed to what number you want to start using
identity increment to how much the seed is incremented each time.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC