Uploaded image for project: 'Derby'
  1. Derby
  2. DERBY-783

Enhance ALTER TABLE syntax to allow users to change the next value to be generated for an identity column

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 10.2.1.6
    • 10.2.1.6
    • SQL
    • None

    Description

      Derby allows a user to change the interval between consecutive values of the identity column using ALTER TABLE. But there is no way to change the next value to be generated for an identity column. Such a support in Derby will be very handy for tables with identity column defined as GENERATED BY DEFAULT and with a unique key defined on them. Column defined with GENERATED BY DEFAULT allows system to generate values for them or allows the user to manually supply the value for them. A column defined this way is very useful when the user might want to import some data into the generated column manually. But this can create problems when the system generated values conflict with manually inserted values.

      eg
      autocommit on;
      create table tauto(i int generated by default as identity, k int);
      create unique index tautoInd on tauto;
      insert into tauto(k) values 1,2; – let system generate values for the identity column
      – now do few manual inserts into identity column
      insert into tauto values (3,3);
      insert into tauto values (4,4);
      insert into tauto values (5,5);
      insert into tauto values (6,6);
      insert into tauto values (7,7);
      insert into tauto values (8,8);
      – notice that identity column at this point has used 1 through 8
      – now if the user wants to let the system generate a value, system will generate 3 but that is already used and hence
      – insert will throw unique key failure exception. System has consumed 3 at this point.
      insert into tauto(k) values 9;
      – the insert above will continue to fail with the unique key failure exceptions until system has consumed all the values till 8
      – If we add ALTER TABLE syntax to allow changing the next value to be generated, then user can simply use that to change
      – next value to be generated to 9 after the manual inserts above and then insert into tauto(k) values 9 will not fail

      SQL standard syntax for changing the next generated value
      ALTER TABLE <tablename> ALTER <columnName> RESTART WITH integer-constant

      Attachments

        1. Derby783AlterTableRestartWith010306.txt
          36 kB
          Mamta A. Satoor
        2. Derby783AlterTableRestartWith011706.txt
          36 kB
          Mamta A. Satoor
        3. Derby783AlterTableRestartWith012206.txt
          2 kB
          Mamta A. Satoor
        4. Derby783AlterTableRestartWith012506.txt
          2 kB
          Mamta A. Satoor
        5. Derby783AlterTableRestartWith012606.txt
          2 kB
          Mamta A. Satoor
        There are no Sub-Tasks for this issue.

        Activity

          People

            mamtas Mamta A. Satoor
            mamtas Mamta A. Satoor
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: