是的,从视图中删除行从基表中删除行。让我们通过创建一个新表来了解这一点。创建表的查询如下
mysql> create table deleteFromBaseTableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into deleteFromBaseTableDemo(Name) values('John'); mysql> insert into deleteFromBaseTableDemo(Name) values('Carol'); mysql> insert into deleteFromBaseTableDemo(Name) values('Bob'); mysql> insert into deleteFromBaseTableDemo(Name) values('Sam');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from deleteFromBaseTableDemo;
以下是输出
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | +----+-------+ 4 rows in set (0.00 sec)
让我们创建一个视图。创建视图的查询如下
mysql> create view delete_view as select Id,Name from deleteFromBaseTableDemo;
让我们检查所有视图记录。查询如下-
mysql> select *from delete_view;
以下是输出
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | +----+-------+ 4 rows in set (0.05 sec)
现在,如果您从视图中删除,那么它也会自动从基表中删除。
从视图中删除行的查询如下
mysql> delete from delete_view where Id=4;
让我们检查视图和父表中的所有记录。查询如下-
mysql> select *from delete_view;
以下是输出
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | +----+-------+ 3 rows in set (0.00 sec)
显示基本表中所有记录的查询如下
mysql> select *from deleteFromBaseTableDemo;
以下是输出
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | +----+-------+ 3 rows in set (0.00 sec)
查看两个示例输出,已从父表和视图中删除了值为4的ID。