視圖可以簡單的認為是一個查詢的別名,它有限制數據訪問和簡化查詢的作用。
下面對比一下建立視圖的語句:
SQL Server建立視圖:
- CREATE VIEW empv20
- AS SELECT *
- FROM emp
- WHERE deptno = 20 ;
修改視圖:
- alter VIEW empv20
- AS SELECT ename
- FROM emp
- WHERE deptno = 20 ;
Oracle和DB2上建立的語句完全相同:
- CREATE OR REPLACE VIEW empvu20
- AS SELECT *
- FROM emp
- WHERE deptno = 20
還可以加上WITH CHECK OPTION選項來保護視圖的完整性:
- CREATE OR REPLACE VIEW empvu20
- AS SELECT *
- FROM emp
- WHERE deptno = 20
- WITH CHECK OPTION CONSTRAINT empvu20_ck;
- SQL> UPDATE empvu20
- SET deptno = 10
- WHERE empno = 7788;
- update empvu20
- *
- ERROR at line 1:
- ORA-01402: vIEw WITH CHECK OPTION where-clause violation