If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Auto increment ID as Primary key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-10, 15:40
DevilsAdvocate DevilsAdvocate is offline
Registered User
 
Join Date: Apr 2010
Posts: 24
Auto increment ID as Primary key

Ok, so I have a bunch of questions stored in a table, and I used Ques_ID as the auto-incremented primary key. A friend then warned me that if I deleted a question in the middle, it might cause some issues, and he was right. If I remove question #3, it doesn't change the number to reflect that so it goes from #2 to #4. And to top it off, when I add a #5, it stores it where #3 was. Has anybody ever experienced this issue and found a solution? Or maybe you determined it wasn't an issue at all. Any opinions would be appreciated.
__________________
DevilsAdvocate
------------------------
The elephant in the room
------------------------
Ordo ab chao
Reply With Quote
  #2 (permalink)  
Old 04-15-10, 16:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
your friend should get out more

there is no issue
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-15-10, 16:27
DevilsAdvocate DevilsAdvocate is offline
Registered User
 
Join Date: Apr 2010
Posts: 24
Quote:
Originally Posted by r937 View Post
your friend should get out more
Well, that's one thing that's true

Quote:
Originally Posted by r937 View Post
there is no issue
I tested it. It is a problem. Well, potentially at least. I added 4 questions. I then deleted ID #3. It then read ID 1,2,4. So I added a new one and let the autoincrement take care of its number and it numbered it 5 and stuck it between 2 and 4, so it read ID 1,2,5,4.

My question is: what if you're iterating through a table or something looking for the first 5 ID's. It would throw them in the stored order which is 1,2,5,4,6which could become a problem couldn't it? Do you get what I mean? I hope I'm explaining that ok.
__________________
DevilsAdvocate
------------------------
The elephant in the room
------------------------
Ordo ab chao
Reply With Quote
  #4 (permalink)  
Old 04-15-10, 16:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
ur doing it wrong

there is no sequence to the rows in a table -- they are stored wherever the engine wants to put them, and they are ~not~ inserted where there is a "gap"

the gaps do not matter, full stop

the only sequence you will ever get is when you use an ORDER BY clause in your query

you want the lowest 5 ids?

simple --

SELECT id FROM daTable ORDER BY id LIMIT 5
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-15-10, 16:34
DevilsAdvocate DevilsAdvocate is offline
Registered User
 
Join Date: Apr 2010
Posts: 24
Quote:
Originally Posted by r937 View Post
ur doing it wrong

there is no sequence to the rows in a table -- they are stored wherever the engine wants to put them, and they are ~not~ inserted where there is a "gap"

the gaps do not matter, full stop

the only sequence you will ever get is when you use an ORDER BY clause in your query

you want the lowest 5 ids?

simple --

SELECT id FROM daTable ORDER BY id LIMIT 5
So it won't matter? As long as the ID remains the same , I can pull out as many as I want before and it will still find it based on the ID no matter where it is stored in chronological order in the table? (Not arguing, clarifying)
__________________
DevilsAdvocate
------------------------
The elephant in the room
------------------------
Ordo ab chao
Reply With Quote
  #6 (permalink)  
Old 04-15-10, 16:44
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
The sole purpose of an autonumber field (aka Sequence or Identity) is to provide a number that is guaranteed to be unique. That's all. It's typically used in cases where you wish to use a surrogate primary key, rather than a natural key, in your table.

Unless you specifically provide the order in which you wish data to be presented, no database is guaranteed to present the data in any particular order. (the ORDER BY clause in a select statement is needed)

If your needs include the requirement that a set of records be presented in a particular order which may be different than the order of your system's autonumber field, then you would need a field in your table to be used for ordering/sequencing of the results.


i.e. If you want to have a specific order of presentation that is independent of the order in which a record may be added, you need to 'roll your own' ordering mechanism.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Last edited by loquin; 04-15-10 at 16:48.
Reply With Quote
  #7 (permalink)  
Old 04-15-10, 17:04
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by loquin View Post
The sole purpose of an autonumber field (aka Sequence or Identity) is to provide a number that is guaranteed to be unique.
I don't want to take the thread off course but this isn't true. I can't speak for all engines but for the ones I am familiar with there is no constraint attached to an identity or autonumber column. You can have unlimited rows in a SQL Server or Access table all with the same value in the identity or autonnumber column.

