Post

๐Ÿข 7. Database

๐Ÿซง ๋น„์ •๊ทœํ™” vs ์ •๊ทœํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ์ •๊ทœํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
    • ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋จ
  • ๋น„์ •๊ทœํ™” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
    • ์ฝ๋Š” ์‹œ๊ฐ„์„ ์ตœ์ ํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋จ

๐Ÿซง SQL ๋ฌธ

    1. ํ•™์ƒ ๋“ฑ๋ก
    • ๋‹ค๋ฅธ ์งˆ์˜๋ฌธ์œผ๋กœ ๊ฐ์‹ธ๊ธฐ
    SELECT StudentName, Students.StudentID, Cnt
    FROM {
      SELECT Students.StudentID, count(StudentCourse.CourseID) as [CNT]
      FROM Students LEFT JOIN StudentCourses
      ON Students.StudentID = StudentCOurse.StudentID
      GROUP BY Students.StudentID
    } T INNER JOIN Students on T.studentID = Students.StudentID
    
    • StudentName ์„ GROUP BY ์ ˆ์— ์ถ”๊ฐ€ํ•˜๊ธฐ
    SELECT Students.StudentID, count(StudentCourse.CourseID) as [CNT]
    FROM Students LEFT JOIN StudentCourses
    ON Students.StudentID = StudentCOurse.StudentID
    GROUP BY Students.StudentID, Students.StudentID
    
    • ์ง‘ํ•ฉ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ๊ธฐ
    SELECT max(StudnetName) as [StudentName], Students.StudentID,
        count(StudentCourse.CourseID) as [CNT]
      FROM Students LEFT JOIN StudentCourses
      ON Students.StudentID = StudentCOurse.StudentID
      GROUP BY Students.StudentID
    
    1. ์ˆ˜๊ฐ•์ƒ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    SELECT TeacherName, isnull(StudnetSize.Number, 0)
    FROM Teachers LEFT JOIN
    (SELECT TeacherID, count(StudnetCourses.CourseID) AS [Number]
    FROM Courses INNER JOIN StudentCourses
    ON Courses.CourseID = StudentCourses.CourseID
    GROUP BY Courses.TeacherID) StudentSzie
    ON Teachers.TeacherID = StudentSize.TeacherID
    ORDER BY StudentSize.Number DESC
    

๐Ÿซง ์†Œ๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„

  • 1๋‹จ๊ณ„: ๋ชจํ˜ธ์„ฑ ์ฒ˜๋ฆฌ
    • ์ •ํ™•ํžˆ ๋ฌด์—‡์„ ์„ค๊ณ„ํ•ด์•ผ ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๊ธฐ
  • 2๋‹จ๊ณ„: ํ•ต์‹ฌ ๊ฐ์ฒด ์ •์˜
    • ํ•ต์‹ฌ ๊ฐ์ฒด ํ•˜๋‚˜๋‹น ๋ณดํ†ต ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ” ์‚ฌ์šฉํ•จ
  • 3๋‹จ๊ณ„: ๊ด€๊ณ„ ๋ถ„์„
    • ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ์–ด๋–ค ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š”์ง€ ๋ถ„์„ํ•˜๊ธฐ
  • 4๋‹จ๊ณ„: ํ–‰์œ„ ์กฐ์‚ฌ
    • ์ˆ˜ํ–‰๋  ์ž‘์—…๋“ค์„ ์‚ดํŽด๋ณด๊ณ , ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ €์žฅํ•˜๊ณ  ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ์ดํ•ดํ•˜๊ธฐ

๐Ÿซง ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„

  • join ์—ฐ์‚ฐ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•„์ฃผ ๋Š๋ฆฌ๋‹ค๊ณ  ๊ฐ„์ฃผํ•˜๊ธฐ
  • ๋ฐ์ดํ„ฐ๋ฅผ ๋น„์ •๊ทœํ™” ํ•ด์•ผ ํ•จ

๐Ÿซง ๋ฉด์ ‘ ๋ฌธ์ œ

