๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ—‚.๋Œ€์™ธํ™œ๋™/๐Ÿ“.์นด์นด์˜คํด๋ผ์šฐ๋“œ์Šค์ฟจ

[DB] ๋ทฐ VIEW

by ๐Ÿ’พ๊ณ ๊ตฌ๋งˆ๋ง›ํƒ•๋จน๊ณ ์‹ถ๋‹ค 2024. 1. 3.
728x90

๐Ÿ–ผ ๋ทฐ( View )

ํ…Œ์ด๋ธ”์„ ํ•ฉ์ณ์„œ ๋งŒ๋“  ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”์ด๋‹ค.

 

ํŽธ๋ฆฌ์„ฑ, ์žฌ์‚ฌ์šฉ์„ฑ: ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ณต์žกํ•œ ์งˆ์˜๋ฅผ ๋ทฐ๋กœ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ณด์•ˆ์„ฑ: ์ค‘์š”ํ•œ ์งˆ์˜(ex. ๊ฐœ์ธ์ •๋ณด)์˜ ๊ฒฝ์šฐ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜, ์‚ฌ์šฉ์ž๋ณ„๋กœ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์„ ๋ณ„ํ•ด ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

๋…๋ฆฝ์„ฑ: ์›๋ณธ ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๊ฐ€ ๋ณ€ํ•ด๋„ ์‘์šฉํ•ด ์˜ํ–ฅ์ฃผ์ง€ ์•Š๋„๋กํ•˜๋Š” ๋…ผ๋ฆฌ์  ๋…๋ฆฝ์„ฑ์ด ์ œ๊ณต๋œ๋‹ค.

 

๐Ÿ–ผ ๋ทฐ์˜ ํŠน์ง•

  1. ์›๋ณธ ๋ฐ์ดํ„ฐ ๊ฐ’์— ๋”ฐ๋ผ ๊ฐ™์ด ๋ณ€ํ•œ๋‹ค.(์—ฐ๊ฒฐ)
  2. ๋…๋ฆฝ์ ์ธ ์ธ๋ฑ์Šค ์ƒ์„ฑ ์–ด๋ ต๋‹ค.
  3. ์‚ฝ์ž…, ์‚ญ์ œ, ๊ฐฑ์‹  ์—ฐ์‚ฐ์— ๋งŽ์€ ์ œ์•ฝ์ด ์กด์žฌํ•œ๋‹ค.
SELECT *
FROM BOOK
WHERE BOOKNAME LIKE '%์ถ•๊ตฌ%';

-- ๋ทฐ ์ƒ์„ฑ
CREATE VIEW VW_BOOK
AS SELECT *
FROM BOOK
WHERE BOOKNAME LIKE '%์ถ•๊ตฌ%';

-- ๋ทฐ ํ…Œ์ด๋ธ” ํ™•์ธ
SELECT *
FROM VW_BOOK;

-- ๋ทฐ ํ…Œ์ด๋ธ” ์‚ญ์ œ
DROP VIEW VW_ORDER;

-- ์—†์œผ๋ฉด ์ƒ์„ฑ, ์žˆ์œผ๋ฉด ์—…๋ฐ์ดํŠธ(์ˆ˜์ •)
CREATE OR REPLACE VIEW VW_ORDER(ORDERID, CUSTID, NAME, BOOKID, BOOKNAME, SALEPRICE, ORDERDATE)
AS SELECT OD.ORDERID, OD.CUSTID, CS.NAME, OD.BOOKID, BK.BOOKNAME, OD.SALEPRICE, OD.ORDERDATE
FROM ORDERS OD, CUSTOMER CS, BOOK BK
WHERE OD.CUSTID = CS.CUSTID AND OD.BOOKID = BK.BOOKID
ORDER BY OD.ORDERID;

-- ์‚ฌ์šฉ ์˜ˆ์‹œ
SELECT ORDERID, BOOKNAME, SALEPRICE
FROM VW_ORDER
WHERE NAME = '๊น€์—ฐ์•„';

 

728x90