sql 视图嵌套视图
SQL | 观看次数 (SQL | Views)
Views in SQL are virtual tables. A view also has rows and columns as they're during a real table within the database. We will create a view by selecting fields from one or more tables present within the database. A View can either have all the rows of a table or specific rows supported under certain conditions. A view is nothing quite a SQL statement that's stored within the database with an associated name. A view is a composition of a table within the sort of a predefined SQL query. All rows of a table or select rows from a table can be contained inside the view. A view is often created from one or many tables which depend on the written SQL query to make a view.
SQL中的视图是虚拟表 。 视图还具有行和列,就像它们在数据库中的真实表中一样。 我们将通过从数据库中存在的一个或多个表中选择字段来创建视图。 视图可以具有表的所有行或在特定条件下受支持的特定行。 视图只不过是存储在数据库中且具有关联名称SQL语句。 视图是预定义SQL查询中的表的组成。 表的所有行或表中的选择行都可以包含在视图内部。 通常从一个或多个表创建视图,这些表取决于编写SQL查询来创建视图。
Views, which are a kind of virtual tables allow users to try to the subsequent:
视图是一种虚拟表,允许用户尝试以下操作:
Structure data during a way that users or classes of users find natural or intuitive.
在用户或用户类别觉得自然或直观的方式下构造数据。
Restrict access to the info in such a way that a user can see and (sometimes) modify exactly what they have and no more.
限制访问信息的方式,使用户可以看到和(有时)准确地修改自己拥有的内容,而不再修改。
Summarize data from various tables which may be wont to generate reports.
汇总来自各种表的数据,这些数据可能不会生成报告。
Here we will discuss creating, deleting and updating views.
在这里,我们将讨论创建,删除和更新视图。
创建视图 (Creating the view)
Views can be created in SQL by using the CREATE VIEW command. This order gives the name to the view and determines the rundown of credits and tuples to be incorporated utilizing a subquery.
可以使用CREATE VIEW命令在SQL中创建视图 。 该顺序为视图指定名称,并使用子查询确定要合并的学分和元组的精简。
The syntax to create a view is given here,
这里给出了创建视图的语法,
CREATE VIEW <view_name>
As <subquery>;
For example, the command to create a view containing details of books which belong to text-book and Language Book can be specified as:
例如,用于创建包含教科书和语言书的书的详细信息的视图的命令可以指定为:
CREATE VIEW Book_1
As SELECT *
FROM BOOK
WHERE Category IN ('Textbook','LanguageBook');
Output
输出量

This command creates the view, named Book_1, having details of books satisfying the condition specified in the WHERE clause. The view created like this consists of all the attributes of Book relation also.
此命令创建名为Book_1的视图,该视图具有满足WHERE子句中指定条件的书籍的详细信息。 这样创建的视图也包含Book关系的所有属性。
For example, consider the command given below:
例如,考虑以下命令:
CREATE VIEW Book_2(B_code,B_title,B_category,B_price)
As SELECT ISBN,Book_Tiltle,Category,Price
FROM Book
WHERE Category IN ('Textbook','LanguageBook');
Output
输出量

This command creates a view Book_2, which consists of the attributes, ispn, book-name, categori, and price from the relation Book with new names, namely, b_code, b_title, b_category, and b_price respectively. Now queries can be performed on these views as they are performed on other relations.
此命令创建一个视图Book_2 ,该视图由关系Book中的属性ispn , book-name , categori和price组成,具有新名称,分别为b_code , b_title , b_category和b_price 。 现在,可以在这些视图上执行查询,就像在其他关系上执行查询一样。
Consider the example given below:
考虑下面给出的示例:
SELECT *
FROM Book_1;
Output
输出量

SELECT *
FROM Book_2
WHERE price>300;
Output
输出量

SELECT b_title, b_category
FROM Book_2
WHERE price BETWEEN 200 and 350;
Output
输出量

Views can contain more than one relation. The views that depend on more than one relation are known as complex views. These types of views are inefficient as they are time-consuming to execute, especially if multiple queries are involved in the view definition. Since their content is not physically stored, they are executed whenever their reference is done inside the program.
视图可以包含多个关系。 依赖于多个关系的视图称为复杂视图 。 这些类型的视图效率低下,因为它们执行起来很耗时,尤其是在视图定义中涉及多个查询的情况下。 由于它们的内容不是物理存储的,因此只要在程序内部完成引用就可以执行它们。
更新视图 (Updating Views)
A view can be updated based upon several conditions as mentioned below,
可以根据以下几种条件来更新视图,
Keyword DISTINCT should not be present in the SELECT statement.
关键字DISTINCT不应出现在SELECT语句中。
The summary function should not be there in the SELECT statement.
摘要函数不应在SELECT语句中存在。
Set function and Set Operations should not be present in the SELECT statement.
设置函数和设置操作不应出现在SELECT语句中。
ORDER BY clause should not be present in the SELECT statement.
SELECT语句中不应存在ORDER BY子句。
Multiple tables should not be contained in the FROM statement.
FROM语句中不应包含多个表。
Subqueries should not be present inside the WHERE clause.
子查询不应出现在WHERE子句中。
A query written in SQL must not contain GROUP BY or HAVING.
用SQL编写的查询不得包含GROUP BY或HAVING 。
Columns that are calculated may not get updated.
计算的列可能不会更新。
If the view satisfies the above-mentioned conditions then the user or programmer is able to update the view. The code written below can be used for serving the purpose.
如果该视图满足上述条件,则用户或程序员可以更新该视图。 下面编写的代码可用于实现此目的。
UPDATE <Name of VIEW>
SET <parameter to be updated>=<value>
WHERE <condition>;
放下视图 (Dropping View)
The view needs to be dropped (i.e. Deleted permanently) when not in further use. The syntax for doing so is,
不使用该视图时,需要将其删除(即永久删除)。 这样做的语法是
DROP VIEW <view_name>;
翻译自: https://www.includehelp.com/sql/views.aspx
sql 视图嵌套视图