예를 들어, 우주비행사라는 단어를 찾는다고 했을때, 104p까지 찾아야 한다.
하지만, index를 활용하면 찾는 횟수를 줄일 수 있다.
DROP DATABASE IF EXISTS sqldb; -- 만약 sqldb가 존재하면 우선 삭제한다.
CREATE DATABASE sqlDB;
CREATE TABLE userTbl -- 회원 테이블
( userID CHAR(8) NOT NULL PRIMARY KEY, -- 사용자 아이디(PK)
name VARCHAR(10) NOT NULL, -- 이름
birthYear INT NOT NULL, -- 출생년도
addr CHAR(2) NOT NULL,
mobile1 CHAR(3), -- 휴대폰의 국번 (011, 016, 017 ,...)
mobile2 CHAR(8), -- 휴대폰의 나머지 전화번호(하이폰제외)
height SMALLINT, -- 키
mDate DATE -- 회원 가입일
);
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('1111','일일일',1991,'광명','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('KBS','케베스','1878','소하','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('MCM','엠씨엠',2019,'일직','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('SBS','에세베',1098,'일산',NULL,NULL,171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('JTBC','제티비시',2022,'파주','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('TVCHU','티비조선',2020,'춘천','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('MBN','엠비엔',1994,'원주','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('tvn','티비앤',1912,'바다','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('spotv','스포티비',1933,'강릉',NULL,NULL,171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('lulu','룰루',1891,'강북','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('lala','랄라',1911,'강남','010','11111111',171,'2012-01-01');
INSERT INTO usertbl (userID, name, birthYear, addr, mobile1, mobile2, height, mDate) VALUES ('hlelo','안녕',1994,'일직','010','11111111',171,'2012-01-01');
값은 어떤 순서대로 들어갈까?
대,소문자를 무시하고 알파벳 순서로 데이터가 들어간 것을 볼 수 있다.
왜그럴까?
그 이유는 userTbl
의 userID
가 PK
로 지정이 되었고, 클러스터형 인덱스로 저장이 되었기 때문이다.
따라서 영어사전순으로 정렬이 된다.
CREATE DATABASE IF NOT EXISTS testDB;
USE testDB;
DROP TABLE IF EXISTS userTbl;
CREATE TABLE userTbl
(userID char(8) NOT NULL PRIMARY KEY,
name varchar(10) NOT NULL,
birthYear int NOT NULL,
addr nchar(4) NOT NULL
);
INSERT INTO usertbl(userID, name, birthYear, addr) VALUES('herry01', '해리케인', 1990, '영국');
INSERT INTO usertbl(userID, name, birthYear, addr) VALUES('우리흥', '손흥민', 1993, '한국');
INSERT INTO usertbl(userID, name, birthYear, addr) VALUES('우라우라모', '모우라', 1988, '브라질');
INSERT INTO usertbl(userID, name, birthYear, addr) VALUES('철벽수비', '데이비스', 1980, '웨일스');
INSERT INTO usertbl(userID, name, birthYear, addr) VALUES('뚝배기', '핸더슨', 1910, '독일');
위와 같이 userID
를 PK로 지정했기 때문에 userID는 클러스터형 인덱스가 되어서 userID
를 기준으로 사전식으로 정렬이 된다.
ALTER TABLE usertbl DROP PRIMARY KEY;
ALTER TABLE usertbl
ADD CONSTRAINT pk_name PRIMARY KEY(name);
SELECT * FROM userTbl;
참고 자료