Post

๐Ÿน 7. Database

14. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

[ SQL ๋ฌธ๋ฒ•๊ณผ ๊ทธ ๋ณ€์ข…๋“ค ]

  • ๋ช…์‹œ์ (explicit) JOIN
1
2
3
SELECT CourseName, TeacherName
FROM Course INNER JOIN Teachers
ON Courses.TeacherID = Teachers.TeacherID
  • ๋ฌต์‹œ์ (implicit) JOIN
1
2
3
SELECT CourseName, TeacherName
FROM Courses, Teachers
WHERE Courses.TeacherID = Teachers.TeacherID

[ ๋น„์ •๊ทœํ™” vs ์ •๊ทœํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ]

  • ์ •๊ทœํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

    : ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

    • Courses์—๋Š” TeacherID์ฒ˜๋Ÿผ Teachers์— ๋Œ€ํ•œ ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ–๋Š” ์ปฌ๋Ÿผ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.
    • ์žฅ์ : ๊ต์‚ฌ ์ •๋ณด(์ด๋ฆ„, ์ฃผ์†Œ ๋“ฑ๋“ฑ)๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ•œ ๋ฒˆ๋งŒ ์ €์žฅํ•ด๋„ ๋œ๋‹ค.
    • ๋‹จ์ : ์ƒ๋‹น์ˆ˜์˜ ์ผ์ƒ์  ์งˆ์˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด JOIN์„ ๋งŽ์ด ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๋น„์ •๊ทœํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

    : ์ฝ๋Š” ์‹œ๊ฐ„์„ ์ตœ์ ํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

    • ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๋ณตํ•ด์„œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋งŒ์•ฝ ๊ฐ™์€ ์งˆ์˜๋ฅผ ์ž์ฃผ ๋ฐ˜๋ณตํ•ด์•ผ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ์œผ๋ฉด, ๊ต์‚ฌ์˜ ์ด๋ฆ„ ์ •๋ณด๋ฅผ Courses ํ…Œ์ด๋ธ”์— ์ค‘๋ณตํ•ด ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    • ๋†’์€ ๊ทœ๋ชจ ํ™•์žฅ์„ฑ์„ ์‹คํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

[ SQL๋ฌธ ]

์œ„ ์˜ˆ์ œ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1
2
3
4
Courses: CourseID*, CourseName, TeacherID
Teachers: TeacherID*, TeacherName
Students: StudentID*, StudentName
StudentCourses: CourseID*, StudentID*

์œ„ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•ด ์งˆ์˜๋ฌธ์„ ๊ตฌํ˜„ํ•ด๋ณด์ž.

์งˆ์˜ #1: ํ•™์ƒ ๋“ฑ๋ก

  • ๋ชจ๋“  ํ•™์ƒ์˜ ๋ชฉ๋ก์„ ๋ฝ‘๊ณ  ๊ฐ ํ•™์ƒ์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๊ฐ•์˜๋ฅผ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์งˆ์˜๋ฌธ
1
2
3
4
5
/* ์ž˜๋ชป๋œ ์ฝ”๋“œ */
SELECT Students.StudentName, count(*)
FROM Students INNER JOIN StudentCourses
ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID

์ด ์ฝ”๋“œ์—๋Š” 3๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.

  1. ๊ฐ•์˜๋ฅผ ์•„์˜ˆ ์ˆ˜๊ฐ•ํ•˜์ง€ ์•Š๋Š” ํ•™์ƒ์€ ๋ชฉ๋ก์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์™œ๋ƒํ•˜๋ฉด Student Courses๊ฐ€ ์ˆ˜๊ฐ• ์‹ ์ฒญํ•œ ํ•™์ƒ๋งŒ์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

    โ†’ LEFT JOIN์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค.

  2. LEFT JOIN์„ ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. count(*)๋Š” StudentID๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ทธ๋ฃน ๋‚ด์— ์กด์žฌํ•˜๋Š” ์•„์ดํ…œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ•์˜๋ฅผ ์•„์˜ˆ ์ˆ˜๊ฐ•ํ•˜์ง€ ์•Š๋Š” ํ•™์ƒ์˜ ๊ฒฝ์šฐ์—๋„ 1๋กœ ๊ณ„์‚ฐ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.

    โ†’ count(StudentsCourses.CourseID)์™€ ๊ฐ™์ด ๊ทธ๋ฃน ๋‚ด์˜ CourseID ์ˆ˜๋ฅผ ์„ธ๋„๋ก ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค.

  3. Students.StudentID๋ฅผ ์‚ฌ์šฉํ•ด ๊ทธ๋ฃน์„ ๋งŒ๋“ค์—ˆ์ง€๋งŒ, ์—ฌ์ „ํžˆ ํ•œ ๊ทธ๋ฃน ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ StudentName์ด ์กด์žฌํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์–ด๋–ค StudentName์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค. ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค ํ•ด๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๊ทธ ์‚ฌ์‹ค์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•  ๊ฒƒ์ด๋‹ค.

    โ†’ first(Students.StudentName)๊ณผ ๊ฐ™์€ ์ง‘๊ณ„ ํ•จ์ˆ˜(aggregate function)๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

