SQL|view"只可远观不可亵玩也"

"只可远观不可亵玩也" 在数据库的世界里你只能看不能动。如果实现?那就是视图(Views)

如何创建、更新和删除视图。

创建视图

视图是基于 SQL 语句的结果集的可视化的表。
视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。
您可以向视图添加 SQL 函数、WHERE 以及 JOIN 语句,也可以呈现数据,就像这些数据来自于某个单一的表一样。

注意视图可以连接多个库及库中的多个表及表中的多个字段。

语法

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

样例

create view v_bom_zy as
select distinct s.ID,
s.DATAID,
(s.D_itemCode || '') as CODE_V,
(s.D_objectName || '') as NAME_V,
'' as SPECIFICATION_V,
'' as MODEL_V,
'' as DEPT_V,
'' as UNIT_V,
(s.D_itemCode || '') as UNIQUEKEY_V,
'F30D29D602BB4898ABE2F2FB0C894955' as MODELID_V
from ZZJ_TABLE248 s
where s.VERSIONSTATUS = '1'
union all
select distinct s.ID,
s.DATAID,
(s.D_itemCode || '') as CODE_V,
(s.D_objectName || '') as NAME_V,
'' as SPECIFICATION_V,
'' as MODEL_V,
'' as DEPT_V,
'' as UNIT_V,
(s.D_itemCode || '') as UNIQUEKEY_V,
'6DF02A65B4914E2B9692F27A40D13A3F' as MODELID_V
from ZZJ_TABLE01248 s
where s.VERSIONSTATUS = '1';

更新视图 create or replace view

语法:

create or repalce view 视图名称 as select 列名 from 表名 where 条件

create or replace view v_bom_zy as
select distinct s.ID,
s.DATAID,
(s.D_itemCode || '') as CODE_V,
(s.D_objectName || '') as NAME_V,
'' as SPECIFICATION_V,
'' as MODEL_V,
'' as DEPT_V,
'' as UNIT_V,
(s.D_itemCode || '') as UNIQUEKEY_V,
'F30D29D602BB4898ABE2F2FB0C894955' as MODELID_V
from ZZJ_TABLE248 s
where s.VERSIONSTATUS = '1'
union all
select distinct s.ID,
s.DATAID,
(s.D_itemCode || '') as CODE_V,
(s.D_objectName || '') as NAME_V,
'' as SPECIFICATION_V,
'' as MODEL_V,
'' as DEPT_V,
'' as UNIT_V,
(s.D_itemCode || '') as UNIQUEKEY_V,
'6DF02A65B4914E2B9692F27A40D13A3F' as MODELID_V
from ZZJ_TABLE01248 s
where s.VERSIONSTATUS = '1';

删除视图

语法

drop view 视图名称

样例

drop view v_bom_zy

你可能感兴趣的:(SQL|view"只可远观不可亵玩也")