SQL 知识量:22 - 44 - 129
视图通过create view语句来创建。只能通过create view创建不存在的视图(因为不能重名)。
如果要重命名视图,必须先删除,再重建。无法通过覆盖的方式重命名视图。要删除视图,可以使用drop view语句。
视图可以简化联结,例如:建立一个查询学生信息及选课信息的视图showCourse。
create view showCourse as select s.*,l.name as lname from student as s,lesson as l,course as c where s.id=c.id_student and l.id=c.id_lesson;
以上SQL语句创建了视图showCourse,查询视图内容语句如下:
select * from showCourse;
结果:
+----+-------+-----+--------+---------+ | id | name | age | sex | lname | +----+-------+-----+--------+---------+ | 1 | Susan | 11 | female | Maths | | 5 | Jen | 11 | female | Natural | | 2 | Jame | 12 | male | Maths | | 4 | Robot | 10 | male | Maths | | 6 | Toney | 10 | male | Maths | | 1 | Susan | 11 | female | English | | 3 | Bob | 11 | male | English | | 4 | Robot | 10 | male | English | | 5 | Jen | 11 | female | English | | 2 | Jame | 12 | male | Natural | | 4 | Robot | 10 | male | Natural | | 6 | Toney | 10 | male | English | +----+-------+-----+--------+---------+
查询以上视图与查询联结的结果是一样的。但是,建立视图后,可以重用SQL语句,而且十分方便,例如:想要再查询Jame的选课信息,只要以下语句即可:
select * from showCourse where name='Jame';
结果:
+----+------+-----+------+---------+ | id | name | age | sex | lname | +----+------+-----+------+---------+ | 2 | Jame | 12 | male | Maths | | 2 | Jame | 12 | male | Natural | +----+------+-----+------+---------+
视图的另一个用途是重新格式化检索出的数据。
例如:建立一个视图showStudent,用来查询学生姓名、年龄和有关表述。
create view showStudent as select name,age,concat(name,' is ',age,' years old.') as describes from student;
创建视图showStudent后,查询该视图:
select * from showStudent;
结果:
+--------+-----+-------------------------+ | name | age | describes | +--------+-----+-------------------------+ | Susan | 11 | Susan is 11 years old. | | Jame | 12 | Jame is 12 years old. | | Bob | 11 | Bob is 11 years old. | | Robot | 10 | Robot is 10 years old. | | Jen | 11 | Jen is 11 years old. | | Toney | 10 | Toney is 10 years old. | | Jack | 13 | Jack is 13 years old. | | Alice | 10 | Alice is 10 years old. | | Bonnie | 11 | Bonnie is 11 years old. | | Ella | 12 | Ella is 12 years old. | +--------+-----+-------------------------+
如果要查询年龄大于11岁的学生信息,按年龄升序排列,其SQL语句为:
select * from showStudent where age>11 order by age;
结果:
+------+-----+-----------------------+ | name | age | describes | +------+-----+-----------------------+ | Jame | 12 | Jame is 12 years old. | | Ella | 12 | Ella is 12 years old. | | Jack | 13 | Jack is 13 years old. | +------+-----+-----------------------+
创建视图时,可以通过使用where子句对数据进行过滤,构建一个理想的虚拟表。
例如:建立一个包含访客完整信息的视图newVisitor,排除没有登记国家的情况。
create view newVisitor as select * from visitor where country is not null;
查询视图
+----+-------+---------+ | id | name | country | +----+-------+---------+ | 1 | Bob | USA | | 3 | Susan | UK | | 4 | Chen | China | | 5 | Koco | Japan | | 7 | Susan | UK | | 8 | Bob | USA | +----+-------+---------+
在这个视图的基础上,我们可以再查找来自美国的客人信息。
select * from newVisitor where country='USA';
结果:
+----+------+---------+ | id | name | country | +----+------+---------+ | 1 | Bob | USA | | 8 | Bob | USA | +----+------+---------+
以上示例中,在视图newVisitor中含有一个where子句,在利用视图查询美国客人信息时,又使用了一个where子句,那么这两个where子句在最终查询时将自动组合。
视图还可以用于简化计算字段。
例如:创建视图future,查询学生姓名和10年之后的年龄。
create view future as select name,age+10 as after10years from student;
创建视图成功后,查询视图future的信息:
select * from future;
结果:
+--------+--------------+ | name | after10years | +--------+--------------+ | Susan | 21 | | Jame | 22 | | Bob | 21 | | Robot | 20 | | Jen | 21 | | Toney | 20 | | Jack | 23 | | Alice | 20 | | Bonnie | 21 | | Ella | 22 | +--------+--------------+
如果要查询10年后,不满22岁的学生姓名和年龄,可以使用如下SQL语句:
select * from future where after10years<22 order by after10years;
查询结果:
+--------+--------------+ | name | after10years | +--------+--------------+ | Robot | 20 | | Toney | 20 | | Alice | 20 | | Susan | 21 | | Bob | 21 | | Jen | 21 | | Bonnie | 21 | +--------+--------------+
Copyright © 2017-Now pnotes.cn. All Rights Reserved.
编程学习笔记 保留所有权利
MARK:3.0.0.20240214.P35
From 2017.2.6