当前位置:首页 > PHP教程 > PHP总结归纳

orale数据库如何利用sql语句创建视图的一般性使用方法

sql create view 语句 什么是视图? 在 sql 中,视图是基于 sql 语句的结果集的可视化的表。 视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库教程中的真实的表中的字段。我们可以向视图添加 sql 函数、where 以及 join 语句,我们

sql create view 语句
什么是视图?
在 sql 中,视图是基于 sql 语句的结果集的可视化的表。

视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库教程中的真实的表中的字段。我们可以向视图添加 sql 函数、where 以及 join 语句,,我们也可以提交数据,就像这些来自于某个单一的表。

注释:数据库的设计和结构不会受到视图中的函数、where 或 join 语句的影响。

sql create view 语法
create view view_name as
select column_name(s)
from table_name
where condition

注释:视图总是显示最近的数据。每当用户查询视图时,数据库引擎通过使用 sql 语句来重建数据。
sql create view 实例
可以从某个查询内部、某个存储过程内部,或者从另一个视图内部来使用视图。通过向视图添加函数、join 等等,我们可以向用户精确地提交我们希望提交的数据。

样本数据库 northwind 拥有一些被默认安装的视图。视图 "current product list" 会从 products 表列出所有正在使用的产品。这个视图使用下列 sql 创建:

create view [current product list] as
select productid,productname
from products
where discontinued=no我们可以查询上面这个视图:


select * from [current product list]northwind 样本数据库的另一个视图会选取 products 表中所有单位价格高于平均单位价格的产品:

create view [products above average price] as
select productname,unitprice
from products
where unitprice>(select avg(unitprice) from products)

我们可以像这样查询上面这个视图:

select * from [products above average price]另一个来自 northwind 数据库的视图实例会计算在 1997 年每个种类的销售总数。请注意,这个视图会从另一个名为 "product sales for 1997" 的视图那里选取数据:

create view [category sales for 1997] as
select distinct categoryname,sum(productsales) as categorysales
from [product sales for 1997]
group by categoryname

我们可以像这样查询上面这个视图:

select * from [category sales for 1997]我们也可以向查询添加条件。现在,我们仅仅需要查看 "beverages" 类的全部销量:

select * from [category sales for 1997]
where categoryname='beverages'

sql 更新视图
您可以使用下面的语法来更新视图:

sql create or replace view syntax
create or replace view view_name as
select column_name(s)
from table_name
where condition

现在,我们希望向 "current product list" 视图添加 "category" 列。我们将通过下列 sql 更新视图:

create view [current product list] as
select productid,productname,category
from products
where discontinued=no

sql 撤销视图
您可以通过 drop view 命令来删除视图。

sql drop view syntax
drop view view_name
or replace view ``

下面为老外网站上的教程

your_view_name>`

as

...其次是正常的sql的select。这个select可以包含一个where子句或其他需要,可以对select语句放在别的事情。该方案是无止境的。这实际上取决于视图的目的。

正如你可以看到在我们看来,我们正在格式化的姓氏和名字。这是一个很常见的事做有一个观点,即我们已经这样做了保存有写在每一个查询的where这是一个要求的功能。你也可以看到,我们已经采取的出生日期列和计算年龄。

执行视图
执行一个sql视图
下面的例子显示所有从视图代码。你也可以做一个select*,或进一步限制列你想看到的。您还可以添加额外的行限制的看法,因为我们的做法。

select firstname ,
lastname ,
birth_dttm ,
fullname_fl ,
age
from vw_students1
where age is not null

/

creating a view containing one or more sql tables
another key advantage of a view is that it allows us to join multiple tables together.

create or replace view vw_occupied_seats_by_class
as
select
c.course_designater_fk as "course",
b.seat_num ,
(a.firstname || ' ' || a.lastname) as "student"

from students a
join classregistration b
on a.student_id = b.student_id_fk

join classes c


on c.classes_num = b.classes_num


/



above is a simple view that provides us with a listing of occupied/unoccupied seats for our classes. as you can see from the examples below, we can use this view in a variety of different ways. note that for each scenario that we did not need to join any tables. the grunt work is already done.

using our view
view a single class

.syntaxhighlighter{padding-top:20px;padding-bottom:20px;}

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!