SQL替换VIEW语句


介绍

View是一个数据库对象,它在一个或多个表中显示数据。用于创建视图的相同SQL语句也可用于替换现有视图。

本指南将使用略有不同且名称不同的视图更新(替换)现有视图“programming-students-v”。

安全提示:在对模式进行更改之前始终备份模式。

一般语法

CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

SQL用于创建视图和当前数据

create view `programming-students-v` as
select FullName, programOfStudy
from student
where programOfStudy = 'Programming';
select * from `programming-students-v`;

当前数据:

+-----------------+----------------+
 | FullName        | programOfStudy |
 +-----------------+----------------+
 | Teri Gutierrez  | Programming    |
 | Spencer Pautier | Programming    |
 | Louis Ramsey    | Programming    |
 | Alvin Greene    | Programming    |
 | Sophie Freeman  | Programming    |
 +-----------------+----------------+
 5 rows in set (0.00 sec)

现有视图的列表:

SHOW FULL TABLES IN fcc_sql_guides_database WHERE TABLE_TYPE LIKE 'VIEW';
+-----------------------------------+------------+
 | Tables_in_fcc_sql_guides_database | Table_type |
 +-----------------------------------+------------+
 | programming-students-v            | VIEW       |
 | students-contact-info_v           | VIEW       |
 | students_dropme_v                 | VIEW       |
 +-----------------------------------+------------+
 3 rows in set (0.00 sec)

替换视图

create or replace view `programming-students-v` as
select FullName, programOfStudy, sat_score
from student
where programOfStudy = 'Programming';
select * from `programming-students-v`;

注意:视图现在显示sat_score。

+-----------------+----------------+-----------+
 | FullName        | programOfStudy | sat_score |
 +-----------------+----------------+-----------+
 | Teri Gutierrez  | Programming    |       800 |
 | Spencer Pautier | Programming    |      1000 |
 | Louis Ramsey    | Programming    |      1200 |
 | Alvin Greene    | Programming    |      1200 |
 | Sophie Freeman  | Programming    |      1200 |
 +-----------------+----------------+-----------+

注意:视图列表没有更改,我们的视图被替换。

mysql>  SHOW FULL TABLES IN fcc_sql_guides_database WHERE TABLE_TYPE LIKE 'VIEW';
 +-----------------------------------+------------+
 | Tables_in_fcc_sql_guides_database | Table_type |
 +-----------------------------------+------------+
 | programming-students-v            | VIEW       |
 | students-contact-info_v           | VIEW       |
 | students_dropme_v                 | VIEW       |
 +-----------------------------------+------------+
 3 rows in set (0.00 sec)

*与所有这些SQL事物一样,它们比本入门指南中的内容更多。我希望这至少足以让你开始。请参阅您的数据库管理员手册,并自己尝试不同的选项。

更多SQL教程

学习更多SQL教程