| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.tools -> Re: Altering a table by adding a primary key column
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
![]() |
![]() |