uTools-Manuals/docs/sql/VIEWS.html
2019-04-21 11:50:48 +08:00

5 lines
3.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="m-bg">
<h1>SQL 视图Views </h1>
<p class="intro">视图是可视化的表。</p> <p class="intro">本章讲解如何创建、更新和删除视图。</p> <h2>SQL CREATE VIEW 语句</h2> <p>在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表。</p> <p>视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。</p> <p>您可以向视图添加 SQL 函数、WHERE 以及 JOIN 语句,也可以呈现数据,就像这些数据来自于某个单一的表一样。</p> <h3>SQL CREATE VIEW 语法</h3> <div class="code notranslate"><pre><div> CREATE VIEW view_name AS<br/> SELECT column_name(s)<br/> FROM table_name<br/> WHERE condition</div></pre></div> <p><b>注释:</b>视图总是显示最新的数据!每当用户查询视图时,数据库引擎通过使用视图的 SQL 语句重建数据。</p> <h2>SQL CREATE VIEW 实例</h2> <p>样本数据库 Northwind 拥有一些被默认安装的视图。</p> <p>视图 "Current Product List" 会从 "Products" 表列出所有正在使用的产品(未停产的产品)。这个视图使用下面的 SQL 创建:</p> <div class="code notranslate"><pre><div> CREATE VIEW [Current Product List] AS<br/> SELECT ProductID,ProductName<br/> FROM Products<br/> WHERE Discontinued=No</div></pre></div> <p>我们可以像这样查询上面这个视图:</p> <div class="code notranslate"><pre><div> SELECT * FROM [Current Product List]</div></pre></div> <p>Northwind 样本数据库的另一个视图会选取 "Products" 表中所有单位价格高于平均单位价格的产品:</p> <div class="code notranslate"><pre><div> CREATE VIEW [Products Above Average Price] AS<br/> SELECT ProductName,UnitPrice<br/> FROM Products<br/> WHERE UnitPrice&gt;(SELECT AVG(UnitPrice) FROM Products)</div></pre></div> <p>我们可以像这样查询上面这个视图:</p> <div class="code notranslate"><pre><div> SELECT * FROM [Products Above Average Price]</div></pre></div> <p>Northwind 样本数据库的另一个视图会计算在 1997 年每个种类的销售总数。请注意,这个视图会从另一个名为 "Product Sales for 1997" 的视图那里选取数据:</p> <div class="code notranslate"><pre><div> CREATE VIEW [Category Sales For 1997] AS<br/> SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales<br/> FROM [Product Sales for 1997]<br/> GROUP BY CategoryName</div></pre></div> <p>我们可以像这样查询上面这个视图:</p> <div class="code notranslate"><pre><div> SELECT * FROM [Category Sales For 1997]</div></pre></div> <p>我们也可以向查询添加条件。现在,我们仅仅需要查看 "Beverages" 类的销售总数:</p> <div class="code notranslate"><pre><div> SELECT * FROM [Category Sales For 1997]<br/> WHERE CategoryName='Beverages'</div></pre></div> <h2>SQL 更新视图</h2> <p>您可以使用下面的语法来更新视图:</p> <h3>SQL CREATE OR REPLACE VIEW 语法</h3> <div class="code notranslate"><pre><div> CREATE OR REPLACE VIEW view_name AS<br/> SELECT column_name(s)<br/> FROM table_name<br/> WHERE condition</div></pre></div> <p>现在,我们希望向 "Current Product List" 视图添加 "Category" 列。我们将通过下列 SQL 更新视图:</p> <div class="code notranslate"><pre><div> CREATE VIEW [Current Product List] AS<br/> SELECT ProductID,ProductName,Category<br/> FROM Products<br/> WHERE Discontinued=No</div></pre></div> <h2>SQL 撤销视图</h2> <p>您可以通过 DROP VIEW 命令来删除视图。</p> <h3>SQL DROP VIEW 语法</h3> <div class="code notranslate"><pre><div> DROP VIEW view_name</div></pre></div> <br/><div class="text-center padding-10 margin-t-5">
</div>
</div>