sql 视图的更新和删除_如何创建,使用和删除SQL视图?

sql 视图的更新和删除

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。

LEARN MORE  How To Download and Install MS SQL Server 2017 Express On Windows?
了解更多信息如何在Windows上下载并安装MS SQL Server 2017 Express?

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 视图的更新和删除

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值