一、练习素材
学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
即学号,姓名,性别,年龄,所在系; Sno为主键
课程表:Course (Cno, Cname,)
即课程号,课程名 ;Cno为主键
学生选课表:SC (Sno, Cno, Score)
即学号,课程号,成绩 ;Sno为主键
-- 创建学生表并插入数据
create table Student(
Sno int(11) primary key,
Sname varchar(20),
Ssex char(1),
Sage int(3),
Sdept varchar(10)
);
insert into Student values(101,'张三','男',20,'计算机'),
(102,'李四','女',19,'经管'),
(103,'王五','男',21,'建筑'),
(104,'赵六','男',20,'计算机'),
(105,'陈七','女',19,'经管');
-- 创建课程表并插入数据
create table Course(
Cno int(11) primary key,
Cname varchar(20)
);
insert into Course values(1111,'高等数学'),
(2222,'大学英语'),
(3333,'MySQL从入门到精通'),
(4444,'如何获取富婆欢心'),
(5555,'如何做白日梦');
--创建学生选课表并插入数据
create table SC(
Sno int(11) primary key,
Cno int(11),
Score int(10)
);
insert into SC values(103,1111,75),
(101,4444,100),
(105,5555,69),
(102,3333,99),
(104,2222,87);
-- 查看各个表中的数据
mysql> select * from Student;
+-----+--------+------+------+-----------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----+--------+------+------+-----------+
| 101 | 张三 | 男 | 20 | 计算机 |
| 102 | 李四 | 女 | 19 | 经管 |
| 103 | 王五 | 男 | 21 | 建筑 |
| 104 | 赵六 | 男 | 20 | 计算机 |
| 105 | 陈七 | 女 | 19 | 经管 |
+-----+--------+------+------+-----------+
5 rows in set (0.00 sec)
mysql> select * from Course;
+------+--------------------------+
| Cno | Cname |
+------+--------------------------+
| 1111 | 高等数学 |
| 2222 | 大学英语 |
| 3333 | MySQL从入门到精通 |
| 4444 | 如何获取富婆欢心 |
| 5555 | 如何做白日梦 |
+------+--------------------------+
5 rows in set (0.00 sec)
mysql> select * from SC;
+-----+------+-------+
| Sno | Cno | Score |
+-----+------+-------+
| 101 | 4444 | 100 |
| 102 | 3333 | 99 |
| 103 | 1111 | 75 |
| 104 | 2222 | 87 |
| 105 | 5555 | 69 |
+-----+------+-------+
5 rows in set (0.00 sec)
二、练习题目
1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
2、删除视图 stu_info。
参考答案(个人所写,仅供参考)
--1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
mysql> create view View_Stu as select Sname,Ssex,Cname,Score
-> from Student,Course,SC
-> where Student.Sno=SC.Sno and Course.Cno=SC.Cno;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from View_Stu;
+--------+------+--------------------------+-------+
| Sname | Ssex | Cname | Score |
+--------+------+--------------------------+-------+
| 张三 | 男 | 如何获取富婆欢心 | 100 |
| 李四 | 女 | MySQL从入门到精通 | 99 |
| 王五 | 男 | 高等数学 | 75 |
| 赵六 | 男 | 大学英语 | 87 |
| 陈七 | 女 | 如何做白日梦 | 69 |
+--------+------+--------------------------+-------+
5 rows in set (0.00 sec)
--2、删除视图 stu_info。
mysql> show create view View_Stu \G
*************************** 1. row ***************************
View: View_Stu
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `View_Stu` AS select `Student`.`Sname` AS `Sname`,`Student`.`Ssex` AS `Ssex`,`Course`.`Cname` AS `Cname`,`SC`.`Score` AS `Score` from ((`Student` join `Course`) join `SC`) where ((`Student`.`Sno` = `SC`.`Sno`) and (`Course`.`Cno` = `SC`.`Cno`))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
mysql> drop view View_Stu;
Query OK, 0 rows affected (0.00 sec)
mysql> show create view View_Stu \G
ERROR 1146 (42S02): Table 'test7_13.View_Stu' doesn't exist