I couldn't leave this unchallenged because it is a commonly held misconception that identities are inherently unique.
Reply With Quote
  #8 (permalink)  
Old 04-15-10, 17:47
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
You are correct, in that the enforcement of uniqueness must come from the constraint on the field. However, unless you reset the identity, (or it wraps around,) the number it provides is unique in the set of numbers generated by the identity. The same is true of sequences.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #9 (permalink)  
Old 04-15-10, 18:00
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Or insert values explicitly.

I thought you would be aware of this. It is one of my pet hates (and sadly very common in the Access forum) when primary keys and autonumbers are considered synonymous.
Reply With Quote
  #10 (permalink)  
Old 04-15-10, 19:13
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
"with great power, comes great responsibility, grasshopper..."

You actually have to USE the sequence/identity to reap the benefits. And, unless you write a trigger to drop any autonumber field's value before it is inserted, you can't FORCE database servers to only use the identity/sequence value...
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #11 (permalink)  
Old 04-15-10, 22:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by loquin View Post
You actually have to USE the sequence/identity to reap the benefits.
i didn't understand what you meant here

Quote:
Originally Posted by loquin View Post
And, unless you write a trigger to drop any autonumber field's value before it is inserted, you can't FORCE database servers to only use the identity/sequence value...
or here

care to elaborate?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 04-16-10, 12:38
DevilsAdvocate DevilsAdvocate is offline
Registered User
 
Join Date: Apr 2010
Posts: 24
Quote:
Originally Posted by loquin View Post
The sole purpose of an autonumber field (aka Sequence or Identity) is to provide a number that is guaranteed to be unique. That's all. It's typically used in cases where you wish to use a surrogate primary key, rather than a natural key, in your table.

Unless you specifically provide the order in which you wish data to be presented, no database is guaranteed to present the data in any particular order. (the ORDER BY clause in a select statement is needed)

If your needs include the requirement that a set of records be presented in a particular order which may be different than the order of your system's autonumber field, then you would need a field in your table to be used for ordering/sequencing of the results.


i.e. If you want to have a specific order of presentation that is independent of the order in which a record may be added, you need to 'roll your own' ordering mechanism.
That's all I want it for, to query questions and answers to display them.
The question ID is the question number, at least in theory, so would that be a problem if I wanted to show 1-5? Is my understanding correct?
__________________
DevilsAdvocate
------------------------
The elephant in the room
------------------------
Ordo ab chao
Reply With Quote
  #13 (permalink)  
Old 04-16-10, 12:56
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
Quote:
Originally Posted by r937 View Post
i didn't understand what you meant here
Quote:
Originally Posted by loquin
You actually have to USE the sequence/identity to reap the benefits.
What I meant was this: If you insert explicit values into the field, thus overriding the default identity/sequence, you aren't actually using the identity/sequence value. It was in response to poot's reply 'Or insert values explicitly.'

Quote:
Originally Posted by r937 View Post
or here
Quote:
Originally Posted by loquin
And, unless you write a trigger to drop any autonumber field's value before it is inserted, you can't FORCE database servers to only use the identity/sequence value...
care to elaborate?
yeah - that was a bit ... muddy. When you insert/update, you can supply a value to the ID field. The value you supply value will override the default value of the sequence/identity.) AFAIK, there's no way to specify that the server will only use the identity/sequence, other than writing a before trigger to set the explicitly supplied [new] value to null, allowing the default constraint to be applied.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Last edited by loquin; 04-16-10 at 13:05.
Reply With Quote
  #14 (permalink)  
Old 04-16-10, 12:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by DevilsAdvocate View Post
Is my understanding correct?
it certainly is

therefore you should not be using auto_increment for the question number -- rather, you should assign the question number value manually as you add each new question to the table

and of course it would still be your primary key

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 04-16-10, 13:01
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
Quote:
Originally Posted by DevilsAdvocate View Post
That's all I want it for, to query questions and answers to display them.
The question ID is the question number, at least in theory, so would that be a problem if I wanted to show 1-5? Is my understanding correct?
So, since any automatically generated field could have gaps, you would either explicitly supply the question id, or keep the question number in a separate, distinct field from any identity/sequence/autonumber field. And, order by the question number when you select data from the table.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On