Article Directory
1 Overview
-- 查询所有 view
SELECT * FROM dba_views t; -- all_views, user_views
-- 删除视图
DROP VIEW 视图名;
2 Grammar
CREATE [OR REPLACE] [FORCE] VIEW [属主.]<view_name>
AS (
SELECT 语句
)
[WITH CHECK OPTION]
[WITH READ ONLY];
Keyword explanation:
1. or replace: 存在就替换原视图
2. force: 强制执行,即使报错。常用来处理执行顺序导致的问题
3. with check option: 插入或修改的数据行必须满足视图定义的约束
4. with read only: 该执行上不能进行任何 dml 操作
Basic data:
CREATE TABLE stu_info (
id number(3) not null,
name varchar2(30) not null,
sex varchar2(2) not null
);
INSERT INTO stu_info (id, NAME, sex) VALUES (1, '瑶瑶', '女');
3 example
3.1 force force to create a view
The actual application scenarios: when the DB deployment, the order is:
DDL 语句 -> DML 语句 -> PKG
,
if DDL statement called pkg view, it will produce an error when deploying (because the pkg has not been deployed), but this error will disappear after pkg deployment, this time Force is used.
Create view statement:
CREATE OR REPLACE VIEW vw_stu_info AS (
SELECT si.id,
si.name,
si.sex,
f_get_class_no(si.id) class_no -- pkg 中,还未被执行(简单演示,就没写到 pkg 里面,但效果是一样的)
FROM stu_info SI
);
/
Execution result: After
adding force, the execution is successful
CREATE OR REPLACE FORCE VIEW vw_stu_info AS (
SELECT si.id,
si.name,
si.sex,
f_get_class_no(si.id) class_no -- 假如有这个方法
FROM stu_info SI
);
/
- Tips: The view at this time is invalid, it is valid only after the f_get_class_no function is executed.
SELECT t.status, t.* FROM all_objects t WHERE t.OBJECT_NAME = UPPER('vw_stu_info');
-- INVALID: 无效
Deploy the methods in pkg in order: f_get_class_no (in actual development, the function is placed in pkg, here is only used as a test)
CREATE OR REPLACE FUNCTION f_get_class_no(v_id stu_info.id%TYPE)
RETURN VARCHAR2 AS
v_class_no VARCHAR2(10);
BEGIN
IF v_id < 5 THEN
v_class_no := 'A001';
ELSIF v_id >= 5 THEN
v_class_no := 'A002';
ELSE
v_class_no := 'A000';
END IF;
RETURN v_class_no;
END;
/
Query view:
SELECT * FROM vw_stu_info;
search result:
3.2 dml operation
Under normal circumstances, we will only perform query operations on views, but we must understand that views can also perform insert, update, and delete, but there are many restrictions.
- Compliance: "simple view" -> the view contains only one base table, no other constraint checks
Create view statement:
CREATE OR REPLACE VIEW vw_stu_info AS
SELECT si.id,
si.name,
si.sex
FROM stu_info SI;
/
Test statement: ( 可以理解为向 基表 中插入数据的情况
)
-- TRUNCATE TABLE stu_info; 清除历史数据,如果有
INSERT INTO vw_stu_info (id, NAME, sex) VALUES (1, '瑶瑶', '女');
SELECT * FROM vw_stu_info;
Test Results:
Personal understanding:
- Basis for
向视图中插入数据情况,可以转化为向基表中插入数据的情况,然后再考虑 dml 操作的可行性
judgment: . - Under normal circumstances, we will only query the view and do not do the dml operation, so just understand it here.
- There are roughly the following situations (maybe you have seen other situations mentioned by others, but everything should be based on actual cases of your own hands!)
Non-conformity | understanding |
---|---|
Multiple base table connection | Provisions (extension: dml operation can be performed by instead of trigger) |
Virtual column exists | Not in the base table, calculated by expressions, rowid, rownum, etc. |
There are not null columns that are not in the view | It is well understood, if not, it violates the non-empty constraint of the base table |
3.3 with check option
- Check options: When performing dml operations in the view, it must meet the restrictions of where
Personal understanding:
1. 对于 insert,有 with check option 时,要保证 insert 后,数据能被视图查询出来(符合 where 条件)
2. 对于 update,有 with check option 时,要保证 update 后,数据能被视图查询出来(符合 where 条件)
3. 对于 delete,有无 with check option 都一样
4. 对于没有 where 子句的视图,使用 with check option 是多余的
Basic data:
TRUNCATE TABLE stu_info; -- 清除历史数据
INSERT INTO stu_info (id, NAME, sex) VALUES (1, '瑶瑶', '女');
INSERT INTO stu_info (id, NAME, sex) VALUES (2, '倩倩', '女');
INSERT INTO stu_info (id, NAME, sex) VALUES (3, '优优', '男');
COMMIT;
View code:
CREATE OR REPLACE VIEW vw_stu_info AS (
SELECT si.id,
si.name,
si.sex
FROM stu_info SI
WHERE si.id <= 5
) WITH CHECK OPTION;
/
Test statement:
-- id = 6 > 5, 不符合视图中的 where 条件
INSERT INTO vw_stu_info (id, NAME, sex) VALUES (6, '测试66', '男');
Test Results:
3.4 with read only
- Check options: read-only view, dml operation is prohibited
Create view statement:
CREATE OR REPLACE VIEW vw_stu_info AS (
SELECT si.id,
si.name,
si.sex
FROM stu_info SI
WHERE si.id <= 5
) WITH READ ONLY;
/
Test statement:
INSERT INTO vw_stu_info (id, NAME, sex) VALUES (6, '测试66', '男');
Test Results: