시퀀스와 같은 역할을 하는 AUTO_INCREMENT
CREATE TABLE TEST (
ID INT auto_increment primary key,
name varchar(400)
);
그대로 사용해봅시다
간단 간단
-- 다른 방법 --
procedure 와 함수 function 을 사용한 예제
procedure 생성
create procedure `create_sequence` (IN the_name text)
modifies sql data
deterministic
begin
delete from SEQUENCES where name = the_name;
insert into SEQUENCES values(the_name, 0);
end;
함수 생성
create function `nextval` (the_name VARCHAR(32))
RETURNS BIGINT unsigned
MODIFIES SQL DATA
Deterministic
begin
declare ret BIGINT unsigned;
update SEQUENCES set currval = currval +1 where name = the_name;
select currval into ret from SEQUENCES where name = the_name limit 1;
return ret;
end;
사용 해보자
굿
근데 프로시저 왜씀 엌ㅋ
'데이터베이스 > MySQL' 카테고리의 다른 글
Mysql - LAST_INSERT_ID() 함수 (0) | 2024.03.05 |
---|---|
MySQL - 명령어 (0) | 2023.12.16 |