Oracle에서는 auto increment(자동 증가) 를 사용할 수 없다.
i. e. , 사용자가 추가되면, 자동으로 emp_id 번호 증가
그래서 sequence 사용
시퀀스 생성
CREATE SEQUENCE 시퀀스_명 -- 시퀀스_명 설정
INCREMENT BY 값 -- 증감_값
START WITH 값 -- 시작_값 (증감값이 양수면 MINVALUE부터, 음수면 MAXVALUE부터 시작)
NOMINVALUE OR MINVALUE 값 -- NOMINVALUE :최소값 없음 MINVALUE : 최소값 설정
NOMAXVALUE OR MAXVALUE 값 -- NOMAXVALUE : 최대값 없음 MAXVALUE : 최대값 설정
NOCYCLE OR CYCLE -- CYCLE : 최대값 도달 시, 최소값부터 다시 시작 NOCYCLE : 최대값 도달 시, 시퀀스 생성 중단
NOCACHE OR CACHE 값 -- 시퀀스를 빨리 제공하기 위해 메모리에 캐쉬 지정, NOCACHE : 기본값 20
create sequence empp_sq
START WITH 1 // 시작 값
INCREMENT BY 1 // 증감 값
MAXVALUE 100 // 최대 값
CYCLE NOCACHE; // CYCLE 최대값 도달 시, 최소부터 다시 시작 NOCACHE 기본값 20
https://wakestand.tistory.com/204
시퀀스 연결
insert into empp values (empp_seq.NEXTVAL, 'emp_id');
// SEQUENCE 생성 시, 작성한 SEQUENCE_NAME = empp_seq
// create sequence empp_sq 해서 나온 empp_sq
// 하나를 넣으면 그다음 값이 나와라 == NEXTVAL
--------------------------------------------------------------------
create table samp1 (user_id number (20) not null);
// samp1 table 생성 constraint not null 조건
insert into samp1 (user_id) values(empp_sq.NEXTVAL);
// samp1 table에 user_id column에 empp_sq sequence 추가
select * from samp1;
create table tasks (
id number primary key,
title varchar2(20) not null);
// tasks table 생성
create sequence tasts_id_seq
increment by 3
start with 10
minvalue 10
maxvalue 100
cycle
// sequence 생성
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT MAN');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT SALES');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT PROG');
insert into tasks (id, title) values(tasks_id_seq.NEXTVAL, 'IT DEVELOP');
// tasks table에 sequence 로 내용 입력. NEXTVAL 다음 것으로 해라.
select id, title from tasks
ID TITLE
10 IT PROG
13 IT MAN
16 IT SALES
19 IT PROG
22 IT DEVELOP
경 과: 00:00:00.00
Execution Plan
----------------------------------------------------------
Plan hash value: 41374823
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 125 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL | TASKS | 5 | 125 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
create sequence dept_id_seq
start with 20
increment by 3;
// 시퀀스 생성
insert into tasks values(dept_id_seq.NEXTVAL, 'IT DEVELOP');
insert into tasks values(dept_id_seq.NEXTVAL, 'IT MAN');
insert into tasks values(dept_id_seq.NEXTVAL, 'IT PROG');
select increment_by from user_sequences
where sequence_name = 'DEPT_ID_SEQ'
// dept_id_seq 시퀀드 있는지 없는지 확인.
INCREMENT_BY
3
경 과: 00:00:00.00
Execution Plan
----------------------------------------------------------
select * from user_sequences
where sequence_name = 'DEPT_ID_SEQ'
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY CYC ORD CACHE_SIZE
------------------------------------------------------------------------------------------ ---------- ---------- ------------ --- --- ----------
LAST_NUMBER
-----------
DEPT_ID_SEQ 1 1.0000E+27 3 N N 20
80
경 과: 00:00:00.00
Execution Plan
----------------------------------------------------------
select dept_id_seq.CURRVAL from dual;
CURRVAL
----------
26
경 과: 00:00:00.00
Execution Plan
----------------------------------------------------------
Plan hash value: 3584566422
------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
| 1 | SEQUENCE | DEPT_ID_SEQ | | | |
| 2 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------
SUB QUERY (0) | 2022.06.08 |
---|---|
JOIN (Oracle JOIN, ANSI JOIN, INNER JOIN, OUTER JOIN, SELF JOIN) (0) | 2022.06.08 |
제약 조건 (CONSTRAINTS) (0) | 2022.06.07 |
테이블 분할, 복사 (연도 별 등 기준), 날짜 자동 입력 (0) | 2022.06.07 |
정규 표현식 Regular Expression (0) | 2022.06.07 |