๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/DB๊ธฐ์ดˆ

[DB] SQL ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด ์œ„๋ฐฐ ์˜ˆ์ œ

728x90

company DB ์—ฐ์Šต๋ฌธ์ œ๋“ค.

์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๊ต์œก์šฉ ์ž๋ฃŒ๋กœ ํ”ํ•˜๊ฒŒ ์“ฐ์ด๋Š” ๋“ฏ ํ•˜๋‹ค

๐Ÿ’ก [ ๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ : ํ‚ค๊ฐ’์ด null์ด๋ฉด ์•ˆ๋จ ]

     [ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ : FK๊ฐ€ PK์— ์—†๋Š” ๊ฐ’์ด๋ฉด ์•ˆ๋จ ]

 

๋‹ค์Œ ๋ช…๋ น์ด ์–ด๋–ค ๋ฌด๊ฒฐ์„ฑ์„ ์œ„๋ฐ˜ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜์‹œ์˜ค.

 

1)  Insert <‘ProductA’, 4, ‘Bellaire’, 2> into PROJECT.

→ DNUM=2์ด๊ณ  DNUMBER=2์™€ DEPACTION ๊ด€๊ณ„์— ํŠœํ”Œ์ด ์—†์œผ๋ฏ€๋กœ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ„๋ฐ˜.

(์ฐธ์กฐํ•  ์ˆ˜ ์—†๋Š” FK๊ฐ’ 2๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฏ€๋กœ)

์ œํ•œ ๋ฐฉ๋ฒ•

(i) ์ƒˆ ํ”„๋กœ์ ํŠธ ํŠœํ”Œ ์‚ฝ์ž… ๊ฑฐ๋ถ€

(ii) ๋ถ€์„œ ๊ด€๊ณ„์—์„œ ์‹ ๊ทœ ํ”„๋กœ์ ํŠธ ํŠœํ”Œ์˜ DNUM ๊ฐ’์„ ๊ธฐ์กด DNUMBER ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ

(iii) DNUMBER=2์˜ ์ƒˆ ๋ถ€์„œ ํŠœํ”Œ์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

 

2)  Insert <‘677678989’, NULL, ‘40.0’> into WORKS_ON.๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ ๋ฐ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์„ ๋ชจ๋‘ ์œ„๋ฐ˜.

WORKS_ON์˜ PK์˜ ์ผ๋ถ€์ธ PNO๊ฐ€ null์ด๋ฏ€๋กœ ๊ฐœ์ฒด ๋ฌด๊ฒฐ์„ฑ์„ ์œ„๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ๋ฐฉ๋ฒ•

(i) ์‚ฝ์ž… ๊ฑฐ๋ถ€

(ii) ์ƒˆ๋กœ์šด WORKS_ON tuple์˜ PNO ๊ฐ’์„ PROJECT ๊ด€๊ณ„์— ์กด์žฌํ•˜๋Š” PNUMBER ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ

ESSN='677678989'์‚ฝ์ž… ์‹œ EMPLOYEE์—๋Š” SSN='677678989' ํŠœํ”Œ์ด ์—†์œผ๋ฏ€๋กœ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ„๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ๋ฐฉ๋ฒ•

(i) ์‚ฝ์ž… ๊ฑฐ๋ถ€

(ii) ESSN์˜ ๊ฐ’์„ ์ง์›์˜ ๊ธฐ์กด SSN ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ

(iii) SSN='677678989'๋กœ ์ƒˆ ์ง์› ํŠœํ”Œ ์‚ฝ์ž…

 

3)  Insert <‘453453453’, ‘John’, ‘M’, ‘1990-12-12’, ‘spouse’> into DEPENDENT. → ์ œ์•ฝ ์กฐ๊ฑด ์œ„๋ฐ˜ ์—†์Œ

 

4)  Delete the PROJECT tuple with Pname = ‘ProductX’.

ํ”„๋กœ์ ํŠธ์—์„œ ์‚ญ์ œํ•  ํŠœํ”Œ์„ ์ฐธ์กฐํ•˜๋Š” ๋‘ ๊ฐœ์˜ ํŠœํ”Œ์ด WORKS_ON ๊ด€๊ณ„์— ์žˆ์œผ๋ฏ€๋กœ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ„๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. (PK์‚ญ์ œํ•˜๋ฉด์•ˆ๋ผ์—ฌ)

์ œํ•œ ๋ฐฉ๋ฒ•

(i) ์‚ญ์ œ ๊ฑฐ๋ถ€

(ii) PNO=1(ํ”„๋กœ์ ํŠธ์—์„œ ์‚ญ์ œ ์ค‘์ธ ํŠœํ”Œ์˜ ๊ธฐ๋ณธ ํ‚ค PNUMBER ๊ฐ’)์ธ WORKS_ON ๊ด€๊ณ„์—์„œ ํŠœํ”Œ์„ ์‚ญ์ œ.

 

5)  Modify the Super_ssn attribute of the EMPLOYEE tuple with Ssn = ‘999887777’ to ‘943775543’.

SUPSSN='943775543'์˜ ์ƒˆ ๊ฐ’๊ณผ SSN='943775543'์˜ ๊ด€๊ณ„์— ํŠœํ”Œ์ด ์—†์œผ๋ฏ€๋กœ ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ„๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. (943~๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง„ ์ง์›์ด ์—†์Œ)

์ œํ•œ ๋ฐฉ๋ฒ•

(i) ์‚ญ์ œ ๊ฑฐ๋ถ€

(ii) SSN='943775543'์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ์ง์› ํŠœํ”Œ์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

728x90