250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- MySqlDB
- JS
- shuffle()
- zipfile
- 파이썬
- locals()
- discard()
- randrange()
- decode()
- shutil
- __sub__
- items()
- node.js
- 오버라이딩
- View
- __len__
- fnmatch
- remove()
- __getitem__
- __annotations__
- MySQL
- fileinput
- count()
- HTML
- Database
- choice()
- glob
- inplace()
- mro()
- CSS
Archives
- Today
- Total
흰둥이는 코드를 짤 때 짖어 (왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!)
(MySQL) 기초 문법(auto_increment) 본문
728x90
반응형
auto_increment (필드의 identity한 숫자를 자동으로 부여)
create table tel(
idx int auto_increment,
name varchar(20) not null,
hp varchar(20) not null,
job varchar(20),
regdate datetime default now()
);
# Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key
auto_increment속성을 갖는 칼럼은 primary key를 반드시 같이 선언 해주어야 한다.
create table tel(
idx int auto_increment primary key,
name varchar(20) not null,
hp varchar(20) not null,
job varchar(20),
regdate datetime default now()
);
insert into tel (name, hp, job) values ('김사과', '010-1111-1111', '학생');

idx 값을 넣지 않아도 자동으로 숫자를 부여한다.
insert into tel (idx, name, hp, job) values (2,'반하나', '010-2222-2222', '학생');
insert into tel (idx, name, hp, job) values (10,'오렌지', '010-3333-3333', '군인');
insert into tel (idx, name, hp, job) values (2,'이메론', '010-4444-4444', '공무원'); # Error Code: 1062. Duplicate entry '2' for key 'tel.PRIMARY'

idx 값을 직접 입력하는 것도 가능하지만 primary key 특성상 중복값은 넣을 수 없다.
insert into tel (name, hp, job) values ('이메론', '010-4444-4444', '공무원');

idx를 직접 입력한 이후 auto_increment 값은 가장 높은 숫자의 다음 숫자로 적용된다.
728x90
반응형
'MySQL' 카테고리의 다른 글
(MySQL) 기초 문법(서브 쿼리) (0) | 2023.03.20 |
---|---|
(MySQL) 기초 문법(유니온) (1) | 2023.03.20 |
(MySQL) 정규화 (0) | 2023.03.20 |
(MySQL) 기초 문법(외래키, 조인) (1) | 2023.03.20 |
(MySQL) 기초 문법(Read, 연산자) (0) | 2023.03.16 |