๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/42890

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ›„๋ณดํ‚ค

[["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]] 2

programmers.co.kr

ํ”„๋ Œ์ฆˆ๋Œ€ํ•™๊ต ์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ ์กฐ๊ต์ธ ์ œ์ด์ง€๋Š” ๋„ค์˜ค ํ•™๊ณผ์žฅ๋‹˜์˜ ์ง€์‹œ๋กœ, ํ•™์ƒ๋“ค์˜ ์ธ์ ์‚ฌํ•ญ์„ ์ •๋ฆฌํ•˜๋Š” ์—…๋ฌด๋ฅผ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

๊ทธ์˜ ํ•™๋ถ€ ์‹œ์ ˆ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฒฝํ—˜์„ ๋˜์‚ด๋ ค, ๋ชจ๋“  ์ธ์ ์‚ฌํ•ญ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋„ฃ๊ธฐ๋กœ ํ•˜์˜€๊ณ , ์ด๋ฅผ ์œ„ํ•ด ์ •๋ฆฌ๋ฅผ ํ•˜๋˜ ์ค‘์— ํ›„๋ณดํ‚ค(Candidate Key)์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ํ•„์š”ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ›„๋ณดํ‚ค์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ์ž˜ ๊ธฐ์–ต๋‚˜์ง€ ์•Š๋˜ ์ œ์ด์ง€๋Š”, ์ •ํ™•ํ•œ ๋‚ด์šฉ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ์„œ์ ์„ ํ™•์ธํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์„ ํ™•์ธํ•˜์˜€๋‹ค.

  • ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฆด๋ ˆ์ด์…˜(Relation)์˜ ํŠœํ”Œ(Tuple)์„ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ(Attribute) ๋˜๋Š” ์†์„ฑ์˜ ์ง‘ํ•ฉ ์ค‘, ๋‹ค์Œ ๋‘ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒƒ์„ ํ›„๋ณด ํ‚ค(Candidate Key)๋ผ๊ณ  ํ•œ๋‹ค.
    • ์œ ์ผ์„ฑ(uniqueness) : ๋ฆด๋ ˆ์ด์…˜์— ์žˆ๋Š” ๋ชจ๋“  ํŠœํ”Œ์— ๋Œ€ํ•ด ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„๋˜์–ด์•ผ ํ•œ๋‹ค.
    • ์ตœ์†Œ์„ฑ(minimality) : ์œ ์ผ์„ฑ์„ ๊ฐ€์ง„ ํ‚ค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์†์„ฑ(Attribute) ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ œ์™ธํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์ผ์„ฑ์ด ๊นจ์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, ๋ฆด๋ ˆ์ด์…˜์˜ ๋ชจ๋“  ํŠœํ”Œ์„ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ๊ผญ ํ•„์š”ํ•œ ์†์„ฑ๋“ค๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•œ๋‹ค.

์ œ์ด์ง€๋ฅผ ์œ„ํ•ด, ์•„๋ž˜์™€ ๊ฐ™์€ ํ•™์ƒ๋“ค์˜ ์ธ์ ์‚ฌํ•ญ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ›„๋ณด ํ‚ค์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ.

์œ„์˜ ์˜ˆ๋ฅผ ์„ค๋ช…ํ•˜๋ฉด, ํ•™์ƒ์˜ ์ธ์ ์‚ฌํ•ญ ๋ฆด๋ ˆ์ด์…˜์—์„œ ๋ชจ๋“  ํ•™์ƒ์€ ๊ฐ์ž ์œ ์ผํ•œ "ํ•™๋ฒˆ"์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ "ํ•™๋ฒˆ"์€ ๋ฆด๋ ˆ์ด์…˜์˜ ํ›„๋ณด ํ‚ค๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.


๊ทธ๋‹ค์Œ "์ด๋ฆ„"์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ™์€ ์ด๋ฆ„("apeach")์„ ์‚ฌ์šฉํ•˜๋Š” ํ•™์ƒ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, "์ด๋ฆ„"์€ ํ›„๋ณด ํ‚ค๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ๋งŒ์•ฝ ["์ด๋ฆ„", "์ „๊ณต"]์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ฆด๋ ˆ์ด์…˜์˜ ๋ชจ๋“  ํŠœํ”Œ์„ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ํ›„๋ณด ํ‚ค๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
๋ฌผ๋ก  ["์ด๋ฆ„", "์ „๊ณต", "ํ•™๋…„"]์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด๋„ ๋ฆด๋ ˆ์ด์…˜์˜ ๋ชจ๋“  ํŠœํ”Œ์„ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ›„๋ณด ํ‚ค๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค.


๋”ฐ๋ผ์„œ, ์œ„์˜ ํ•™์ƒ ์ธ์ ์‚ฌํ•ญ์˜ ํ›„๋ณดํ‚ค๋Š” "ํ•™๋ฒˆ", ["์ด๋ฆ„", "์ „๊ณต"] ๋‘ ๊ฐœ๊ฐ€ ๋œ๋‹ค.

๋ฆด๋ ˆ์ด์…˜์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด relation์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ๋ฆด๋ ˆ์ด์…˜์—์„œ ํ›„๋ณด ํ‚ค์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ.

์ œํ•œ์‚ฌํ•ญ

  • relation์€ 2์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด๋‹ค.
  • relation์˜ ์ปฌ๋Ÿผ(column)์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 8 ์ดํ•˜์ด๋ฉฐ, ๊ฐ๊ฐ์˜ ์ปฌ๋Ÿผ์€ ๋ฆด๋ ˆ์ด์…˜์˜ ์†์„ฑ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • relation์˜ ๋กœ์šฐ(row)์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ด๋ฉฐ, ๊ฐ๊ฐ์˜ ๋กœ์šฐ๋Š” ๋ฆด๋ ˆ์ด์…˜์˜ ํŠœํ”Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • relation์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 8 ์ดํ•˜์ด๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
  • relation์˜ ๋ชจ๋“  ํŠœํ”Œ์€ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ ๊ฐ€๋Šฅํ•˜๋‹ค.(์ฆ‰, ์ค‘๋ณต๋˜๋Š” ํŠœํ”Œ์€ ์—†๋‹ค.)

ํ’€์ด

์ด ๋ฌธ์ œ๋Š” ๊ฒฐ๊ตญ ์œ ์ผ์„ฑ์ตœ์†Œ์„ฑ์„ ํŒ๋‹จํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

 

๋จผ์ € ์ฃผ์–ด์ง€๋Š” ์ปฌ๋Ÿผ๋“ค์˜ ์กฐํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ์ฒ˜๋Ÿผ ํ•™๋ฒˆ, ์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„ ์ปฌ๋Ÿผ์ด ์žˆ๋‹ค๋ฉด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์„ ๋จผ์ € ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง€๋Š” ์กฐํ•ฉ๋“ค์˜ ์ง‘ํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

{ํ•™๋ฒˆ}, {ํ•™๋ฒˆ, ์ด๋ฆ„}, {ํ•™๋ฒˆ, ์ „๊ณต}, {ํ•™๋ฒˆ, ํ•™๋…„}, {์ด๋ฆ„, ์ „๊ณต}, {์ด๋ฆ„, ํ•™๋…„}, {์ „๊ณต, ํ•™๋…„}, {ํ•™๋ฒˆ, ์ด๋ฆ„, ์ „๊ณต}, {ํ•™๋ฒˆ, ์ „๊ณต, ํ•™๋…„}, {์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„}, {ํ•™๋ฒˆ, ์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„}

 

์ด์ œ ์ด ์ปฌ๋Ÿผ ์กฐํ•ฉ๋“ค์— ๋Œ€ํ•ด ์œ ์ผ์„ฑ๊ณผ ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ํ›„๋ณดํ‚ค์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์œ ์ผ์„ฑ์„ ํŒ๋‹จํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹จ์ˆœํžˆ ํ•ด๋‹น ์ปฌ๋Ÿผ์˜ ๊ฐ’๋“ค ์ค‘ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์„ ๋ชจ๋‘ ๋Œ๋ฉด์„œ ์ปฌ๋Ÿผ์˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด Set์— ์ง‘์–ด๋„ฃ๊ณ , ์ด๋ฏธ Set์— ๊ฐ™์€ ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ์ค‘๋ณต๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง„ ํ–‰์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ์œ ์ผ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ์˜ˆ์‹œ์—์„œ {์ „๊ณต, ํ•™๋…„} ์ปฌ๋Ÿผ์— ๋Œ€ํ•ด ์œ ์ผ์„ฑ์„ ํŒ๋‹จํ•˜๋ ค๋ฉด ๊ฐ ํ–‰์˜ ๋‘ ์ปฌ๋Ÿผ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์ณ Set์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด Set์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.

 

{music2, math2, computer2, computer1, music3}

 

์ด๋•Œ ๋งˆ์ง€๋ง‰ ํ–‰์˜ ๋ฐ์ดํ„ฐ๊ฐ€ music2์ธ๋ฐ, ์ด๋ฏธ Set์—๋Š” ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ์ด ์ปฌ๋Ÿผ ์กฐํ•ฉ์€ ์œ ์ผ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋Š” ์กฐํ•ฉ์œผ๋กœ ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ ์ผ์„ฑ์ด ํ™•์ธ๋˜์—ˆ๋‹ค๋ฉด, ์ด์ œ ์ตœ์†Œ์„ฑ์„ ํ™•์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ตœ์†Œ์„ฑ์ด ๋งŒ์กฑ๋˜๋ ค๋ฉด ์ปฌ๋Ÿผ ์กฐํ•ฉ์˜ ์š”์†Œ๋“ค ์ค‘ ์ผ๋ถ€๋งŒ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์œ ์ผ์„ฑ์ด ์ ˆ๋Œ€ ๋งŒ์กฑ๋˜์–ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ ์ผ์„ฑ์ด ํ™•์ธ๋œ ์กฐํ•ฉ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์‹œ ๋ชจ๋“  ์ปฌ๋Ÿผ๋“ค์˜ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜์—ฌ ์œ ์ผ์„ฑ์ด ๋งŒ์กฑ๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋Š” ๊ฒƒ์œผ๋กœ ์ตœ์†Œ์„ฑ์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์˜ˆ๋ฅผ ๋“ค์–ด, ์˜ˆ์‹œ์—์„œ {์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„} ์กฐํ•ฉ์˜ ์œ ์ผ์„ฑ์„ ๋จผ์ € ๊ฒ€์‚ฌํ•ด๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด Set์„ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋˜๊ณ  ์œ ์ผ์„ฑ์ด ๋งŒ์กฑ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

{ryanmusic2, apeachmath2, tubecomputer3, concomputer1, muzimusic3, apeachmusic2} 

 

์ด์ œ ์ตœ์†Œ์„ฑ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์œ„ ์ปฌ๋Ÿผ์กฐํ•ฉ์˜ ์š”์†Œ๋“ค์„ ๋ฝ‘์•„ ๋‹ค์‹œ ์ปฌ๋Ÿผ ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. {์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„}์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

{์ด๋ฆ„}, {์ด๋ฆ„, ์ „๊ณต}, {์ด๋ฆ„, ํ•™๋…„}, {์ „๊ณต, ํ•™๋…„}, {์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„}

 

๊ฐ ์ปฌ๋Ÿผ ์กฐํ•ฉ์— ๋Œ€ํ•ด ์œ ์ผ์„ฑ์„ ํ™•์ธํ•ด๋ณด๋ฉด, {์ด๋ฆ„, ์ „๊ณต}๋งŒ์œผ๋กœ๋„ ์œ ์ผ์„ฑ์„ ๋ณด์žฅ๋จ์„ ์•Œ๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

 

{ryanmusic, apeachmath, tubecomputer, concomputer, muzimusic, apeachmusic}

 

๋”ฐ๋ผ์„œ, {์ด๋ฆ„, ์ „๊ณต, ํ•™๋…„} ์กฐํ•ฉ์€ ์œ ์ผ์„ฑ์€ ๋งŒ์กฑ์‹œํ‚ค์ง€๋งŒ ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ›„๋ณดํ‚ค๊ฐ€ ๋  ์ˆ˜ ์—†์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ๋ฐฉ์‹์œผ๋กœ ์œ ์ผ์„ฑ์ด ๋งŒ์กฑ๋˜๋Š” ์ปฌ๋Ÿผ์กฐํ•ฉ์œผ๋กœ ๋ชจ๋“  ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด ๋ชจ๋“  ์„œ๋ธŒ์ปฌ๋Ÿผ์กฐํ•ฉ๋“ค์ด ์œ ์ผ์„ฑ์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•ด๋‹น ์ปฌ๋Ÿผ์กฐํ•ฉ์„ ํ›„๋ณดํ‚ค๋กœ ํŒ๋‹จํ•˜๊ณ  ์ •๋‹ต์— ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