Find the answer to your Linux question:
Results 1 to 9 of 9
How would I go about creating a unique ID for every row in an SQL database? So, if I had a table with ID's of 1, 2, 3,5, 8, how ...
  1. #1
    Just Joined! Alboin's Avatar
    Join Date
    Jan 2007
    Posts
    59

    SQL unique ID

    How would I go about creating a unique ID for every row in an SQL database? So, if I had a table with ID's of 1, 2, 3,5, 8, how would I find an unused number in the list? Is there something for this in SQL? (I'm using sqlite in a C program, if that matters.)

    Thanks!

  2. #2
    Just Joined!
    Join Date
    Mar 2007
    Posts
    8
    you could create a sequencing table, and then when an new row is inserted, select the value out of the seq table, then update it plus one.

    edit:
    If you are using php, pear DB has a great sequencing feature built in.
    Last edited by flann; 04-26-2007 at 07:18 PM. Reason: update info

  3. #3
    Just Joined! Alboin's Avatar
    Join Date
    Jan 2007
    Posts
    59
    Yeah, but what if an object is deleted? Then there is a space in the table.....

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Important is to have records with a unique ID in your table. For new records just add 1 to the last ID-number.

    The SQL statement to find out the last ID-number is:
    Code:
    select max(ID) as maxID from your_table;
    Regards

  5. #5
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    Quote Originally Posted by Franklin52
    The SQL statement to find out the last ID-number is:
    Code:
    select max(ID) as maxID from your_table;
    Regards

    I dont think this will work for what he was looking for. What i read from the question was say he had the Unique ID's 1, 2, 3, 5, 8. If you added some new data, it wouldn't add it as ID 9, but in fact, add it as 4 since that's the lowest unique ID. Your method i agree is the simplest way, and had it been me, i would just set the table as an integer and set it as an auto_increment, that way passing NULL as the value will auto-asign it.

    In order to assign 4 however, i can only think of one way to do so... retreive all data sorted by the Unique ID, and loop through it for the first available Unique ID.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  6. #6
    Just Joined!
    Join Date
    Mar 2007
    Posts
    8
    Quote Originally Posted by sdousley
    Your method i agree is the simplest way, and had it been me, i would just set the table as an integer and set it as an auto_increment, that way passing NULL as the value will auto-asign it.
    I might be wrong, but I believe that sqlite doesn't have the auto_increment ability that mysql does.

  7. #7
    Just Joined! Alboin's Avatar
    Join Date
    Jan 2007
    Posts
    59
    Thanks all! I think I'm going to go with the simpler method, as it doesn't really matter that much anyway. (The other way just would have been cleaner.)

    I have another question, however: I have an array of numbers to store. Is there an array type with SQL? If not, how would you suggest I do such?

    Thanks!

  8. #8
    Just Joined!
    Join Date
    Mar 2007
    Posts
    8
    Quote Originally Posted by flann
    I might be wrong, but I believe that sqlite doesn't have the auto_increment ability that mysql does.
    http://www.sqlite.org/lang_createtable.html

    AUTOINCREMENT, among a few others are support in SQLite3 and I believe 2 aswell.

    I have another question, however: I have an array of numbers to store. Is there an array type with SQL? If not, how would you suggest I do such?
    If you know the array will always be X element long, do something like "num1 INT, num2 INT, numX INT". If not, you ~could~ use a space separated list of numbers, in a STRING. Depending on the programing language used to access the data you may even be able to access the raw binary data of the array and put it in a "BLOB"? I think it's called blob anyways. Serialized can usually spit out thier data to a BLOB and back in, but some serialized data isn't compatible between different languages and even different versions of the same language.

  9. #9
    Just Joined! Alboin's Avatar
    Join Date
    Jan 2007
    Posts
    59
    Quote Originally Posted by deadram
    If you know the array will always be X element long, do something like "num1 INT, num2 INT, numX INT". If not, you ~could~ use a space separated list of numbers, in a STRING.
    Hm, I think I'll use a string, as that should be a bit more friendly. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...