sql视图语句
A View is a database object that presents data from in one or more tables. The same SQL statement used to create a view can also be used to replace an existing view.
视图是一个数据库对象,用于显示一个或多个表中的数据。 用于创建视图的相同SQL语句也可以用于替换现有视图。
This guide will update (replace) the existing view “programming-students-v” with one that is slightly different and has a different name.
本指南将使用略有不同且名称不同的视图来更新(替换)现有视图“ programming-students-v”。
Safety tip: always backup the schema before making changes to it.
安全提示:始终在更改架构之前备份架构。
一般语法 (General sytax)
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SQL用于创建视图和当前数据 (SQL Used to create the view and the current data)
create view `programming-students-v` as
select FullName, programOfStudy
from student
where programOfStudy = 'Programming';
select * from `programming-students-v`;
Current Data:
当前数据:
+-----------------+----------------+
| FullName | programOfStudy |
+-----------------+----------------+
| Teri Gutierrez | Programming |
| Spencer Pautier | Programming |
| Louis Ramsey | Programming |
| Alvin Greene | Programming |
| Sophie Freeman | Programming |
+-----------------+----------------+
5 rows in set (0.00 sec)
A list of the existing views:
现有视图的列表:
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)
更换视图 (Replacing the view)
create or replace view `programming-students-v` as
select FullName, programOfStudy, sat_score
from student
where programOfStudy = 'Programming';
select * from `programming-students-v`;
Note: the view now shows the sat_score.
注意:该视图现在显示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 |
+-----------------+----------------+-----------+
Note: the list of views hasn’t change, our view is replaced.
注意:视图列表未更改,我们的视图已替换。
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)
As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.
与所有这些SQL事物一样,它们比本入门指南中的内容要多得多。
I hope this at least gives you enough to get started. Please see the manual for your database manager and have fun trying different options yourself.
我希望这至少能给您足够的入门。 请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。
翻译自: https://www.freecodecamp.org/news/the-sql-replace-view-statement-example-syntax/
sql视图语句