练习8:MySQL视图应用

【案例】假设HenanHebei的3个学生参加TsinghuaUniversity、PekingUniversity的自学考试,现在需要用数据对其考试的结果进行查询和管理,TsinghuaUniversity的分数线为40,PekingUniversity的分数线为41。

学生表包含了学生的学号、姓名、家庭住址和电话号码;报名表包含学号、姓名、所在学校和报名的学校,表结构以及表中的内容分别如下所示:
练习8:MySQL视图应用_第1张图片
练习8:MySQL视图应用_第2张图片
步骤1:创建学生表stu,插入3条记录。

mysql> create table stu
    -> (
    -> s_id int primary key,
    -> s_name varchar(20) not null,
    -> addr varchar(50) not null,
    -> tel varchar(50) not null
    -> );
Query OK, 0 rows affected (0.12 sec)

mysql> insert into stu
    -> values(1,'XiaoWang','Henan','0371-1234**78'),
    -> (2,'XiaoLi','Hebei','134722*****'),
    -> (3,'XiaoTian','Henan','0371-1234**70');
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from stu;
+------+----------+-------+---------------+
| s_id | s_name   | addr  | tel           |
+------+----------+-------+---------------+
|    1 | XiaoWang | Henan | 0371-1234**78 |
|    2 | XiaoLi   | Hebei | 134722*****   |
|    3 | XiaoTian | Henan | 0371-1234**70 |
+------+----------+-------+---------------+
3 rows in set (0.00 sec)

步骤2:创建报名表sign,插入三条报考记录:

mysql> create table sign
    -> (
    -> s_id int primary key,
    -> s_name varchar(20) not null,
    -> s_sch varchar(50) not null,
    -> s_sign_sch varchar(50) not null
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> insert into sign
    -> values(1,'Xiaowang','Middle School1','Peking University'),
    -> (2,'Xiaoli','Middle School2','Tsinghua University'),
    -> (3,'XiaoTian','Middle School3','Tsinghua University');
Query OK, 3 rows affected (0.07 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select  * from sign;
+------+----------+----------------+---------------------+
| s_id | s_name   | s_sch          | s_sign_sch          |
+------+----------+----------------+---------------------+
|    1 | Xiaowang | Middle School1 | Peking University   |
|    2 | Xiaoli   | Middle School2 | Tsinghua Universitu |
|    3 | XiaoTian | Middle School3 | Tsinghua University |
+------+----------+----------------+---------------------+
3 rows in set (0.00 sec)

步骤3:创建成绩表stu_mark,插入三条记录:

mysql> create table stu_mark
    -> (
    -> s_id int primary key,
    -> s_name varchar(20) not null,
    -> mark int not null
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into stu_mark values(1,'XiaoWang',80),
    -> (2,'XiaoLi',71),
    -> (3,'XiaoTian',70);
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from stu_mark;
+------+----------+------+
| s_id | s_name   | mark |
+------+----------+------+
|    1 | XiaoWang |   80 |
|    2 | XiaoLi   |   71 |
|    3 | XiaoTian |   70 |
+------+----------+------+
3 rows in set (0.00 sec)

步骤4:创建考上PekingUniversity的学生的视图,SQL语句如下:

mysql> create view beida (id,name,mark,sch)
    -> as select stu_mark.s_id,stu_mark.s_name,stu_mark.mark,sign.s_sign_sch
    -> from stu_mark,sign
    -> where stu_mark.s_id = sign.s_id AND stu_mark.mark>=41 AND sign.s_sign_sch='PekingUniversity';
Query OK, 0 rows affected (0.06 sec)

mysql> select * from beida;
+----+----------+------+-------------------+
| id | name     | mark | sch               |
+----+----------+------+-------------------+
|  1 | XiaoWang |   80 | Peking University |
+----+----------+------+-------------------+
1 row in set (0.00 sec)

视图beida包含了考上PekingUniversity的学号、姓名、成绩和报考的学校名称,其中报考的学校名称为PekingUniversity。通过select语句进行查看,可以获得成绩在PekingUniversity分数线之上的学生信息。

步骤5:创建考上TsinghuaUniversity的学生的视图

mysql> create view qinghua (id,name,mark,sch)
    -> as select stu_mark.s_id,stu_mark.s_name,stu_mark.mark,sign.s_sign_sch
    -> from stu_mark,sign
    -> where stu_mark.s_id=sign.s_id AND stu_mark.mark >=40 AND
    -> sign.s_sign_sch='TsinghuaUniversity';
Query OK, 0 rows affected (0.06 sec)

mysql> select * from qinghua;
+----+----------+------+---------------------+
| id | name     | mark | sch                 |
+----+----------+------+---------------------+
|  2 | XiaoLi   |   71 | Tsinghua University |
|  3 | XiaoTian |   70 | Tsinghua University |
+----+----------+------+---------------------+
2 rows in set (0.00 sec)

步骤6:在录入的时候XiaoTian的成绩录入错误,多录了50分,对其录入成绩进行改正。
更新XiaoTian的成绩,代码如下:

mysql> update stu_mark set mark = mark-50
    -> where stu_mark.s_name = 'XiaoTian';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

步骤7:查看更新后视图和表的情况。

mysql> select * from stu_mark;
+------+----------+------+
| s_id | s_name   | mark |
+------+----------+------+
|    1 | XiaoWang |   80 |
|    2 | XiaoLi   |   71 |
|    3 | XiaoTian |   20 |
+------+----------+------+
3 rows in set (0.00 sec)

mysql> select * from qinghua;
+----+--------+------+---------------------+
| id | name   | mark | sch                 |
+----+--------+------+---------------------+
|  2 | XiaoLi |   71 | Tsinghua University |
+----+--------+------+---------------------+
1 row in set (0.00 sec)

mysql> select * from beida;
+----+----------+------+-------------------+
| id | name     | mark | sch               |
+----+----------+------+-------------------+
|  1 | XiaoWang |   80 | Peking University |
+----+----------+------+-------------------+
1 row in set (0.00 sec)

从结果来看视图qinghua中已经不存在XiaoTian的信息了,说明更新成绩基本表stu_mark后,视图qinghua的内容也相应地更新了。

步骤8:查看视图的创建信息。

select * from information_schema.views \G

步骤9:删除创建的视图

mysql> drop view beida;
Query OK, 0 rows affected (0.06 sec)

mysql> drop view qinghua;
Query OK, 0 rows affected (0.07 sec)

执行完毕,qinghua和beida两个视图分别被成功的删除。


几个注意点:

MySQL中视图和表的区别以及联系是什么?

区别:

  • ①视图是已经编译好的sql语句,是基于sql语句的结果集的可视化表,而表不是,
  • ②视图没有实际的物理记录,而表有。
  • ③表是内容,视图是窗口。
  • ④表占用物理空间,而视图不占用物理空间,只是逻辑概念的存在。表可以及时修改,但视图只能用创建的语句来修改。
  • ⑤视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些sql语句的集合。视图可以防止用户解除数据表,因而用户不知道表结构。
  • ⑥表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
  • ⑦视图的建立和删除只影响视图本身,不影响对应的基本表。

联系:

  • 视图是在基本表之上建立的表,它的结构(所定义的列)和内容(所有记录)都来自基本表,依据基本表存在而存在。
  • 一个视图既可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。

你可能感兴趣的:(【随便写写】)