๐ก View
๋ฅผ ์์ฌ์ฉํ ๊น?
๋ค๋ฅธ ๋ถ์์์ ๋ง์ด ์ฌ์ฉํ๋ ์ ์ฉํ ๊ธด ์ฟผ๋ฆฌ๊ฐ ์๋๋ฐ,์ด ๋ณต์กํ ์ฟผ๋ฆฌ ์์ฒด๋ฅผ
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ๋์ '๋ฐ๋ก๊ฐ๊ธฐ' ์ฒ๋ผ ์ ์ฅํด๋๊ณ , ํ์ํ ๋๋ง๋ค ๊ฐ๋จํ ์ด๋ฆ์ผ๋ก ํธ์ถํ๋ ๋ฐฉ๋ฒ
์ ์ด๋ ๊ฒ ์ฌ์ฉํ ๊น?
๊ถํ์ ์ด
๋ฅผ ๊ฐ๋ฅํ๊ฒ ํจโ View
๊ฐ์์ ํ
์ด๋ธ
์ด๋คSELECT
์ฟผ๋ฆฌ๋ฌธ์ฆ View
๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ํ
์ด๋ธ์ด ์๋๋ฉฐ, ๋จ์ง ๋ณต์กํ SELECT
์ฟผ๋ฆฌ๋ฌธ ์์ฒด๋ฅผ ์ ์ฅํ๊ณ ์๋ค
๊ทธ๋ฌ๋ฉด ๋ณต์กํ ์ฟผ๋ฆฌ ์คํ ์์ด
SELECT * FROM ๋ฐ๋ก๊ฐ๊ธฐ_๋ทฐ;
๋ผ๋ ๊ฐ๋จํ ๋ช
๋ น์ด๋ก ๋ณต์กํ ์ฟผ๋ฆฌ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค
์ฌ์ฉ์๋ ๋ง์น ๊ทธ๋ฅ ํ
์ด๋ธ์ ์กฐํํ ๊ฒ์ฒ๋ผ ๋๊ปด์ง๋ค
โ
VIEW ์์ฑ ๋ฐฉ๋ฒ
CREATE VIEW ๋ทฐ์ด๋ฆ AS SELECT ์ฟผ๋ฆฌ๋ฌธ;
// ex)
create view v_category_count as
select u.name, count(o.order_id) as 'total_orders',
count(case when category = '์ ์๊ธฐ๊ธฐ' then 1 end) as 'elcetronic_orders',
count(case when category = '๋์' then 1 end) as 'book_orders',
count(case when category = 'ํจ์
' then 1 end) as 'fasion_orders'
from users u
left join orders o
on u.user_id = o.user_id
left join products p
on o.product_id = p.product_id
group by u.name;
์ด ์ฟผ๋ฆฌ๋ฅผ ์คํํ๋ฉด v_category_count
๋ผ๋ view ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค
โ
VIEW ์กฐํ ๋ฐฉ๋ฒ
์์์ ๋ง๋ view ๊ฐ์ฒด๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค
SELECT * FROM v_category_count;
โ
VIEW ์์ ๋ฐฉ๋ฒ
ALTER VIEW
๋ฅผ ์ฌ์ฉํ๊ณ ๋ค์ SELECT
๋ฌธ๋ง ๋ฐ๊ฟ์ฃผ๋ฉด ๋๋ค
๐ก View ์ด๋ฆ์ ๊ทธ๋๋ก ๋ฌ์ผํจ
alter view v_category_count as // alert view ์ด๋ฆ์ ๊ทธ๋๋ก
select u.name, count(o.order_id) as 'total_orders',
count(case when category = '์ ์๊ธฐ๊ธฐ' then 1 end) as 'elcetronic_orders',
count(case when category = '๋์' then 1 end) as 'book_orders',
count(case when category = 'ํจ์
' then 1 end) as 'fasion_orders',
1 as 'alert_view' // ํ๋์ ์ปฌ๋ผ ์ถ๊ฐ
from users u
left join orders o
on u.user_id = o.user_id
left join products p
on o.product_id = p.product_id
group by u.name;
โ
VIEW ์ญ์ ๋ฐฉ๋ฒ
DROP VIEW
๋ก ์ญ์ ๊ฐ ๊ฐ๋ฅ
DROP VIEW v_category_count;
์ค๋ฌด์์๋ VIEW๋ฅผ ์ฌ์ฉํ๋ค๋ณด๋ฉด ์ฅ๋จ์ ์ด ์๋ค
์ฅ์ ์ VIEW๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ ๋ํด์ ์๊ฒ์ด๊ณ
๋จ์ ์ VIEW ์์ ์ฟผ๋ฆฌ๊ฐ ์ค์ ๋ก ํฌ๋ฉด ๋ง์ ์ฑ๋ฅ์ด ์์์๋ ์์ผ๋ฉฐ
๋ทฐ๋ฅผ ์ค์ฒฉ์์ ์ฌ์ฉํ๋ฉด, ์ฑ๋ฅ์ ํ์ ์์ธ์ด ๋ ์ ์๋ค
VIEW๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์กฐํ์ฉ
์๊ฐํ๋๊ฒ์ด ์ผ๋ฐ์ ์ธ ์์น์ด๋ค
VIEW์๋ ๋ฐ์ดํฐ๋ฅผ ์์ ์ฝ์
ํ ์ ์์ง๋ง
JOIN
์ง๊ณํจ์
GROUP BY
DISTINCT
๋ฑ ์ ์ฌ์ฉํ ๋ณต์กํ ๋ทฐ๋ ์ผ๋ฐ์ ์ผ๋ก
INSERT, DELETE, UPDATE ๊ฐ ๋ถ๊ฐํ๋ค
๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ์ผ์ด ํ์ํ๋ค๋ฉด, ๋ทฐ๊ฐ ์๋ ์๋ณธํ ์ด๋ธ์์ ์ํํด์ผ ํ๋ค