์œ„ ๋ฌธ์ œ๋ฅผ ๊ณ ์ณ ๋‹ค์‹œ ๋งŒ๋“  ์งˆ์˜๋ฌธ์ด๋‹ค.

1
2
3
4
5
6
7
8
/* ์ฒซ ๋ฒˆ์งธ ํ•ด๋ฒ•: ๋‹ค๋ฅธ ์งˆ์˜๋ฌธ์œผ๋กœ ๊ฐ์‹ธ๊ธฐ */
SELECT StudentName, Student.StudentID, Cnt
FROM (
		SELECT Students.StudentID, count(StudentCourses.CourseID) as [Cnt]
		FROM Students LEFT JOIN StudentCourses
		ON Students.StudeentID = StudentCourses.StudentID
		GROUP BY Students.StudentID
) T INNER JOIN Students on T.StudentID = Students.StudentID

์œ„ ์ฝ”๋“œ 3๋ฒˆ์งธ ์ค„์—์„œ ๊ทธ๋ƒฅ ํ•™์ƒ ์ด๋ฆ„์„ SELECTํ•˜๋ฉด 3~6๋ฒˆ ์ค„์—์„œ ์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ์งˆ์˜๋ฌธ์œผ๋กœ ๊ฐ์Œ€ ํ•„์š”๊ฐ€ ์—†์ง€ ์•Š๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‹ค์‹œ ๋งŒ๋“ค์–ด๋ณธ ์ž˜๋ชป๋œ ํ•ด๊ฒฐ์ฑ… ์ฝ”๋“œ์ด๋‹ค.

1
2
3
4
5
/* ์ž˜๋ชป๋œ ์ฝ”๋“œ */
SELECT StudentName, Students.StudentID, count(StudentCourses.CourseID) as [Cnt]
FROM Students LEFT JOIN StudentCourses
ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID

์ด ์ฝ”๋“œ๋Š” ์ด์ „ ์ฝ”๋“œ์™€ ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์—†๋‹ค. SELECT๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์€ ์ง‘๊ณ„ ํ•จ์ˆ˜๋‚˜ GROUP BY ์ ˆ์— ํฌํ•จ๋œ ๊ฐ’ ๋ฟ์ด๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์จ์•ผํ•œ๋‹ค.

1
2
3
4
5
/* ๋‘ ๋ฒˆ์งธ ํ•ด๋ฒ•: StudentName์„ GROUP BY ์ ˆ์— ์ถ”๊ฐ€ํ•˜๊ธฐ */
SELECT StudentName, Students.StudentID, count(StudentCourses.CourseID) as [Cnt]
FROM Students LEFT JOIN StudentCourses
ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID, Students.StudentName

๋˜ ๋‹ค๋ฅธ ํ•ด๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1
2
3
4
5
6
/* ์„ธ ๋ฒˆ์งธ ํ•ด๋ฒ•: ์ง‘ํ•ฉ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ๊ธฐ */
SELECT max(StudentName) as [StudentName], Students.StudentID, 
							count(StudentCourses.CourseID) as [Count]
FROM Students LEFT JOIN StudentCourses
ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID

์งˆ์˜ #2: ์ˆ˜๊ฐ•์ƒ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

  • ๋ชจ๋“  ๊ต์‚ฌ ๋ชฉ๋ก๊ณผ ๊ฐ ๊ต์‚ฌ๊ฐ€ ๊ฐ€๋ฅด์น˜๋Š” ํ•™์ƒ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์งˆ์˜๋ฌธ

