# 1. 시퀸스 정보를 저장할 테이블 생성
CREATE TABLE MYSQL_SEQUENCE(
SEQUENCE_NAME VARCHAR(20) PRIMARY KEY,
SEQUENCE_NO BIGINT NOT NULL DEFAULT 0
);
# 2. 시퀸스 정보 생성
INSERT INTO MYSQL_SEQUENCE (SEQUENCE_NAME) VALUES ('sequence1');
# 3. Function 생성
DROP FUNCTION IF EXISTS NEXTVAL; # NEXTVAL 함수가 존재하는 경우 삭제 처리
DELIMITER $$
CREATE FUNCTION NEXTVAL(
p_sequence_name VARCHAR(20)
) RETURNS BIGINT
BEGIN
DECLARE v_sequence_no BIGINT;
# MYSQL_SEQUENCE 테이블의 SEQUENCE_NO + 1
UPDATE MYSQL_SEQUENCE SET SEQUENCE_NO = SEQUENCE_NO + 1 WHERE SEQUENCE_NAME = p_sequence_name;
# 증가된 SEQUENCE_NO를 저장
SELECT SEQUENCE_NO INTO v_sequence_no FROM MYSQL_SEQUENCE WHERE SEQUENCE_NAME = p_sequence_name;
# 반환
RETURN v_sequence_no;
END;
# 4. 사용법
INSERT INTO table_name(column_name, ... ) VALUES (NEXTVAL('sequence1'), ...);