How to fill empty column with number sequence?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken1

    How to fill empty column with number sequence?

    I am going to drop a primary key from one column and create a new
    column to be used as primary key in an existing database.
    The old column was a date column which someone earlier though was a
    good candidate for a primary key which we all know it's not.

    Now I want to add a new field, i.e. called ID, with a normal number
    sequence as primary key.

    I have dropped the primary key, created the new column, created the
    new sequence and created a trigger to generate the primary key for new
    inserts.

    But I don't know how to generate/update the new ID column with
    sequence numbers for the already existing records.

    Any ideas?

    /Kenneth
  • Ed prochak

    #2
    Re: How to fill empty column with number sequence?

    ken1@tjohoo.se (Ken1) wrote in message news:<3bb6578e. 0402130011.75bd 6b51@posting.go ogle.com>...
    I am going to drop a primary key from one column and create a new
    column to be used as primary key in an existing database.
    The old column was a date column which someone earlier though was a
    good candidate for a primary key which we all know it's not.
    >
    Now I want to add a new field, i.e. called ID, with a normal number
    sequence as primary key.
    >
    I have dropped the primary key, created the new column, created the
    new sequence and created a trigger to generate the primary key for new
    inserts.
    >
    But I don't know how to generate/update the new ID column with
    sequence numbers for the already existing records.
    >
    Any ideas?
    >
    /Kenneth
    A simple PL/SQL process can do it.
    open the table for all rows,
    count each row fetched,
    and update the ID to the count value.

    HTH,
    ed

    Comment

    • Jia Lian Chang

      #3
      Re: How to fill empty column with number sequence?

      You can create a temporary trigger that fires on updates. It will be
      somewhat similar to your insert trigger, e.g.
      if (:new.id = -1) then
      :new.id := seq_name@nextva l;

      Then, just update your whole table:
      update tablename
      set id = -1;

      Remove the temporary update trigger after that.


      Comment

      • boa

        #4
        Re: How to fill empty column with number sequence?

        Ken1 wrote:
        I am going to drop a primary key from one column and create a new
        column to be used as primary key in an existing database.
        The old column was a date column which someone earlier though was a
        good candidate for a primary key which we all know it's not.
        >
        Now I want to add a new field, i.e. called ID, with a normal number
        sequence as primary key.
        >
        I have dropped the primary key, created the new column, created the
        new sequence and created a trigger to generate the primary key for new
        inserts.
        >
        But I don't know how to generate/update the new ID column with
        sequence numbers for the already existing records.
        >
        Any ideas?
        How about using rownum to generate id's for the existing rows?

        alter table foo add bar number;
        update foo set bar = rownum;

        Remember to create the sequence with the proper "START WITH" number,
        which should be max(bar) + 1.

        HTH
        Boa
        >
        /Kenneth

        Comment

        • steve

          #5
          Re: How to fill empty column with number sequence?

          On Fri, 13 Feb 2004 16:11:25 +0800, Ken1 wrote
          (in article <3bb6578e.04021 30011.75bd6b51@ posting.google. com>):
          I am going to drop a primary key from one column and create a new
          column to be used as primary key in an existing database.
          The old column was a date column which someone earlier though was a
          good candidate for a primary key which we all know it's not.
          >
          Now I want to add a new field, i.e. called ID, with a normal number
          sequence as primary key.
          >
          I have dropped the primary key, created the new column, created the
          new sequence and created a trigger to generate the primary key for new
          inserts.
          >
          But I don't know how to generate/update the new ID column with
          sequence numbers for the already existing records.
          >
          Any ideas?
          >
          /Kenneth
          create a sequence.
          when you add the new col to the table ,
          in the default value for the col , query the sequence.

          this will add a new col with the values already installed.

          then for future additions add a trigger. that calls the sequence whenever the
          table is inserted.

          or you could be real lazy an just leave the default value in place.



          Comment

          • Mark C. Stock

            #6
            Re: How to fill empty column with number sequence?

            "steve" <me@me.comwro te in message
            news:0001HW.BC5 A003200008C96F0 3055B0@news.new sguy.com...
            | On Fri, 13 Feb 2004 16:11:25 +0800, Ken1 wrote
            | (in article <3bb6578e.04021 30011.75bd6b51@ posting.google. com>):
            |
            | I am going to drop a primary key from one column and create a new
            | column to be used as primary key in an existing database.
            | The old column was a date column which someone earlier though was a
            | good candidate for a primary key which we all know it's not.
            | >
            | Now I want to add a new field, i.e. called ID, with a normal number
            | sequence as primary key.
            | >
            | I have dropped the primary key, created the new column, created the
            | new sequence and created a trigger to generate the primary key for new
            | inserts.
            | >
            | But I don't know how to generate/update the new ID column with
            | sequence numbers for the already existing records.
            | >
            | Any ideas?
            | >
            | /Kenneth
            |
            | create a sequence.
            | when you add the new col to the table ,
            | in the default value for the col , query the sequence.
            |
            | this will add a new col with the values already installed.
            |
            | then for future additions add a trigger. that calls the sequence whenever
            the
            | table is inserted.
            |
            | or you could be real lazy an just leave the default value in place.
            |
            |
            |

            you can't use a sequence as a default value

            from the SQL manual:

            --------------------------------------------------------------------------
            DEFAULT specifies a value to be assigned to the column if a subsequent
            INSERT statement omits a
            value for the column. The datatype of the expression must match the datatype
            of the
            column. The column must also be long enough to hold this expression. For the
            syntax of
            expr, see "Expression s" on page 5-1.
            Restriction: A DEFAULT expression cannot contain references to other
            columns, the
            pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM, or date constants that
            are not
            fully specified.
            --------------------------------------------------------------------------

            note also that the default value can be overridden, including with an
            explicit null (making default values basically useless)

            but you can use the sequence in an update clause

            update x
            set id = x_seq.nextval;

            -- mcs


            Comment

            Working...