๋งŒ์•ฝ ํ•œ ๊ต์‚ฌ๊ฐ€ ๋™์ผํ•œ ํ•™์ƒ์„ ์—ฌ๋Ÿฌ ๊ฐ•์˜์—์„œ ๊ฐ€๋ฅด์น˜๋Š” ๊ฒฝ์šฐ, ๊ทธ ๊ฐ๊ฐ์„ ๋‹ค๋ฅธ ํ•™์ƒ์œผ๋กœ ๊ฐ„์ฃผํ•ด์„œ ๊ฐ€๋ฅด์น˜๋Š” ํ•™์ƒ ์ˆ˜์— ํ•ฉ์‚ฐํ•œ๋‹ค. ๊ต์‚ฌ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ ๊ต์‚ฌ๊ฐ€ ๊ฐ€๋ฅด์น˜๋Š” ํ•™์ƒ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉด ๋œ๋‹ค.

1
2
3
4
SELECT TeacherID, count(StudentCourses.CourseID) AS [Number]
FROM Courses INNER JOIN StudentCourses
ON Courses.CourseID = StudentCourses.CourseID
GROUP BY Courses.TeacherID

INNER JOIN์„ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ•์˜๋ฅผ ํ•˜์ง€ ์•Š๋Š” ๊ต์‚ฌ๋Š” ๋ชฉ๋ก์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด LEFT JOIN์„ ํ•˜๋ฉด ๋ชจ๋“  ๊ต์‚ฌ ๋ชฉ๋ก์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
SELECT TeacherName, isnull(StudentSize.Number, 0)
FROM Teachers LEFT JOIN
		(SELECT TeacherID, count(StudentCourses.CourseID) AS [Number]
		FROM Courses INNER JOIN StudentCourses
		ON Courses.CourseID = StudentCourses.CourseID
		GROUP BY Courses.TeacherID) StudentSize
ON Teachers.TeacherID = StudentSize.TeacherID
ORDER BY StudentSize.Number DESC

NULL๊ฐ’์„ 0์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด SELECT ๋ฌธ์—์„œ isnull์„ ์‚ฌ์šฉํ•œ๋‹ค.

  • isnull(a, b): NULL์ด ์•„๋‹ ๋•Œ ๊ฐ’ a, NULL์ผ ๋•Œ ๊ฐ’ b

[ ์†Œ๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„ ]

๋ฉด์ ‘์žฅ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ค๊ณ„ํ•ด๋ณด๋ผ๋Š” ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ! ์ ‘๊ทผ๋ฒ•!

๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ๋น„์Šทํ•œ ์ ์ด ๋งŽ๋‹ค.

1๋‹จ๊ณ„: ๋ชจํ˜ธ์„ฑ ์ฒ˜๋ฆฌ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ด€๋ จ๋œ ๋ฌธ์ œ์—๋Š” ๋ชจํ˜ธํ•œ ๋ถ€๋ถ„์ด ๋‚ดํฌ๋˜์–ด ์žˆ๋‹ค. ์ •ํ™•ํžˆ ๋ฌด์—‡์„ ์„ค๊ณ„ํ•ด์•ผ ํ•˜๋Š”์ง€ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค.

[ ์•„ํŒŒํŠธ ์ž„๋Œ€ ๋Œ€ํ–‰์—…์ฒด๊ฐ€ ์‚ฌ์šฉํ•  ์‹œ์Šคํ…œ์„ ์„ค๊ณ„ ]

  • ์ด ์—…์ฒด์˜ ๋Œ€๋ฆฌ์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ์ง€ ์•„๋‹ˆ๋ฉด ํ•˜๋‚˜๋งŒ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ์–ผ๋งˆ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ์„ค๊ณ„ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ฉด์ ‘๊ด€๊ณผ ๋…ผ์˜ํ•ด์•ผ ํ•œ๋‹ค.
  • ex) ์–ด๋–ค ์‚ฌ๋žŒ์ด ๊ฐ™์€ ๋นŒ๋”ฉ์— ์žˆ๋Š” ์ง‘์„ ๋‘ ๊ฐœ ๋นŒ๋ฆฌ๋Š” ์ผ์€ ๊ต‰์žฅํžˆ ๋“œ๋ฌผ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ์ˆ˜๋„ ์žˆ๊ณ , ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค.

    โ†’ ๋“œ๋ฌผ๊ฒŒ ์ƒ๊ธฐ๋Š” ์ผ์€ ๋ณ„๋„์˜ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์„œ, ์ฆ‰ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ณด๊ด€๋œ ๊ทธ ์‚ฌ๋žŒ์˜ ๊ฐœ์ธ ์ •๋ณด๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค๊ฑฐ๋‚˜ ํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

