관리 메뉴

Partially Committed

Natural Join 의 μœ„ν—˜μ„±? (03/22) λ³Έλ¬Έ

πŸ’» Study !/Database System

Natural Join 의 μœ„ν—˜μ„±? (03/22)

WonderJay 2023. 3. 22. 17:39
728x90
λ°˜μ‘ν˜•
SMALL

이번 학기에 λ°μ΄ν„°λ² μ΄μŠ€ 쑰ꡐλ₯Ό 맑은 김에,

μž‘μ„±ν•œ μ§ˆμ˜μ‘λ‹΅ λ‚΄μš©μ„ κΈ°λ‘ν•˜λ©΄μ„œ λ³΅μŠ΅ν•΄λ³΄κΈ°λ‘œ ν–ˆλ‹€! 

 

Q. Student 와 takes relation 의 곡톡 attribute κ°€ ν•œ 개 밖에 μ—†λ‹€λ©΄

select name, title
from (student natural join takes) natural join course using (course_id)

라고 μž‘μ„±ν•  수 μžˆλ‹€λŠ” 것을 μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€λ§Œ λ§Œμ•½ student 와 takes relation μ—μ„œ 곡톡 attribute κ°€ μ—¬λŸ¬ 개 인 경우 (ex. name) μ•„λž˜μ™€ 같이 μž‘μ„±ν•˜λ©΄ λ™μΌν•œ 쿼리 κ²°κ³Όλ₯Ό 얻을 수 μžˆλ‚˜μš”?

select name, title
from (student natural join takes using id), natural join course using (course_id)

A. Natural Join 을 μ‚¬μš©ν•œ 경우, Join 의 기쀀이 λ˜λŠ” Attribute κ°€ λͺ…ν™•ν•œ κ²½μš°μ—λŠ” μ˜λ„λŒ€λ‘œ λ™μž‘ν•˜μ§€λ§Œ λ§Œμ•½μ— λͺ¨ν˜Έν•œ κ²½μš°μ—λŠ” μ˜λ„λŒ€λ‘œ λ™μž‘ν•˜μ§€ μ•Šμ„ 수 μžˆλ‹€λŠ” 점이 μ€‘μš”ν•˜λ‹€. (Danger in Natural Join) 이λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄μ„œ Join 의 기쀀을 λͺ…ν™•νžˆ μ œμ‹œν•˜κΈ° μœ„ν•΄ Using κ³Ό 같은 Clause λ₯Ό ν™œμš© ν•  수 μžˆλ‹€. μ΄λŸ¬ν•œ λ‚΄μš©μ„ λ°”νƒ•μœΌλ‘œ μ•„λž˜μ™€ 같이 μ§ˆμ˜μ‘λ‹΅ 닡변을 μž‘μ„±ν•˜μ˜€λ‹€.

 

06. Intermediate_sql κ°•μ˜μžλ£Œμ˜ 11번째 μŠ¬λΌμ΄λ“œμ—μ„œ λ§ν•˜λŠ” λ°”λŠ”

Natural Join μ‚¬μš© μ‹œ μ˜λ„μΉ˜ μ•Šμ€ λ™μž‘μ„ λ°©μ§€ν•˜κΈ° μœ„ν•΄ Using Clause λ₯Ό μ΄μš©ν•˜μ—¬

Join ν•˜λŠ” Key λ₯Ό μ§€μ •ν•  μˆ˜ μžˆλ‹€λŠ” κ²ƒμΈλ°μš”,

student μ™€ takes relation μ— λ™μΌν•œ attribute κ°€ ν•œ κ°œ λ°–에 μ—†λ‹€λ©΄

κ°•μ˜μžλ£Œμ— λ‚˜μ˜¨ μ˜ˆμ‹œλŒ€λ‘œ

select name, title

from (student natural join takes) natural join course using (course_id)



라고 μž‘μ„±ν–ˆμ„ λ•Œ μ˜λ„λŒ€λ‘œ λ™μž‘ν•˜κ² μ§€λ§Œ

λ§Œμ•½ student μ™€ takes relation μ— λ™μΌν•œ attribute κ°€ μ—¬λŸ¬ κ°œ μΌ κ²½μš°μ—λŠ” μ˜λ„μΉ˜ λͺ»ν•œ κ²°κ³Όκ°€ λ°œμƒν•  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€. 

(⭐ Natural Join 의 μœ„ν—˜μ„±)

이λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄ Using 을 톡해 Join ν•˜λŠ” key λ₯Ό λͺ…ν™•νžˆ μ§€μ •ν•˜μ—¬ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

select name, title

from (student natural join takes using id), natural join course using (course_id)


쿼리 κ²°κ³ΌλŠ” μ•„λ§ˆ λ™μΌν•˜κ²Œ λ‚˜μ˜¬ κ²ƒμœΌλ‘œ μ˜ˆμƒμ΄ λ©λ‹ˆλ‹€λ§Œ,

μ€‘μš”ν•œ κ²ƒμ€ Natural Join μ„ ν•¨λΆ€λ‘œ μ‚¬μš©ν•˜λ©΄ μ˜λ„μΉ˜ μ•Šμ€ κ²°κ³Όκ°€ λ°œμƒν•  μˆ˜ μžˆκ³ 

이λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄μ„œ Using Clause λ₯Ό ν†΅ν•΄ Join μ˜ κΈ°μ€€μ„ λͺ…ν™•ν•˜κ²Œ ν•˜λŠ” κ²ƒμ΄ λ°©λ²•μ΄ λ  μˆ˜ μžˆλ‹€.

λΌλŠ” κ²ƒμ„ μ΄ν•΄ν•˜κ³  λ„˜μ–΄κ°€λ©΄ λ  κ²ƒ κ°™μŠ΅λ‹ˆλ‹€!

ν˜Ήμ‹œ λ” κΆκΈˆν•˜μ‹  μ μ΄λ‚˜, λͺ¨ν˜Έν•œ λ‚΄μš©μ΄ μžˆμ„ μ‹œ μΆ”κ°€μ μœΌλ‘œ μ§ˆμ˜μ‘닡을 μ˜¬λ €μ£Όμ‹œλ©΄

같이 κ³ λ―Όν•΄λ³΄λ„둝 ν•˜κ² μŠ΅λ‹ˆλ‹€! πŸ˜Š

 

μ§ˆμ˜μ‘λ‹΅μ„ 곡개적으둜 μ˜¬λ¦¬λŠ”κ±° 쑰금 λΆ€λ‹΄λœλ‹€..

728x90
λ°˜μ‘ν˜•
LIST

'πŸ’» Study ! > Database System' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[DB] Correlation subquery 의 μ‹€ν–‰ μˆœμ„œ  (0) 2023.03.30
[#07] Normalization  (0) 2022.07.18
[#06] Database Design Using the E-R Model  (0) 2022.07.18
[#01] Introduction  (0) 2022.07.06
Comments