개발/DB(MariaDB)

12. DB - (auto_increment)

oneidsin 2025. 3. 18. 11:27

AUTO_INCREMENT

auto_increment 는 자동 증가하는 속성이다.

중복되지 않는 컬럼에 사용되며 키 설정이 되어있어야 사용 가능하기에 PK에 자주 사용된다.

 

-- 생성법 1 : 테이블 생성시

-- 생성법 1 : 테이블 생성시
create table auto_inc(
	no int(10) primary key auto_increment
	, name varchar(50) not null
);

select * from auto_inc;
insert into auto_inc (name) values ('kim');
insert into auto_inc (name) values ('lee');
insert into auto_inc (name) values ('park');

 

-- 생성법 2 : 이미 생성된 테이블에 추가

create table test(
	no int(10)
	, name varchar(10) not null
);

alter table test modify no int(10) primary key auto_increment;

insert into test (name) values ('b');
insert into test (name) values ('c');

select * from test;

 

-- 자동속성 값 초기화

alter table test auto_increment = 1000;
insert into test (name) values ('b');
insert into test (name) values ('c');

'개발 > DB(MariaDB)' 카테고리의 다른 글

13. DB - (LIMIT)  (0) 2025.03.18
11. DB - (VIEW)  (0) 2025.03.17
10. DB - (INDEX)  (0) 2025.03.17
9. DB - (EXISTS, ANY, ALL)  (0) 2025.03.17
8. DB - (JOIN)  (0) 2025.03.17