24.png

  • 14.1 ํ•˜๋‚˜ ์ด์ƒ์˜ ์ง‘: ํ•˜๋‚˜ ์ด์ƒ์˜ ์ง‘์„ ๋Œ€์—ฌํ•œ ๋ชจ๋“  ๊ฑฐ์ฃผ์ž์˜ ๋ชฉ๋ก์„ ๊ตฌํ•˜๋Š” SQL ์งˆ์˜๋ฌธ
SELECT TenantName
FROM Tenants
INNER JOIN
(SELECT TenantID FROM AptTenants GROUP BY TenantID HAVING count(*) > 1) C
ON Tenants.TenantID = C.TenantID
  • 14.2 Open ์ƒํƒœ์ธ Request: ๋ชจ๋“  ๊ฑด๋ฌผ ๋ชฉ๋ก๊ณผ Status ๊ฐ€ Open ์ธ ๋ชจ๋“  Request ์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
SELECT BuildingName, ISNULL(Count, 0) as 'Count'
FROM Buildings
WHERE Requests.Status = 'Open'
  • 14.3 Request ๋ฅผ Close๋กœ ๋ฐ”๊พธ๊ธฐ: 11๋ฒˆ ๋นŒ๋”ฉ์—์„œ ๋Œ€๊ทœ๋ชจ ๋ฆฌ๋ชจ๋ธ๋ง ๊ณต์‚ฌ ์ง„ํ–‰์ค‘์ž„. ์ด ๊ฑด๋ฌผ์— ์žˆ๋Š” ๋ชจ๋“  ์ง‘์— ๋Œ€ํ•œ ๋ชจ๋“  Request ์˜ Status ๋ฅผ Close ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ์งˆ์˜๋ฌธ ์ž‘์„ฑํ•˜๋ผ
