Skip to content

Latest commit

ย 

History

History
241 lines (192 loc) ยท 9.19 KB

File metadata and controls

241 lines (192 loc) ยท 9.19 KB

MySQL 05 - ๊ด€๊ณ„(Foreign Key)์™€ index์˜ ๊ฐœ๋…

Relation

  • ์˜ˆ์‹œ 1

    • ํ…Œ์ด๋ธ”

      • ํ•™์ƒ(id, name)
      • ๊ณผ๋ชฉ(id, name)
      • ์ˆ˜๊ฐ•๋‚ด์—ญ(id, ํ•™์ƒ, ๊ณผ๋ชฉ)
    • ๊ด€๊ณ„

      • ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ (ํ•™์ƒ ์—ฌ๋Ÿฌ๋ช…์ด ๊ณผ๋ชฉ ์—ฌ๋Ÿฌ๊ฐœ ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ์Œ)
      • ํ•™์ƒ ํ•œ ๋ช…์ด ์—ฌ๋Ÿฌ ๊ณผ๋ชฉ ๋“ค์„ ์ˆ˜๋„ ์žˆ๊ณ  ๊ณผ๋ชฉ ํ•˜๋‚˜๋Š” ์—ฌ๋Ÿฌ ํ•™์ƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
      • ํ•™์ƒ - ์ˆ˜๊ฐ•๋‚ด์—ญ (1:N)
      • ๊ณผ๋ชฉ - ์ˆ˜๊ฐ•๋‚ด์—ญ (1:N)
      • ํ•™์ƒ - ๊ณผ๋ชฉ (N:N)
  • ์˜ˆ์‹œ 2

    • ํ…Œ์ด๋ธ”

      • ํ•™์ƒ
      • ๊ต์ˆ˜
      • ์ฃผ์ž„๊ต์ˆ˜
    • ๊ด€๊ณ„

      • ํ•™์ƒ 1๋ช…๋‹น ์ฃผ์ž„๊ต์ˆ˜๋Š” 1๋ช…
        ์ฃผ์ž„๊ต์ˆ˜ 1๋ช…๋‹น ํ•™์ƒ ์—ฌ๋Ÿฌ๋ช…
      • ํ•™์ƒ - ์ฃผ์ž„๊ต์ˆ˜ (N:1)
      • ๊ต์ˆ˜ - ์ฃผ์ž„๊ต์ˆ˜ (1:1)
      • ํ•™์ƒ - ๊ต์ˆ˜ (1:N)
  • ์˜ˆ์‹œ 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 (๋ถ€์„œ - ์ง์›)
  • ์˜ˆ์‹œ 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

[Note]

  • id๋Š” ๋ณดํ†ต PK, auto_increment
  • DB๋Š” ERD (Entity Relationship Diagram)
    • ์œ„์—๋Š” ํ…Œ์ด๋ธ” ๋ช…
    • ์•„๋ž˜๋Š” ์ปฌ๋Ÿผ ๋ช…
  • ํด๋ž˜์Šค๋Š” ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ
  • tinyint(1byte, 256)
  • smallint(2B, 65536)
  • int(4B)
  • ํ•™์ƒ์ˆ˜๊ฐ• ํ…Œ์ด๋ธ”์—์„œ PK๋ฅผ ๋ณตํ•ฉํ‚ค๋กœ ์žก์ง€ ์•Š๊ณ  ๋”ฐ๋กœ id๋กœ ๋‘๋Š” ์ด์œ 
    1. ORM ๊ตฌ์„ฑ ์‹œ ์ฐธ์กฐ(hasMany, belongsTo) ์šฉ์˜
    2. PK ์ธ๋ฑ์Šค ํฌ๊ธฐ๊ฐ€ ์ž‘์•„ ๋””์Šคํฌ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ์— ํšจ์œจ์ 
    3. ๋‘ ๊ฐœ ์ด์ƒ์˜ column์— PK(Unique Key)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด
      ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์ด ๋‚ด๋ถ€์—์„œ ํ•  ์ผ์ด ๋งŽ์•„์ง
    4. where ์กฐ๊ฑด์ ˆ์ด๋‚˜ join ์ ˆ์ด ๋ณต์žกํ•˜๊ณ  ๋Š๋ ค์ง
    5. ๋ณตํ•ฉํ‚ค๋ผ ํ•จ์€ ์˜๋ฏธ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ปฌ๋Ÿผ๋“ค์ด 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์ด ์ข‹์Œ
  • 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์‹œ ์„ฑ๋Šฅ ํ–ฅ์ƒ ์œ„ํ•จ)

Reference