1. Primary Key 지정
- table이름 = user
- primary key로 지정할 속성 = userID
alter table user add constraint pk_name primary key(userID);
select * from user;
show index from user;
- userID를 기준으로 정렬된 것을 볼 수 있다.
- 1개만 생성 가능 (보조인덱스, 클러스터링 인덱스)
- 보조 인덱스보다 검색 속도는 빠르지만, 데이터 입력/수정/삭제는 더 느리다
2. 단순 보조 인덱스 생성
- 단순 보조 인덱스로 지정할 속성 이름 = addr
- 인덱스 이름 = idx_user_addr
create index idx_user_addr on user(addr);
- 단순(중복 허용, non-unique) <=> 고유(unique) : Non_unique = 1로 되어 있음
3. 고유 보조 인덱스 생성
- 고유 보조 인덱스로 지정할 속성 이름 = name
- 인덱스 이름 = idx_user_name
create unique index idx_user_name on user(name);
- Non_unique = 0으로 되어 있음
- 중복된 값 허용X => 중복된 값이 있을 때는 생성할 수 없기 때문에 Error 발생
4. 여러 속성 조합해서 인덱스 생성
- 속성 이름 = name, birthYear
- 인덱스 이름 = idx_user_name_birthYear
create index idx_user_name_birthYear on user(name, birthYear);
- 인덱스 상태를 보면 Seq_in_index 열이 1과 2로 설정되어 있는 것을 확인할 수 있다.
결과 화면
5. 인덱스 삭제하기
drop index idx_user_name on user;
drop index idx_user_addr on user;
drop index idx_user_name_birthYear on user;
alter table user drop primary key;
- 보조 인덱스를 먼저 삭제하는 것이 좋다.
- 보조 인덱스는 drop index, alter table 둘 다 사용 가능
- Primary Key는 alter table로만 삭제 가능
'CS > 데이타베이스' 카테고리의 다른 글
[실습] Stored Procedure (저장 프로시저) (0) | 2021.11.11 |
---|---|
[실습] 고급 SQL SELECT문 (0) | 2021.11.01 |
[실습] 키 생성 (0) | 2021.10.20 |
[실습] MySQL 시작하기 (0) | 2021.10.18 |
MySQL 문법 (0) | 2021.09.17 |