Results 1 to 3 of 3

Thread: How to add a 12 digit auto incremental field in sql?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to add a 12 digit auto incremental field in sql?

    How to add a 12 digit auto incremental field in sql? I would like to create an application in Qt which has 3 fields, name, date of birth and pincode. If user gives all the information including the pin code, it should be inserted in the database. But the unique id field will be updated automatically with the pincode (6 digit)+ 6 digit.. For instance, if the user gives the pincode as 641001 then, it should be added as 641001000001, and if another user with same pin code enters the details, then it should be added as 641001000002. And if another user with different pincode 552145 then it should be added as 552145000001.. and so on.. It should check for pincode if already exists, then add 1 to the last digit and update it. and it should be unique.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to add a 12 digit auto incremental field in sql?

    Qt does not has any built-in class to do so. I don't think one can find such a class in any programming framework. It is highly application specific.

    You will have to write for yourself using some help from QString and QRegExp

    How to add a 12 digit auto incremental field in sql?
    I don't think SQL has this ability.

    I have a suggestion, instead of concatenating the zip and serial number, have zip code as a field, and serial number in another field. Then zip and serial fields put together will be the primary key. That way querying the rows with same zip code will be possible using standard SQL, an then you could look for the largest serial number.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to add a 12 digit auto incremental field in sql?

    My heart sinks every time is see yet another database "design" with keys encoding information for mutable fields. I've seen a lot of them in my time and it rarely ends well.

    The unique identifier of a database record should either be:
    • An immutable natural key based one or more of the actual data fields in the record, or
    • An immutable artificial identifier (surrogate key) that has no intrinsic meaning and nothing to do with the data in the record.

    Your data may occasionally provide the first, though its generally not the case, and most SQL implementations have mechanisms to do the latter with little effort (e.g. Sqlite's INTEGER PRIMARY KEY). Using a database key that has some "meaning" to the user is subject to changing whims... and they will change. Using a database key that can change just makes handling data uglier because you have to stay on top of primary key changes caused by changing the data fields it is derived from, and then foreign key references that are broken as a result. Eventually you will miss something and the database integrity is broken.

    If the user expects to see a bastard "key" like the one you describe then please build it for display only, do not use it as the basis of a database key.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. incremental plotting
    By shiv.072007 in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2012, 10:40
  2. Incremental Spectrogram...
    By uppu in forum Qwt
    Replies: 11
    Last Post: 13th April 2011, 13:47
  3. QDate and two digit year
    By Lesiok in forum Qt Programming
    Replies: 8
    Last Post: 24th March 2011, 09:13
  4. incremental 2d plotter with QGraphicsView
    By bingofuel in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2009, 06:03
  5. QDateEdit and 2 digit years
    By Byngl in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2006, 23:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.