-
์์ 1
-
ํ ์ด๋ธ
- ํ์(id, name)
- ๊ณผ๋ชฉ(id, name)
- ์๊ฐ๋ด์ญ(id, ํ์, ๊ณผ๋ชฉ)
-
๊ด๊ณ
- ๋ค๋๋ค ๊ด๊ณ (ํ์ ์ฌ๋ฌ๋ช ์ด ๊ณผ๋ชฉ ์ฌ๋ฌ๊ฐ ์๊ฐํ ์ ์์)
- ํ์ ํ ๋ช ์ด ์ฌ๋ฌ ๊ณผ๋ชฉ ๋ค์ ์๋ ์๊ณ ๊ณผ๋ชฉ ํ๋๋ ์ฌ๋ฌ ํ์์ ๋ฐ์ ์ ์์
- ํ์ - ์๊ฐ๋ด์ญ (1:N)
- ๊ณผ๋ชฉ - ์๊ฐ๋ด์ญ (1:N)
- ํ์ - ๊ณผ๋ชฉ (N:N)
-
-
์์ 2
-
ํ ์ด๋ธ
- ํ์
- ๊ต์
- ์ฃผ์๊ต์
-
๊ด๊ณ
- ํ์ 1๋ช
๋น ์ฃผ์๊ต์๋ 1๋ช
์ฃผ์๊ต์ 1๋ช ๋น ํ์ ์ฌ๋ฌ๋ช - ํ์ - ์ฃผ์๊ต์ (N:1)
- ๊ต์ - ์ฃผ์๊ต์ (1:1)
- ํ์ - ๊ต์ (1:N)
- ํ์ 1๋ช
๋น ์ฃผ์๊ต์๋ 1๋ช
-
-
์์ 3
-
ํ ์ด๋ธ
- ํ์(id)
- ๋์๋ฆฌ(ํ์์งฑ:id)
- ๋์๋ฆฌํ์
-
๊ด๊ณ
- ํ์ - ๋์๋ฆฌ (1:N)
- ๋์๋ฆฌ - ๋์๋ฆฌํ์(1:N)
- ํ์ - ๋์๋ฆฌ ํ์(1:N)
-
-
์์ 4
-
ํ ์ด๋ธ
- ๋ถ์(์ธ์ฌ)
- ์ง์
-
๊ด๊ณ
- ๋ถ์ - ์ง์ (1:N)
(๋ถ์ ํ๋์๋ ์ง์ ์ฌ๋ฌ๋ช ๊ฐ๋ฅ) - ๋ง์ฝ, ์ง์์ด ๋ ๊ฐ ๋ถ์ ์ด์ ๊ฐ๋ฅํ๋ค๋ฉด N:N ๊ด๊ณ๊ฐ ๋จ
(์ง์ 1๋ช ์ด ์ฌ๋ฌ ๋ถ์ ๊ฐ๋ฅ && ๋ถ์ 1๊ฐ๋ ์ฌ๋ฌ ์ง์ ๊ฐ๋ฅ) - ๋ง์ฝ, ์ง์ 1๋ช
์ด ๋ถ์ 1๊ฐ๋ง ๊ฐ๋ฅ, ๋ถ์ 1๊ฐ๋ ์ง์ ์ฌ๋ฌ ๋ช
๊ฐ๋ฅ
=> 1:1, 1:N ๊ฐ๊ฐ ๊ณฑํด์ =>1*1 : 1*n
=> 1 : N (๋ถ์ - ์ง์)
- ๋ถ์ - ์ง์ (1:N)
-
-
์์ 5
-
ํ ์ด๋ธ
- ํ์(id, name, gender)
- ๊ธฐํ์ ๋ณด(id, ํ์กํ, ์ทจ๋ฏธ, ํน๊ธฐ)
- ์ ๊ณผ๊ธฐ๋ก(id, ์ ๊ณผ์ฐ๋)
-
๊ด๊ณ
- ํ์ - ๊ธฐํ์ ๋ณด (1:1)
- ํ์ - ์ ๊ณผ๊ธฐ๋ก (1:N)
(ํ์ 1๋ช ์ด ์ฌ๋ฌ ์ ๊ณผ ๊ฐ๋ฅํ๋ค๋ฉด) - ์์ฃผ ์ฐ์ง ์๋ ์ ๋ณด๋ ํ ์ด๋ธ๋ก ๋ถ๋ฆฌ (์ ๊ทํ) => SELECT ํธํจ
- ๋ฉ์ธ ํ ์ด๋ธ์ ํ์ ํ ์ด๋ธ (์ฐธ์กฐ ๋นํ๋/๋๋ ํ ์ด๋ธ)
- ๊ธฐํ์ ๋ณด depends on ํ์ (๊ธฐํ์ ๋ณด๋ ํ์์ ์์กด์ )
=> ๊ธฐํ์ ๋ณด์๋ ํ์์ id๊ฐ ์์ด์ผ ํจ - ์ ๊ณผ๊ธฐ๋ก depends on ํ์ => ์ ๊ณผ๊ธฐ๋ก ํ
์ด๋ธ์๋ ํ์ id ์์ด์ผ ํจ
์ ๊ณผ๊ธฐ๋ก ํ ์ด๋ธ์ ํ์ id (FK)๊ฐ ํ์ ํ ์ด๋ธ์ ํ์ id (PK) ์ฐธ์กฐ
-
-
์ค์ต
-
ํ ์ด๋ธ
- Club(๋์๋ผid, ๋์๋ฆฌname, createdate, leader)
- Student(ํ์id)
- Enroll(id int, subject:fk_subject, student:fk_student)
๋์ค์ ORM ๋๋ฌธ์ id ํ๋๋ฅผ PK๋ก ๋บ, FK๋ ๋ ๋ค CASCADE ๊ฐ๋ฅ - Subject(๊ณผ๋ชฉid, ๊ณผ๋ชฉname, professor:fk_prof set null)
- Professor(๊ต์id smallint unsigned, ๊ต์name varchar(31), likecnt int default 0)
-
๊ด๊ณ
- Club์ leader - Student์ id (1:1, ๋ฆฌ๋ 1๋ช ๋น ํ์ 1๋ช )
- Enroll์ student - Student์ id (N:N, ํ์ 1๋ช ์ด ์ฌ๋ฌ ๊ณผ๋ชฉ ์๊ฐ ๊ฐ๋ฅ, ํ ๊ณผ๋ชฉ์ ์ฌ๋ฌ ํ์ ์๊ฐ ๊ฐ๋ฅ)
- Enroll์ subject - Subject์ id (1:1)
- Subject์ prof - Professor์ ๊ต์id (1:1๋ก ๊ฐ์ฃผ, ํ ๊ณผ๋ชฉ์ ํ ๊ต์๋ง ๋ด๋น, ๋ด๋น๊ต์)
- ์๋ก ๋ค๋ฅธ ํ
์ด๋ธ์ name column ๋ผ๋ฆฌ ๋งคํํ๋ฉด ์ ๋ชจ๋ฆ (๋๋ช
์ด์ธ ๊ฐ๋ฅ)
=> id๋ฅผ ๋งคํํ์!
-
-- Club table ์์ฑ
-- leader๋ ํ์ ํ
์ด๋ธ์ id๋ฅผ ์ฐธ์กฐํ๋ฏ๋ก type ์ผ์น ํ์
create table Club(
id smallint unsigned not null auto_increment primary key,
name varchar(31) not null,
createdate timestamp not null default current_timestamp,
leader int,
[Constraint] foreign key fk_leader_student(leader) references Student(id)
[on delete]
[on update]
);
desc Club;
show create table Club;
-- Professor table ์์ฑ
create table Professor(
id smallint unsigned not null auto_increment primary key,
name varchar(31) not null,
likecnt int not null default 0
)
-- Subject table ์์ฑ
create table `Subject`(
id smallint unsigned not null auto_increment primary key,
name varchar(31) not null,
professor smallint unsigned,
constraint foreign key fk_professor_professor (professor) references Professor(id)
on delete set null
);
-- Enroll table ์์ฑ
create table Enroll(
id int unsigned not null auto_increment primary key,
subject smallint unsigned not null,
student int not null
);
show create table Enroll;
alter table Enroll add constraint foreign key fk_subject (subject) references Subject(id) on delete cascade;
alter table Enroll add constraint foreign key fk_student (student) references Student(id) on delete cascade;
insert into Club(name, leader) values('์ํธ๋ถ', 100);
insert into Club(name, leader) values('์์
๋ถ', 200);
insert into Club(name, leader) values('๋ฏธ์ ๋ถ', 300);
-- leader ์ด๋ฆ๊น์ง ๊ฐ์ด ๋ณด๊ณ ์ถ์
select c.*, s.name as 'student name' from Club c inner join Student s on c.leader = s.id;
-- MySQL์ dual ์์จ๋ ๋จ (์๋๋ ์ฐ๋ ๊ฒ ์ ์)
select ceil(rand() * 10) from dual;
-- Student ํ
์ด๋ธ๋ถํฐ ๋ถํน์ ์์ 100๋ช
๊ฐ์ ธ์ด
-- order by rand()๋ PK(id)์ ๋ํ ๋์
-- name์ PK๋ฅผ ๋๋คํ๊ฒ ๋ฐ์ ๋ฐ์ดํฐ์์ ๋ฝ์๋ธ ์ปฌ๋ผ์ผ ๋ฟ
insert into Professor(name, likecnt) select name, ceil(rand() * 10) from Student order by rand() limit 100;
select * from Professor;
insert into Subject(name, professor)
select '๊ตญ์ด', id from Professor order by rand() limit 10;
select * from Subject;
update Subject set name = '์ญ์ฌ' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '์ฌํ' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '์ค๋ฆฌ' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '๊ณต์
' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '์์ด' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '์๋ฌผ' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = 'ํํ' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '์ปดํจํฐ' where name = '๊ตญ์ด' and id <> 10 limit 1;
update Subject set name = '๋ฌธ์' where name = '๊ตญ์ด' and id <> 10 limit 1;
-- tool index setting
-- uq_subject_name UNIQUE name
- id๋ ๋ณดํต PK, auto_increment
- DB๋ ERD (Entity Relationship Diagram)
- ์์๋ ํ ์ด๋ธ ๋ช
- ์๋๋ ์ปฌ๋ผ ๋ช
- ํด๋์ค๋ ํด๋์ค ๋ค์ด์ด๊ทธ๋จ
- tinyint(1byte, 256)
- smallint(2B, 65536)
- int(4B)
- ํ์์๊ฐ ํ
์ด๋ธ์์ PK๋ฅผ ๋ณตํฉํค๋ก ์ก์ง ์๊ณ ๋ฐ๋ก id๋ก ๋๋ ์ด์
- ORM ๊ตฌ์ฑ ์ ์ฐธ์กฐ(hasMany, belongsTo) ์ฉ์
- PK ์ธ๋ฑ์ค ํฌ๊ธฐ๊ฐ ์์ ๋์คํฌ ๋ฐ ๋ฉ๋ชจ๋ฆฌ์ ํจ์จ์
- ๋ ๊ฐ ์ด์์ column์ PK(Unique Key)๋ฅผ ์ฌ์ฉํ๋ฉด
๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ ์ด ๋ด๋ถ์์ ํ ์ผ์ด ๋ง์์ง - where ์กฐ๊ฑด์ ์ด๋ join ์ ์ด ๋ณต์กํ๊ณ ๋๋ ค์ง
- ๋ณตํฉํค๋ผ ํจ์ ์๋ฏธ์๋ ๋ฐ์ดํฐ ์ปฌ๋ผ๋ค์ด PK๋ก ์ฌ์ฉ๋๊ฒ ๋๋๋ฐ,
๋ง์ฝ ์์ ์ด ๋ฐ์ํ๋ค๋ฉด PK์ ๊ธฐ๋ณธ ์๊ฑด
(์์ ์ ์ด๊ณ ๊ฐ๋จํ๋ฉฐ ๊ฐฑ์ ํ ํ์๊ฐ ์๋ ์ปฌ๋ผ์ผ๋ก ๊ตฌ์ฑ)์ ์๋ฐฐ๋จ
- FK๋ create table ํ ๋ ๊ฑธ์ด๋ ๋๊ณ , alter table ํ ๋ ํด๋ ๋จ
- FK๋ constraint์ธ๋ฐ ์์์๋ index์
index๊ฐ ๋์ผ table join ํด์ ๋น ๋ฅด๊ฒ ๊ฒ์ ๊ฐ๋ฅ
(์์ชฝ ๋ชจ๋ index ํค๋ก ์ธํ ๋์ด์ผ ๋น ๋ฅด๊ฒ ๊ฒ์ ๊ฐ๋ฅ, FK-PK) - index๋ sorting ๋์ด ์๋ ๋ฉ๋ชจ๋ฆฌ์ ์ด๋ค ์์ธ
- FK
- RESTRICT: ๋๊ฐ ์ฐธ์กฐํ๊ณ ์์ผ๋ฉด PK ์ญ์ ๋ถ๊ฐ (default)
- SET NULL: PK ์ญ์ ์ ์ FK ๋ถ๋ถ null๋ก ์ธํ ํ PK ์ญ์
- CASCADE: PK ์ญ์ ํ๋ฉด FK ํด๋น ๋ถ๋ถ๋ ๋ชจ๋ ์ญ์ (์กฐ์ฌ)
(ex. ์๊ฐ ํ ์ด๋ธ์์ ๊ณผ๋ชฉ์ด ์์ด์ง๋ฉด ์๊ฐ ๋ด์ญ๋ ๋ชจ๋ ์ญ์ ํด์ผํจ)
- Column
- not null default 0
(null ์ง์ ํด์ insert๋ ์๋ฌ, column ๋ค ์ง์ ์ํ๋ฉด ์๋์ผ๋ก 0 ๋ค์ด๊ฐ) - data ์ฉ๋๋ฉด์์๋ ์์ข์ ์ ์์ง๋ง ์ฑ๋ฅ ๋ฉด์์๋ ๋ฌด์กฐ๊ฑด not null์ด ์ข์
- not null default 0
- MySQL index; ๋์คํฌ์ data ์
- ํ record size๊ฐ 16KB ์์ชฝ์ด ์ข์
- index page; ํ page๋ฅผ 16KB๋ก ์ ํด๋์
- record ์์น ์๊ธฐ ์ํด id(PK) ์ฃผ์ ๊ฐ์ ๋ํ ์์ธ์ ๋ณ๋ ๊ณต๊ฐ์ ๋ช ์ธํจ (index page)
- index page๋ ๋ชจ๋ 16KB๋ก ๋๋ (์๋์ฅ ํ๋ 16KB)
- ์ธ๋ฑ์ค ํ์ด์ง ์ค ํ๋ 16KB => 16 x 1024 B
(int 4byte, varchar(2) UTF8 => 6byte)
index๋ฅผ int์ varchar ๋ชจ๋ ์ก์ => 10 byte (์ค์ ๋ฐ์ดํฐ ๊ฐ)
๋์ ํ์ด์ง๊ฐ ์ฃผ์์ - ์ฃผ์๊ฐ์ 16B๋ก ์ ํด์ ธ ์์ + 10B => 26B (๋ ์ฝ๋ ํ๋)
- ์ธ๋ฑ์ค ํ์ด์ง ํ๋์ 16KB / 26B => 16 * 1024B / 26B => ์ฝ 630๊ฐ ๋ ์ฝ๋ ์ํ ๊ฐ๋ฅ
- ๋ ์ฝ๋ 630๊ฐ ๋์ด๊ฐ๋ฉด ์ธ๋ฑ์ค ํ์ด์ง 2์ฅ ํ์
- ์ธ๋ฑ์ค ํ์ด์ง ์ ๊ฒ ๊ฐ์ ธ๊ฐ๋ ค๋ฉด ์ธ๋ฑ์ค๋ฅผ ์๊ฒ ํด์ผ ํจ
๋ณตํฉ ์ปฌ๋ผ์ PK๋ก ์ก์ผ๋ฉด (๋ณตํฉํค) ์ธ๋ฑ์ค ํ์ด์ง ๋ ๋ง์ด ํ์ํด์ ์ฑ๋ฅ ๋จ์ด์ง
์ธ๋ฑ์ค๊ฐ ๋ง์๋ ์์ข๊ณ ์ฌ์ด์ฆ ์ปค๋ ์์ข์
- ์ค์ ๋ก ํ๊ธ ํ๊ธ์๊ฐ 3byte๊ฐ ์๋ ์ ์์ (DB์์ 1/2/3 byte ์ง์ ๊ฐ๋ฅ)
- ์ธ๋ฑ์ค๋ง๋ค ๋ณ๋์ ์ธ๋ฑ์ค ํ์ด์ง ์์ฑ๋จ
- index page ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ ์ํด PK๋ฅผ ๋ณตํฉํค๋ก ์ง์ ํ์ง ์์
- comment ๋ค ๋ค๋๊ฒ ์ข์
- MySQL์ ์๋์ผ๋ก FK๋ฅผ index๋ก ๋ง๋ฆ (table join์ ์ฑ๋ฅ ํฅ์ ์ํจ)