Create Sequence – Generador de indentificadores unicos que no bloquea transacciones
Crea un objeto capaz de darnos numeros consecutivos unicos.
CREATE SEQUENCE secuencia
INCREMENT BY n
START WITH n
{MAX VALUE n | NOMAXVALUE}
{MIN VALUE N | NOMINVALUE}
{CYCLE | NOCYCLE}
{CACHE N | NOCACHE}
{ORDER | NOORDER};
En realida es un generador de indentificadores unicos que no bloquea transacciones.
Es muy util para generar primary keys.
DROP TABLE students CASCADE CONSTRAINTS;
CREATE TABLE students (
id NUMBER(5) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
major VARCHAR2(30),
current_credits NUMBER(3)
);
CREATE SEQUENCE student_sequence
START WITH 10000
INCREMENT BY 1;
INSERT INTO students (id, first_name, last_name, major,
current_credits)
VALUES (student_sequence.NEXTVAL, ‘Scott’, ‘Smith’,
‘Computer Science’, 11);
INSERT INTO students (id, first_name, last_name, major,
current_credits)
VALUES (student_sequence.NEXTVAL, ‘Margaret’, ‘Mason’,
‘History’, 4);
SELECT * FROM students;
Asi obtenemos el siguiente valor:
SELECT student_sequence.NEXTVAL FROM DUAL;
Tambien podemos obtener el valor actual:
SELECT student_sequence.CURRVAL FROM DUAL;