Chap.8 테이블과 뷰[뷰] - 뷰(View), 뷰 만들기, 뷰 삭제
뷰
뷰(View), 뷰 만들기, 뷰 삭제
현재 tableDB.userTBL
현재 tableDB.buyTBL
뷰(View)
-
테이블과 동일하게 사용되는 개체
-
SELECT 쿼리 결과를 하나의 테이블로 간주
- 여기에 이름을 배정하고 이를 통해 사용
[형식]
CREATE VIEW 뷰이름
AS
SELECT 쿼리
뷰 만들기
USE tableDB;
CREATE VIEW v_userTBL
AS
SELECT userid, name, addr FROM userTBL;
SELECT * FROM v_userTBL; -- 뷰를 테이블이라고 생각해도 무방
결과
SELECT U.userid, U.name, B.prodName, U.addr,
CONCAT(U.mobile1, U.mobile2) AS '연락처'
FROM userTBL U
INNER JOIN buyTBL B
ON U.userid = B.userid ;
결과
CREATE VIEW v_userbuyTBL
AS
SELECT U.userid, U.name, B.prodName, U.addr,
CONCAT(U.mobile1, U.mobile2) AS '연락처'
FROM userTBL U
INNER JOIN buyTBL B
ON U.userid = B.userid ;
CREATE VIEW v_userbuyTBL
AS
SELECT U.userid, U.name, B.prodName, U.addr,
CONCAT(U.mobile1, U.mobile2) AS '연락처'
FROM userTBL U
INNER JOIN buyTBL B
ON U.userid = B.userid ;
SELECT * FROM v_userbuyTBL WHERE name = N'김범수';
결과
ALTER VIEW v_userbuyTBL
AS
SELECT U.userid AS '사용자 아이디', U.name AS '이름',
B.prodName AS '제품 이름', U.addr,
CONCAT(U.mobile1, U.mobile2) AS '전화 번호'
FROM userTBL U
INNER JOIN buyTBL B
ON U.userid = B.userid ;
SELECT `이름`,`전화 번호` FROM v_userbuyTBL;
결과
뷰 삭제
[형식]
DROP VIEW 뷰이름
DROP VIEW v_userbuyTBL;
댓글