2024. 1. 11. 19:30ㆍ· LANGUAGE/└ Database
환경 : DBeaver
시퀀스(SEQUENCE)는 순서를 자동 입력할 때 사용할 수 있다 대부분 기본 키가 적용된 컬럼에 시퀀스도 적용하여 사용하곤 한다. 또, 데이터베이스는 객체 중 하나이다.
(참고) (현재 글에서 진행되는 테이블 생성 구문)
-- Create a new table for customer information
CREATE TABLE customer(
customer_id NUMBER PRIMARY KEY,
customer_name VARCHAR2(100) NOT NULL,
customer_email VARCHAR2(100) NOT NULL
);
시퀀스 테이블 생성 [ CREATE SEQUENCE]
먼저 시퀀스도 테이블 생성과 동일하게 생성해줄 수 있다.
-- Create a new sequence
CREATE SEQUENCE customer_seq;
(+) CREATE SEQUENCE 시퀀스명;
(추가 1) 시퀀스를 생성할 때 조건을 줄 수 있다.
CREATE SEQUENCE customer_seq START WITH 1 INCREMENT BY 1;
(+) 시작 값은 1이고, 1씩 증가한다.
START WITH 1 INCREMENT BY 1;
(추가 2) nocache
create sequence product_seq nocache;
(+) 'NOCACHE' 옵션을 사용하면, 시퀀스 번호를 미리 할당하고 메모리에 저장하지 않도록 지정하는 데 사용된다.
따로 글 업로드 예정
시퀀스 적용 [ 시퀀스명.nextval ]
-- Insert data into the customer table using the sequence
INSERT INTO customer(customer_id, customer_name, customer_email)
VALUES(customer_seq.nextval, 'John Doe', 'john.doe@example.com');
INSERT INTO customer(customer_id, customer_name, customer_email)
VALUES(customer_seq.nextval, 'Jane Smith', 'jane.smith@example.com');
-- Display the contents of the customer table
SELECT * FROM customer;
(+) 같은 데이터를 여러번 삽입 시
테이블 확인을 해보면 같은 데이터가 들어가도, 번호가 다르게 들어가는 것을 확인 할 수 있다.
( → customer_name, customer_emial 컬럼에 중복 허용이기 때문에 가능)
시퀀스 삭제 [ DROP SEQUENCE ]
삭제 구문도 테이블 삭제와 비슷하다.
-- Drop the sequence
DROP SEQUENCE customer_seq;
시퀀스 자세한 정보 확인 [ user_sequence ]
-- Display detailed information about the sequence
SELECT * FROM user_sequence;
(+) 위와 같이 포괄적으로 확일할 수 있다.
-- Display detailed information about the sequence
SELECT * FROM user_sequences WHERE sequence_name = 'CUSTOMER_SEQ';
(+) 조건을 주어 해당하는 시퀀스의 정보만 알아낼 수 있다.
실행 결과는 다음과 같다.
개인 공부 기록용입니다:)
'· LANGUAGE > └ Database' 카테고리의 다른 글
[ DB / Oracle ] 날짜 데이터 (date / timestamp) (0) | 2024.01.13 |
---|---|
[ DB / Oracle ] 시퀀스 옵션 'NOCACHE' (0) | 2024.01.12 |
[ DB / Oracle ] 기본키와 기본값 설정 (Primary key / default) (0) | 2024.01.10 |
[ DB / Oracle ] 제약 조건 사용, 나머지 연산(MOD()) (0) | 2024.01.09 |
[ DB / Oracle ] 테이블 생성 시 제약 조건 사용해보기 + 정규표현식 (2) | 2024.01.09 |