Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Posts
    3

    Unanswered: How to add a sequence column to an existing table with records

    Hello,

    I had created a new table named USERLOG with two fields from a previous VIEW. The table already consist of about 9000 records. The two fields taken from the VIEW, i.e. weblog_views consist of IP (consists of IP address), and WEB_LINK (consists of URL). This is the code I used,

    Code:
    CREATE TABLE USERLOG
    AS
    SELECT C_IP, WEB_LINK FROM weblog_views;
    I want to add another column to this table called the USER_ID, which would consists of a sequence starting with 1 to 9000 records to create a unique id for each existing rows. I need help with this part. I'm using Oracle SQL Developer: ODMiner version 3.0.04.
    I tried using the AUTO-INCREMENT option,

    Code:
    ALTER TABLE USERLOG
    ADD USER_ID INT UNSIGNED NOT NULL AUTO_INCREMENT;
    But I get an error with this,

    Code:
    Error report:
    SQL Error: ORA-01735: invalid ALTER TABLE option
    01735. 00000 -  "invalid ALTER TABLE option"
    So I would really appreciate any help that I can get!

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    the best solution will make use of a SEQUENCE
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    Where did you find AUTO_INCREMENT? Could you point to documentation, please? As far as I can tell, there's no such parameter in Oracle. So the question is: do you use Oracle at all?

    If so, here's an example. I don't have your tables, so I'll create my own one. I'll also create a sequence and update existing records' USER_ID values.
    Code:
    SQL> create table userlog as
      2    select ename, job from emp;
    
    Table created.
    
    SQL> alter table userlog add user_id number;
    
    Table altered.
    
    SQL> create sequence seq_id;
    
    Sequence created.
    
    SQL> update userlog set
      2    user_id = seq_id.nextval;
    
    14 rows updated.
    For future inserts, you'd use a database trigger:
    Code:
    SQL> create or replace trigger trg_bi_ul
      2    before insert on userlog
      3    for each row
      4  begin
      5    select seq_id.nextval into :new.user_id from dual;
      6  end;
      7  /
    
    Trigger created.
    
    SQL> insert into userlog (ename, job) values ('Littlefoot', 'LURKER');
    
    1 row created.
    Finally, the result:
    Code:
    SQL> select * from userlog;
    
    ENAME      JOB          USER_ID
    ---------- --------- ----------
    SMITH      CLERK              1
    ALLEN      SALESMAN           2
    WARD       SALESMAN           3
    JONES      MANAGER            4
    MARTIN     SALESMAN           5
    BLAKE      MANAGER            6
    CLARK      MANAGER            7
    SCOTT      ANALYST            8
    KING       PRESIDENT          9
    TURNER     SALESMAN          10
    ADAMS      CLERK             11
    JAMES      CLERK             12
    FORD       ANALYST           13
    MILLER     CLERK             14
    Littlefoot LURKER            15
    
    15 rows selected.
    
    SQL>
    Of course, you can insert USER_ID value along with other values:
    Code:
    SQL> insert into userlog (ename, job, user_id)
      2    values ('Bigfoot', 'COOK', seq_id.nextval);
    
    1 row created.

  4. #4
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    From what I have read about oracle 12, it will have an identity type which is just like the identity in tsql.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

Tags for this Thread

Posting Permissions

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