ex20_view
๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ฒด
1. ํ
์ด๋ธ
2. ๊ณ์ (hr)
3. ์ ์ฝ์ฌํญ
4. ์ํ์ค
5. ๋ทฐ
View, ๋ทฐ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ฒด ์ค ํ๋
- ๊ฐ์ ํ
์ด๋ธ, ๋ทฐ ํ
์ด๋ธ ๋ฑ..
- ํ
์ด๋ธ์ฒ๋ผ ์ฌ์ฉํ๋ค.(*****)
- ๋ทฐ๋ SQL์ ์ ์ฅํ ๊ฐ์ฒด์ด๋ค.
- ๋ทฐ๋ ํธ์ถ๋ ๋๋ง๋ค ์ ์ฅ๋ SQL์ ์คํํ๋ค.(์ค์๊ฐ ๊ฐ์ ํ
์ด๋ธ) > ์๋ณธํ
์ด๋ธ ์์ ์ view ํ
์ด๋ธ๋ ์์ ๋จ
View ์ญํ (๋ชฉ์ )
1. ์ฟผ๋ฆฌ๋ฅผ ๋จ์ํํ๋ค. > ๊ฐ๋
์ฑ ํฅ์
2. ๋ณด์ ๊ด๋ฆฌ
3. ์ฟผ๋ฆฌ > ๋ค๋ฅธ ์ฌ์ฉ์(hr ๋ฑ)๊ณผ ๊ณต์
create view ๋ทฐ๋ช
as
select ๋ฌธ;
create [or replace] view ๋ทฐ๋ช
as
select๋ฌธ;
create or replace view vwInsa -- tblInsa ํ
์ด๋ธ์ ๋ณต์ฌ๋ณธ
as
select * from tblInsa;
select * from vwInsa; --tblInsa ์ฒ๋ผ ํ๋
--replace ๋๋ฌธ์ ๋๊ฐ์ ์ด๋ฆ์ ์ค๋ธ์ ํธ ์์ผ๋ฉด ์์ ํด์ค > ๋๊ฐ์ ๊ฐ์ฒด ์์ฑํด๋ ์ค๋ฅ ์ ๋จ
create or replace view vwInsa
as
select name, jikwi, city, buseo from tblInsa where buseo = '์์
๋ถ';
select * from vwInsa; -- ๋ทฐ == ์์
๋ถ ํ
์ด๋ธ
-- ์์
๋ถ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ(์ปฌ๋ผ 4๊ฐ)
select name, jikwi, city, buseo from tblInsa where buseo = '์์
๋ถ';
select * from vwInsa;
-- view ๋ฐ๋์ ์จ์ผํ๋ ๊ทน๋จ์ ์ธ ์ํฉ
-- ๋น๋์ค ๋์ฌ์ ์ฌ์ฅ > ๋ฐ๋ณต ์
๋ฌด > view๋ก ํ๋ฉด ๊ธด์ฟผ๋ฆฌ ๋ฐ๋ณต ๊ฐ๋จํด์ง
create or replace view ๋์ฌ์ฒดํฌ
as
-- ์ฐ์ฒด์ผ์ : ํ์ฌ๋ ์ง - (๋์ฌ๋ ์ง + ๋์ฌ๊ธฐ๊ฐ)
select
m.name as mname,
v.name as vname,
to_char(r.rentdate, 'yyyy-mm-dd') as rentdate,
case
when r.retdate is not null then '๋ฐ๋ฉ์๋ฃ'
else '๋ฏธ๋ฐ๋ฉ'
end as state,
case
when r.retdate is null
then round(sysdate - (r.rentdate + (select period from tblGenre where seq = v.genre)))
end as ์ฐ์ฒด์ผ์,
case
when r.retdate is null
then round((sysdate - (r.rentdate + (select period from tblGenre where seq = v.genre))))
* g.price * 0.1
end as ์ฐ์ฒด๊ธ -- ๋์ฌ๊ฐ๊ฒฉ(10%) x ์ฐ์ฒด์ผ
-- r.rentdate as "๋์ฌ๋ ์ง",
-- (select period from tblGenre where seq = v.genre) as "๋์ฌ๊ธฐ๊ฐ(์ผ)",
-- r.rentdate + (select period from tblGenre where seq = v.genre) as "๋ฐ๋ฉ๋ ์ง",
-- sysdate - (r.rentdate + (select period from tblGenre where seq = v.genre)) as "์ฐ์ฒด๊ธฐ๊ฐ(์ผ)"
from tblRent r
inner join tblVideo v
on v.seq = r.video
inner join tblMember m
on m.seq = r.member
inner join tblGenre g
on g.seq = v.genre
order by state asc;
select * from ๋์ฌ์ฒดํฌ;
-- ๋ทฐ ์ ์ > select ๊ฒฐ๊ณผ์
์ ๋ณต์ฌ๋ณธ..? > ๋ฐ์ดํฐ ์ ์ฅํ ๊ฐ์ฒด(X)
-- ๋ทฐ ์ ์ > select๋ฌธ์ ์ ์ฅํ ๊ฐ์ฒด > SQL ์ ์ฅํ ๊ฐ์ฒด
create or replace view vwComedian
as
select * from tblComedian;
select * from tblComedian; --์๋ณธ ํ
์ด๋ธ
select * from vwComedian; --๋ณต์ฌ ํ
์ด๋ธ
update tblComedian set
weight = 70 where first = '์ฌ์'; --๋ณต์ฌ ํ
์ด๋ธ์๋ ๊ฒฐ๊ณผ ๋ฐ๋
select * from vwComedian; -- ์ฌ์ฌ์ฉ ๋ชฉ์ (์๋๋ ๋๊ฐ์)
select * from (select * from tblComedian); -- from ์๋ธ์ฟผ๋ฆฌ == ์ธ๋ผ์ธ ๋ทฐ > 1ํ์ฉ
-- ๋ณด์(๊ถํ)
select * from tblInsa; --์ ์ง์ + ๋ชจ๋ ์ ๋ณด
-- ์ ์
์ฌ์(hr) > ์์
๋ถ > ์
๋ฌด > ์์
๋ถ ์ง์๋ค์๊ฒ ์ผ๊ด ๋ฌธ์ ๋ฉ์์ง ์ ์ก
select * from tblInsa; --์ ์
์ฌ์์๊ฒ tblInsa ์ ๊ทผํ ๊ถํ > tblInsa ์ ๊ทผ ์ ํ
create or replace view ์ฐ๋ฝ์ฒ
as
select name, tel from tblInsa where buseo = '์์
๋ถ';
select * from ์ฐ๋ฝ์ฒ; --์ ์
์ฌ์์๊ฒ ์ฐ๋ฝ์ฒ ๊ฐ์ฒด์ ๋ํ ์ ๊ทผ ๊ถํ๋ง ๋ถ์ฌ
create or replace view vwTodo
as
select * from tblTodo;
-- ๋ทฐ ์ฌ์ฉ
-- 1. select > ๊ฐ๋ฅ > ์ ๋ ์ฌ์ฉ๊ธ์ง > ๋ทฐ๋ ์ฝ๊ธฐ ์ ์ฉ์ด๋ค(***) ์ฝ๊ธฐ ์ ์ฉ ํ
์ด๋ธ์ด๋ผ๊ณ ์๊ฐํ ๊ฒ
-- 2. insert > ๊ฐ๋ฅ > ์ ๋ ์ฌ์ฉ๊ธ์ง
-- 3. update > ๊ฐ๋ฅ > ์ ๋ ์ฌ์ฉ๊ธ์ง
-- 4. delete > ๊ฐ๋ฅ > ์ ๋ ์ฌ์ฉ๊ธ์ง
select * from vwTodo; --๋จ์๋ทฐ > ๋ทฐ์ select๋ฌธ์ด 1๊ฐ์ ํ
์ด๋ธ๋ก ๊ตฌ์ฑ
insert into vwTodo values (21, '์ค๋ผํด ๋ณต์ตํ๊ธฐ', sysdate, null);
update vwTodo set title = '์ค๋ผํด ์ ๋ฆฌํ๊ธฐ' where seq = 21;
delete from vwTodo where seq = 21;
select * from ๋์ฌ์ฒดํฌ; --๋ณตํฉ๋ทฐ > 2๊ฐ์ ์ด์ ํ
์ด๋ธ์ ์ฌ์ฉํด์ select
insert into ๋์ฌ์ฒดํฌ values ('ํ๊ธธ๋', '๋ฐ์ง์ ์ ์', sysdate, '๋ฏธ๋ฐ๋ฉ', 0, 0); --์๋ฌ. ๊ฐ๊ฐ ๋ค๋ฅธํ
์ด๋ธ.
--๋จ์๋ทฐ, ๋ณตํฉ๋ทฐ ์ฌ์ฉ์๋ ๊ตฌ๋ถ ๋ชปํจ.. select๋ง ๋๋ค๊ณ ๊ฐ์ ํ๊ณ ์ฌ์ฉํ ๊ฒ.
'ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ > Oracle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ex22] alter (0) | 2023.03.26 |
---|---|
[ex21] union (0) | 2023.03.26 |
[ex19] join (0) | 2023.03.25 |
[ex18] subquery (0) | 2023.03.21 |
[ex17] group by (0) | 2023.03.19 |