1
2
3
UPDATE Requests
SET Status = 'Close'
WHERE AptID IN (SELECT AptID FROM Apartments WHERE BuildingID = 11)
  • 14.4 JOIN: ์„œ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ JOIN ์€ ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”๊ฐ€? ๊ฐ๊ฐ์ด ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅด๊ณ , ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์–ด๋–ค JOIN ๊ณผ ์–ด์šธ๋ฆฌ๋Š”์ง€ ์„ค๋ช…ํ•ด๋ผ

  • Inner JOIN: ์„œ๋กœ ๋งค์นญ๋˜๋Š” ๊ฒƒ๋งŒ ์กฐํšŒํ•œ๋‹ค. (๊ต์ง‘ํ•ฉ) 25.png

  • Outer JOIN: ๋งค์นญ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก ๋˜๋Š” ์šฐ์ธก์˜ ๋ฐ์ดํ„ฐ๋„ ๊ฐ™์ด ์กฐํšŒํ•œ๋‹ค.

    • Left Outer JOIN : LEFT OUTER JOIN B ํ•  ๊ฒฝ์šฐ A, B ํ…Œ์ด๋ธ”๊ฐ„ ์กฐ์ธ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š๋Š” A์™€B ํ…Œ์ด๋ธ” ์ค‘ A ํ…Œ์ด๋ธ”์€ ๋ฌด์กฐ๊ฑด ๊ฒฐ๊ณผ๊ฐ’์— ํฌํ•จ๋˜์–ด ๋‚˜์˜ค๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ B์˜ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๊ทธ ์†์„ฑ์„ NULL๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 26.png

    • Right Outer JOIN : A์™€B ํ…Œ์ด๋ธ” ์ค‘ B ํ…Œ์ด๋ธ”์€ ๋ฌด์กฐ๊ฑด ๊ฒฐ๊ณผ๊ฐ’์— ํฌํ•จ๋˜์–ด ๋‚˜์˜ค๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ A์˜ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๊ทธ ์†์„ฑ์„ NULL๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 27.png

    • Full Outer JOIN : ์ขŒ์ธก๊ณผ ์šฐ์ธก์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด JOINํ•œ๋‹ค. 28.png

  • Natural JOIN: ๋‘ ํ…Œ์ด๋ธ”์„ JOINํ• ๋•Œ ์ปฌ๋Ÿผ๋ช…, ํƒ€์ž…์ด ๊ฐ™์€ ๋ชจ๋“  ์ปฌ๋Ÿผ์„ JOIN ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. 29.png

  • Self JOIN: ์ž๊ธฐ ์ž์‹ ์˜ ํ…Œ์ด๋ธ”์„ JOINํ•˜๋Š” ๊ฒƒ์ด๋‹ค.- employees ํ…Œ์ด๋ธ”์ด ์‚ฌ์›๋ฒˆํ˜ธ, ์ƒ์‚ฌ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ํŠน์ • ์‚ฌ์›์˜ ์ƒ์‚ฌ๋ฅผ ๊ตฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Self JOIN์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  • 14.5 ๋น„์ •๊ทœํ™”: ๋น„์ •๊ทœํ™”๋ž€ ๋ฌด์—‡์ธ๊ฐ€? ๊ทธ ์žฅ๋‹จ์ ์„ ์„ค๋ช…ํ•ด๋ผ

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

      • ์ˆ˜ํ–‰ ์†๋„๊ฐ€ ๋งŽ์ด ๋Š๋ฆฐ ๊ฒฝ์šฐ
      • ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ ์—ฐ์‚ฐ์„ ์ง€๋‚˜์น˜๊ฒŒ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ์ˆ ์ ์œผ๋กœ ์–ด๋ ค์šด ๊ฒฝ์šฐ
      • ํ…Œ์ด๋ธ”์— ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๊ณ , ๋‹ค๋Ÿ‰์˜ ๋ฒ”์œ„ ํ˜น์€ ํŠน์ • ๋ฒ”์œ„๋ฅผ ์ž์ฃผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
    • ๋ฐ˜์ •๊ทœํ™”์˜ ์žฅ์ ๊ณผ ๋‹จ์ 
      • ์žฅ์ 
        • ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Œ
        • ์กฐ์ธ์„ ์ œ๊ฑฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ ์‹œ๊ฐ„์ด ์ตœ์ ํ™”
      • ๋‹จ์ 
        • ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…, ์‚ญ์ œ, ์ˆ˜์ • ๋“ฑ ๊ฐฑ์‹  ์‹œ ๋น„์šฉ์ด ๋†’์•„์ง
        • ๋ฐ์ดํ„ฐ๊ฐ„์˜ ์ผ๊ด€์„ฑ์ด ๊นจ์งˆ ์ˆ˜ ์žˆ์Œ (์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋  ์ˆ˜ ์žˆ์Œ)
        • ๋งŽ์€ ์ €์žฅ ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ
  • 14.6 ๊ฐœ์ฒด-๊ด€๊ณ„ ๋‹ค์ด์–ด๊ทธ๋žจ: ํšŒ์‚ฌ, ์‚ฌ๋žŒ, ์ง์›์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ER ๋‹ค์ด์–ด๊ทธ๋žจ์„ ๊ทธ๋ ค๋ผ

1
2
3
4
5
6
7
8
companyID        int
companyName      varchar(10)

peopleID         int
peopleName       varchar(10)

employeeID       int
employeeName     varchar(10)
  • 14.7 ์„ฑ์  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„: ํ•™์ƒ๋“ค์˜ ์„ฑ์ ์„ ์ €์žฅํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ค๊ณ„ํ•˜๊ณ , ์„ฑ์ ์ด ์šฐ์ˆ˜ํ•œ ํ•™์ƒ(์ƒ์œ„ 10%) ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•˜๋Š” sql ์งˆ์˜๋ฌธ์„ ์ž‘์„ฑํ•ด๋ผ. ๋‹จ ํ•™์ƒ ๋ชฉ๋ก์€ ํ‰๊ท  ์„ฑ์ ์— ๋”ฐ๋ผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด์•ผ ํ•œ๋‹ค
1
SELECT ํ•™์ƒ, avg(์„ฑ์ ) FROM ์„ฑ์ ํ‘œ ORDER BY avg(์„ฑ์ ) DESC;
This post is licensed under CC BY 4.0 by the author.