Populating a Primary Key Using a Sequence : Primary Key « Sequences « Oracle PL/SQL Tutorial






SQL>
SQL>
SQL> CREATE TABLE myTable (
  2    id INTEGER CONSTRAINT pk PRIMARY KEY,
  3    status VARCHAR2(10)
  4  );

Table created.

SQL> CREATE SEQUENCE my_seq NOCACHE;

Sequence created.

SQL>
SQL> INSERT INTO myTable (id, status) VALUES (my_seq.nextval, 'PLACED');

1 row created.

SQL> INSERT INTO myTable (id, status) VALUES (my_seq.nextval, 'PENDING');

1 row created.

SQL>
SQL> select * from myTable;

        ID STATUS
---------- ----------
         1 PLACED
         2 PENDING

SQL>
SQL> drop table myTable;

Table dropped.

SQL> drop sequence my_seq;

Sequence dropped.








5.5.Primary Key
5.5.1.Populating a Primary Key Using a Sequence