Node.js
[07.14] MYSQL - View, Subquery 사용
ljw4104
2021. 7. 14. 11:22
1. View
- 뷰는 SQL시점에서 보면 테이블과 동일하지만 테이블과 같은 데이터는 가지고 있지 않다.
- 테이블에 대한 SELECT를 가지고 있다.
- 테이블 대신 뷰를 사용하는 이점은 다음과 같다.
- 복잡한 SELECT 문을 일일이 매번 기술할 필요가 없다.
- 필요한 열과 행만 사용자에게 보여줄수 있고, 갱신 시에도 뷰 정의에 따른 갱신으로 한정할 수 있다.
- 데이터 저장 없이 실현할 수 있다.
- 뷰를 제거 해도 참조하는 테이블은 영향을 받지 않는다.
#create view
create view view_products as select name, price, amount from products;
#select view
select * from view_products;
2. SubQuery
delimiter //
create procedure select_products(target_price int)
begin
declare cnt int;
#subquery
select count(*) into cnt
from products where price > target_price;
#분기
IF cnt > 0 then
select name, price, amount from products
where price > target_price;
END IF;
end//
delimiter ;
call select_products(1000);