Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

oracle sql plus making an existing primary key auto_increment

  • 04-10-2007 05:04PM
    #1
    Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭


    Can I change an existing primary key in a table to auto_increment?

    The table has nothing in it.


Comments

  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    there's no such thing as an auto-increment field in oracle. you can use a sequence to simulate an auto-increment field though.

    *edit* the above information is good up to Oracle 9. After that I dont have a clue


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Beano is correct in that there is no auto_incrememnt feature directly implemented in Oracle.

    I did, however, run a quick google on "oracle auto_increment" and the second hit returned gives a decent workaround.


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    Beano wrote:
    *edit* the above information is good up to Oracle 9. After that I dont have a clue

    Yip, same in 10.

    As the chaps are saying. You need a trigger and a sequence.


  • Registered Users, Registered Users 2 Posts: 12,025 ✭✭✭✭Giblet


    You just need to create a sequence, then call sequencename.nextval in your field to use it. (or currval to check what it is)


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    Giblet wrote:
    You just need to create a sequence, then call sequencename.nextval in your field to use it. (or currval to check what it is)

    Then how does it auto-increment upon an update?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    tom dunne wrote:
    Then how does it auto-increment upon an update?

    Why would you want it to auto-increment on an update?


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    bonkey wrote:
    Why would you want it to auto-increment on an update?

    Doh! Insert is of course what I meant.


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    I think what Giblet is describing is how to use a sequence to provide the functionality....which is fine if you have control over who can run inserts and make sure that this technique is always used.

    If you do it with a sequence inside a before-insert trigger, then it doesn't matter who runs inserts, nor how.


Advertisement