Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.tools -> Re: Altering a table by adding a primary key column

Re: Altering a table by adding a primary key column

From: Daniel A. Morgan <dmorgan_at_exesolutions.com>
Date: Tue, 05 Jun 2001 23:13:05 -0700
Message-ID: <3B1DC9F0.D89DA79D@exesolutions.com>

Bill Hutchison wrote:

> I haven't been able to find documentation on adding a column which is to
> be the primary key of the table.
>
> I want to add a column ID which will be numeric, and should auto-increment.
> I need to populate it for existing records. Is there a datatype which auto-
> increments, or must this be taken care of by a trigger?
>
> Please suggest how I should accomplish this. I have EZSQL, but am a
> beginner in using it.
>
> Thanks very much!
> Bill Hutchison

Thankfully, Oracle does not supply autoincrementing fields. In Oracle you create an object called a SEQUENCE and then use it in an insert statement. Adding the column is just generic ... alter table ... and adding a column.

To use a sequence in your insert statement you can use the following syntax:

INSERT INTO xyz
(pk_field)
VALUES
(mysequence.nextval);

where "mysequence" is the name of the sequence created.

Daniel A. Morgan Received on Wed Jun 06 2001 - 01:13:05 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US