2๋‹จ๊ณ„: ํ•ต์‹ฌ ๊ฐ์ฒด ์ •์˜

๊ทธ ๋‹ค์Œ์œผ๋กœ๋Š”, ์ด ์‹œ์Šคํ…œ์˜ ํ•ต์‹ฌ ๊ฐ์ฒด(core object)๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์‚ดํŽด๋ด์•ผ ํ•œ๋‹ค. ๋ณดํ†ต ํ•ต์‹ฌ ๊ฐ์ฒด ํ•˜๋‚˜๋‹น ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•œ๋‹ค.

์•„ํŒŒํŠธ ์ž„๋Œ€ ๋Œ€ํ–‰์—…์ฒด์˜ ๊ฒฝ์šฐ Property, Building, Apartment, Tenant, Manager ๋“ฑ์ด ํ•ต์‹ฌ ๊ฐ์ฒด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

3๋‹จ๊ณ„: ๊ด€๊ณ„ ๋ถ„์„

ํ•ต์‹ฌ ๊ฐ์ฒด์˜ ์œค๊ณฝ์„ ์žก๊ณ ๋‚˜๋ฉด ์–ด๋–ป๊ฒŒ ํ…Œ์ด๋ธ”์„ ์„ค๊ณ„ํ•ด์•ผ ํ• ์ง€ ๊ฐ์„ ์žก์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ์–ด๋–ค ๊ด€๊ณ„๊ฐ€ ์žˆ์„ ์ง€(๋‹ค๋Œ€๋‹ค, ์ผ๋Œ€๋‹ค ๋“ฑ) ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.

Buildings์™€ Apartments์˜ ๊ด€๊ณ„๊ฐ€ ์ผ-๋Œ€-๋‹ค ๊ด€๊ณ„๋ผ๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

Apartmentss
ApartmentIDint
ApartmentAddressvarchar(100)
BuildingIDint
Buildingss
BuildingIDint
BuildingNamevarchar(100)
BuildidngAddressvarchar(500)

Apartments ํ…Œ์ด๋ธ”๊ณผ Buildings ํ…Œ์ด๋ธ”์€ BuildingID๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ๋‹ค.

ํ•œ ์‚ฌ๋žŒ์ด ํ•˜๋‚˜ ์ด์ƒ์˜ ์•„ํŒŒํŠธ๋ฅผ ์ž„๋Œ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค-๋Œ€-๋‹ค ๊ด€๊ณ„๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

TenantApartmentss
TenantIDint
ApartmentIDint
Apartmentss
ApartmentIDint
ApartmentAddressvarchar(100)
BuildingIDint
Tenantss
TenantIDint
TenantNamevarchar(100)
TenantAddressvarchar(500)

TenantApartments ํ…Œ์ด๋ธ”์€ Tenants์™€ Apartments ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.

4๋‹จ๊ณ„: ํ–‰์œ„ ์กฐ์‚ฌ

๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ๋ถ€์ ์ธ ๋ถ€๋ถ„์„ ์ฑ„์›Œ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. ํ”ํžˆ ์ˆ˜ํ–‰๋œ ์ž‘์—…๋“ค์„ ์‚ดํŽด๋ณด๊ณ , ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ €์žฅํ•˜๊ณ  ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค.

์ž„๋Œ€์™€ ๊ด€๊ณ„๋œ ์šฉ์–ด๋“ค ํ‡ด๊ฑฐ(moving out)๋‚˜ ์ž„๋Œ€๋น„(rent payments)์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ์ž‘์—…๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”์ด ํ•„์š”ํ•˜๋‹ค.

[ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„ ]

๋Œ€๊ทœ๋ชจ์˜ ๊ทœ๋ชจ ํ™•์žฅ์„ฑ์ด ๋†’์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ์— JOIN ์—ฐ์‚ฐ(์•ž์„  ์˜ˆ์ œ)์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•„์ฃผ ๋Š๋ฆฌ๋‹ค๊ณ  ๊ฐ„์ฃผํ•ด์•ผ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„์ •๊ทœํ™”(denormalize)ํ•ด์•ผ ํ•œ๋‹ค.

This post is licensed under CC BY 4.0 by the author.