Mysql 简明教程

MySQL - Drop View

MySQL 视图是从预定义的 SQL 查询生成的虚拟表。它包含一个或多个数据库表中的(全部或选定的)记录。

A MySQL View is a virtual table which is generated from a predefined SQL query. It contains (all or selective) records from one or more database tables.

视图不会物理存储在数据库中,但在不再需要时仍可以将其删除。即使它们用于查看和修改数据库表中的数据,但当视图被删除时,该表中的数据将保持不变。

Views are not stored in a database physically, but they can still be dropped whenever not necessary. Even though they are used to see and modify the data in a database table, the data in that table remains unchanged when views are dropped.

The MySQL DROP VIEW Statement

MySQL 中的 DROP VIEW 语句用于删除现有视图以及其定义和其他信息。一旦视图被删除,它的所有权限也会被移除。我们还可以使用此语句删除索引视图。

The DROP VIEW statement in MySQL is used to delete an existing view, along with its definition and other information. Once the view is dropped, all the permissions for it will also be removed. We can also use this statement to drop indexed views.

假设使用 DROP TABLE 命令删除了一个表并且它有一个与之关联的视图,也必须使用 DROP VIEW 命令显式删除此视图。

Suppose a table is dropped using the DROP TABLE command and it has a view associated to it, this view must also be dropped explicitly using the DROP VIEW command.

NOTE -

NOTE

  1. While trying to perform queries, the database engine checks all the objects referenced in that statement are valid and exist. So, if a view does not exist in the database, the DROP VIEW statement will throw an error.

  2. To drop a table in a database, one must require ALTER permission on the said table and CONTROL permissions on the table schema.

Syntax

以下是 DROP VIEW 语句的语法 -

Following is the syntax of the DROP VIEW Statement −

DROP VIEW view_name;

其中,view_name 是要删除的视图的名称。

Where, view_name is the name of the view to be deleted.

Example

假设我们使用以下 CREATE TABLE 查询创建了一个名为 CUSTOMERS 的表 -

Suppose we have created a table named CUSTOMERS using the following CREATE TABLE query −

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR(15) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS VARCHAR(25),
   SALARY DECIMAL(10, 2),
   PRIMARY KEY(ID)
);

让我们使用以下 INSERT 查询将记录插入到上面创建的表中 -

Let us insert records in the above created table using the following INSERT query −

INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', '32', 'Ahmedabad', 2000),
(2, 'Khilan', '25', 'Delhi', 1500),
(3, 'Kaushik', '23', 'Kota', 2500),
(4, 'Chaitali', '26', 'Mumbai', 6500),
(5, 'Hardik','27', 'Bhopal', 8500),
(6, 'Komal', '22', 'MP', 9000),
(7, 'Muffy', '24', 'Indore', 5500);

Creating a View −

Creating a View −

现在,让我们使用 CREATE VIEW 语句在此表上创建视图,如下所示 -

Now, let us create a view on this table using the CREATE VIEW statement as shown below −

CREATE VIEW testView AS SELECT * FROM CUSTOMERS;

您可以使用以下查询来验证所有视图的列表:

You can verify the list of all the views using the following query −

SHOW FULL TABLES WHERE table_type = 'VIEW';

视图将按如下方式创建:

The view will be created as follows −

Dropping a View −

Dropping a View −

以下查询删除上面创建的视图 -

Following query drops the view created above −

DROP VIEW testView;

Verification

为了验证我们是否已删除视图,可以使用以下查询显示视图列表 -

To verify if we have deleted the view or not, display the list of views using the query below −

SHOW FULL TABLES WHERE table_type = 'VIEW';

当视图被删除时,将返回一个空集。

As the view is dropped, an empty set is returned.

Empty set (0.12 sec)

The IF EXISTS clause

如果您尝试删除不存在的视图,将会生成一个错误。让我们看一个示例,我们在其中使用以下查询删除名为 NEW 的视图 -

If you try to drop a view that doesn’t exist, an error will be generated. Let us see an example where we are dropping a view named NEW using the following query −

DROP VIEW NEW;

以下错误将被显示(其中“tutorialspoint”是数据库名称)−

The following error is displayed (where 'tutorialspoint' is the database name) −

ERROR 1051 (42S02): Unknown table 'tutorialspoint.new'

但是,如果你将 IF EXISTS 子句与 DROP VIEW 语句结合使用如下所示,查询将被忽略即使不存在具有给定名称的 VIEW。

However, if you use the IF EXISTS clause along with the DROP VIEW statement as shown below, the query will be ignored even if a VIEW with the given name does not exist.

DROP VIEW IF EXISTS NEW;

Deleting Rows from a View

我们不仅可以移除整个视图,我们还可以使用带有 WHERE 子句的 DELETE 语句删除视图的 选定行。

Instead of removing an entire view, we can also drop selected rows of a view using the DELETE statement with a WHERE clause.

Syntax

以下 DELETE 语句的语法:

Following is the syntax of the DELETE statement −

DELETE FROM view_name WHERE condition;

Example

在此示例中,让我们首先使用以下查询在 CUSTOMERS 表上创建一个 testView −

In this example, let us first create a testView on the CUSTOMERS table using the following query −

CREATE VIEW testView AS SELECT * FROM CUSTOMERS;

现在,使用以下查询,你可以从 CUSTOMERS 表上创建的 testView 中删除一条记录。对视图中数据的更改最终将反映在基础表 CUSTOMERS 中。

Now, using the following query, you can delete a record from the testView created on the CUSTOMERS table. The changes made to the data in view will finally be reflected in the base table CUSTOMERS.

DELETE FROM testView WHERE Location = 'Indore';

关联表 CUSTOMERS 将具有以下记录 −

The associated table CUSTOMERS will have the following records −

Dropping View Using Client Program

除了使用 MySQL 查询从 MySQL 数据库删除视图,我们还可以使用客户端程序对表执行另一个操作。

In addition to drop a view from the MySQL database using the MySQL query, we can also perform the another operation on a table using a client program.

Syntax

以下是各种编程语言中 MySQL Drop View 的语法 −

Following are the syntaxes of the Drop View from MySQL in various programming languages −

Example

以下是该操作在各种编程语言中的实现 −

Following are the implementations of this operation in various programming languages −