SEQUENCE
is a data object that multiple users can access to automatically generate incremented values.
It prevents duplicate values from being created simultaneously because multiple users are effectively forced to “take turns” before each sequential item is generated.
For the purposes of creating a unique primary key for a new table,
CREATE
the table, course3, we’ll be using.
ALTER TABLE
.
SEQUENCE
that will be utilized later to actually generate the unique, auto incremented value.
INSERT
a row into the table and use NEXTVAL
function to generate a primary key.
SQL> DROP TABLE course3; Table dropped. SQL> CREATE TABLE course3 ( 2 cnumber NUMBER NOT NULL, 3 title VARCHAR2(64) NOT NULL ); Table created. SQL> ALTER TABLE course3 2 ADD ( CONSTRAINT course3_pk PRIMARY KEY ( cnumber ) ); Table altered. SQL> CREATE SEQUENCE course3_seq; Sequence created. SQL> INSERT INTO course3 VALUES ( course3_seq.NEXTVAL, 'Databases' ); 1 row created. SQL> INSERT INTO course3 VALUES ( course3_seq.NEXTVAL, 'Advanced Database Systems' ); 1 row created. SQL> INSERT INTO course3 VALUES ( course3_seq.NEXTVAL, 'Data Engineering and Mining' ); 1 row created. SQL> SELECT * FROM course3; CNUMBER TITLE ---------- ------------------------------------ 1 Databases 2 Advanced Database Systems 3 Data Engineering and Mining |
Result:
|
The Database includes:
|
The Database includes:
|
My doctor recommended to eat at BurgerKing more often. Well, he actually said I should not have McDonald’s anymore, but I know what he meant. |