Tuesday, July 7, 2009

Create Sequence Numbers for Oracle

Full Create Sequence Syntax
CREATE SEQUENCE <sequence_name> INCREMENT BY <integer> START WITH <integer> MAXVALUE <integer> / NOMAXVALUEMINVALUE <integer> / NOMINVALUECYCLE / NOCYCLECACHE <#> / NOCACHEORDER / NOORDER;

Create Sequence Simplest Form
CREATE SEQUENCE seq_campus_site_id;
SELECT seq_campus_site_id.NEXTVAL FROM dual;

Simple Autonumber With Sequence
INSERT INTO <table_name>(<column_name>)VALUES(<sequence_name>.NEXTVAL);
INSERT INTO campus_site(site_id, organization_name, campus_name)VALUES(seq_campus_site_id.NEXTVAL, 'Univ. of Washington', 'Main Seattle');
SELECT * FROM campus_site;
INSERT INTO campus_site(site_id, organization_name, campus_name)VALUES(seq_campus_site_id.NEXTVAL, 'Univ. of Washington', 'Bothell');
SELECT * FROM campus_site;

Simple Autonumber With Sequence Into Two Tables
INSERT INTO <table_name>(<column_name>)VALUES(<sequence_name>.CURRVAL);
CREATE SEQUENCE seq_division_id;
INSERT INTO campus_site(site_id, organization_name, campus_name)VALUES(seq_campus_site_id.NEXTVAL, 'Univ. of Washington', 'Tacoma');
INSERT INTO division (division_id, site_id, division_name) VALUES(seq_division_id.NEXTVAL, seq_campus_site_id.CURRVAL, 'Engineering');
SELECT * FROM campus_site;
SELECT * FROM division;