Populating a Primary Key Using a Sequence

Sequences are useful for populating integer primary key column values.


SQL> CREATE TABLE emp (
  2  id            INTEGER CONSTRAINT my_pk1 PRIMARY KEY
  3  );

Table created.

SQL>
SQL> CREATE SEQUENCE my_sql NOCACHE;

Sequence created.

SQL>
SQL> INSERT INTO emp (id) VALUES (my_sql.nextval);

1 row created.

SQL> INSERT INTO emp (id) VALUES (my_sql.nextval);

1 row created.

SQL>
SQL> SELECT *
  2  FROM emp;

        ID
----------
         1
         2

SQL>
Home »
Oracle »
Table » 

Sequence:
  1. Creating a Sequence
  2. Retrieving Information on Sequences
  3. Using a Sequence
  4. Populating a Primary Key Using a Sequence
  5. Modifying a Sequence
  6. Using Sequences in PL/SQL
  7. Dropping a Sequence
Related: