【案例】假设HenanHebei的3个学生参加TsinghuaUniversity、PekingUniversity的自学考试,现在需要用数据对其考试的结果进行查询和管理,TsinghuaUniversity的分数线为40,PekingUniversity的分数线为41。
学生表包含了学生的学号、姓名、家庭住址和电话号码;报名表包含学号、姓名、所在学校和报名的学校,表结构以及表中的内容分别如下所示:
步骤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中视图和表的区别以及联系是什么?
区别:
联系: