Results 1 to 6 of 6

Thread: Increment Primary key

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Increment Primary key

    Hallo,

    I am working with Sql server 2005 and Vb.net.
    I have a table with 3 fields - pay_id, pay_name and account_num.

    pay_id is set as Primary Key and is automatically incremented when a record is added. Now, when you delete a record(s), and add another record the pay_id remembers the last number (even if deleted) and increments to create the new pay_id. For example, I had the records below

    pay_id pay_name account_num

    2 John 7836434
    3 Ali 3473443
    4 James 8243245

    If the last record is deleted (pay_id = 4), the next new record will have a pay_id of 5 as shown below;

    pay_id pay_name account_num
    2 John 7836434
    3 Ali 3473443
    5 Julius 2752457

    Notice, pay_id is 5 rather than 4. Therefore I have two problems;

    1) I want to amend this so that when a record is deleted then it reads the most present last record and increment the pay_id..
    2) My table (tblpay) pay_id's are so random due to deletion of records and creating new records. What will be the easier way of having pay_id's start from 1 onwards in an incremental fashion...

    Thanks

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Increment Primary key

    They aren't random at all. An AUTO_INCREMENT (or IDENTITY, as MS calls it) column does just what it says it does: it increments a counter to provide for a unique row, even if other data is the same. It will NEVER insert into a previously used row ID, nor should you force it to.
    Last edited by Campion; Oct 9th, 2009 at 07:16 AM.
    From my burrow, 2 feet under.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Increment Primary key

    Quote Originally Posted by dr223 View Post
    I want to amend this so that when a record is deleted then it reads the most present last record and increment the pay_id
    Why? What purpose would this serve?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Increment Primary key

    It's neat?

    Technically, you can do this by altering the settings for the primary key field, but don't do it.
    My usual boring signature: Nothing

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Increment Primary key

    Quote Originally Posted by dr223 View Post
    2 John 7836434
    3 Ali 3473443
    4 James 8243245
    What if you delete records from in between (say record #3). Won't you still have records out of order?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Increment Primary key

    What does it matter if the ID's are neatly in order without skipping numbers? The ID is usually just a number to identify that particular record with. The user, nor you really, should never have to even see it. Your application should function the same way whether the ID's are neatly in order or not.

    If you still REALLY want to do this, perhaps an option is to create another column, DELETED, which tells you if that particular record is deleted or not. This way, you never actually delete a record, but you merely set the DELETED flag (set it to 1, for example, when 'deleted', and 0 otherwise). Then, in all your queries, you simply add a where clause "where DELETED = 0". The behavior will be the same as if you had really deleted the rows, and your ID's should remain neatly in order, and you have to added advantage of the customer never losing records, as they are still in the table. You could then create a simple 'restore' functionality that resets the DELETED flag of any deleted records. Obviously, this may not be an option if you are working with huge tables where you simply need to remove records to be able to cope with the size.

Posting Permissions

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



Click Here to Expand Forum to Full Width