![sql 视图的更新和删除](https://i-blog.csdnimg.cn/blog_migrate/6e3268818d460b223518d134c8c28669.png)
sql 视图的更新和删除
SQL Programming language and SQL Database engines provide some useful mechanisms in order to make management of the data easier. SQL View
is one of them which can be used to create a table from a SQL result set.
SQL编程语言和SQL数据库引擎提供了一些有用的机制,以使数据管理更加容易。 SQL View
是其中之一,可用于从SQL结果集中创建表。
什么是SQL视图? (What Is SQL View?)
SQL View is a virtual table which does not exist in a database physically. SQL View table is created from an SQL statement which is generally a select result set. View have rows, columns just like a regular table. The fields are generally related with one or more fields from real tables in the database.
SQL View是一个虚拟表,实际上在数据库中不存在。 SQL View表是根据通常是选择结果集SQL语句创建的。 视图具有行,列,就像常规表一样。 这些字段通常与数据库中实际表中的一个或多个字段相关。
句法 (Syntax)
The syntax of the SQL View is simple and like a SELECT
statement. We will use Create View
with view name and As
statement like below. The Select
statement will bind related rows, columns and fields to the SQL View
table. We can also use conditional statements like Where
etc.
SQL View的语法很简单,就像SELECT
语句一样。 我们将使用带有视图名称和As
语句的Create View
,如下所示。 Select
语句会将相关的行,列和字段绑定到SQL View
表。 我们还可以使用条件语句,例如Where
等。
Create View VIEWNAME AS Select column1, column2, … From TABLENAME Where CONDITION;
创建View VIEWNAME AS选择Column1,Column2…从TABLENAME中选择CONDITION;
- `VIEWNAME` is the name of the view we will create VIEWNAME是我们将创建的视图的名称
- `column1`, `column2` etc are columns we will use in view from `TABLENAME`“ column1”,“ column2”等是我们将在“ TABLENAME”视图中使用的列
- `TABLENAME` is the table name we want to feed our view. Provided columns will be selected from this table. TABLENAME是我们要提供视图的表名。 将从该表中选择提供的列。
- `CONDITION` is optional with `Where` statement. CONDITION在带有where语句的情况下是可选的。
创建SQL视图 (Create SQL View)
We will start with a simple SQL View creation. We will create a view named TurkishCustomers
from the table named Customers
and use CustomerName
and Contact
columns from this Customers
table.
我们将从创建简单SQL View开始。 我们将从名为“ Customers
的表中创建名为“ TurkishCustomers
的视图,并使用此“ Customers
表中的“ CustomerName
和“ Contact
列。
Create View TurkishCustomers AS
Select Name, Contact From Customers Where Country="Turkey";
使用SQL视图 (Using SQL View)
We have created the SQL View successfully. We can use it as a regular table. In this example, we will use Select
statement by providing the SQL View name as a table name which is TurkishCustomers
in this example.
我们已经成功创建了SQL View。 我们可以将其用作常规表。 在此示例中,我们将通过提供SQL View名称作为表名来使用Select
语句,该名称在此示例中为TurkishCustomers
。
Select * From TurkishCustomers;
更新SQL视图 (Update SQL View)
In some cases, the SQL View may have some changes. But as we use the View in a lot of SQL queries we just need to update the SQL View content like fields or conditions. We can use Create Or Replace View
statement. This statement will update the SQL View if it exists, if not the SQL View will be created from scratch.
在某些情况下,SQL视图可能会有一些更改。 但是,由于我们在许多SQL查询中使用View,所以我们只需要更新SQL View内容(例如字段或条件)即可。 我们可以使用Create Or Replace View
语句。 该语句将更新SQL View(如果存在),否则将从头开始创建SQL View。
In this example, we will update SQL View named TurkishCustomers
.
在此示例中,我们将更新名为TurkishCustomers
SQL视图。
Create Or Replace View TurkishCustomers AS
Select Name, Contact From AllCustomers Where Country="Turkey";
删除或删除SQL视图 (Drop or Remove SQL View)
If we do not need the SQL View nay we can delete it with Drop View
statement. This can be useful if we will use the view name as a regular table. In this example we will delete or drop the SQL View TurkishCustomers
.
如果我们不需要SQL View,则可以使用Drop View
语句将其Drop View
。 如果我们将视图名称用作常规表,这将很有用。 在此示例中,我们将删除或删除SQL视图TurkishCustomers
。
Drop View TurkishCustomers;
翻译自: https://www.poftut.com/how-to-create-use-and-delete-sql-view/
sql 视图的更新和删除