表定义:
-------
学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
-------------------------------------------------------------
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
-------------------------------------------------------------
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno,Cno为主键
-------------------------------------------------------------1.用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。
mysql> create table student (
-> sno int primary key comment '学号',
-> sname varchar(50) unique comment '姓名',
-> ssex enum('男','女') comment '性别',
-> sage int comment '年龄',
-> sdept varchar(50) default '计算机' comment '所在系'
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> desc Student;
+-------+-------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------+------+-----+-----------+-------+
| Sno | int | NO | PRI | NULL | |
| Sname | varchar(50) | YES | UNI | NULL | |
| Ssex | enum('男','女') | YES | | NULL | |
| Sage | int | YES | | NULL | |
| Sdept | varchar(50) | YES | | 计算机 | |
+-------+-------------------+------+-----+-----------+-------+
5 rows in set (0.00 sec)
mysql> insert into Student values
-> (1, '张三', '男', 20, '计算机'),
-> (2, '李四', '女', 22, '计算机'),
-> (3, '王五', '男', 21, '数学'),
-> (4, '赵六', '女', 23, '数学'),
-> (5, '孙七', '男', 24, '英语');
Query OK, 0 rows affected (0.04 sec)mysql> select * from Student;
+-----+--------+------+------+-----------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----+--------+------+------+-----------+
| 1 | 张三 | 男 | 20 | 计算机 |
| 2 | 李四 | 女 | 22 | 计算机 |
| 3 | 王五 | 男 | 21 | 数学 |
| 4 | 赵六 | 女 | 23 | 数学 |
| 5 | 孙七 | 男 | 24 | 英语 |
+-----+--------+------+------+-----------+
5 rows in set (0.00 sec)
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。mysql> alter table student modify sage smallint;
Query OK, 0 rows affected (0.04 sec)
mysql> desc Student;
+-------+-------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------+------+-----+-----------+-------+
| Sno | int | NO | PRI | NULL | |
| Sname | varchar(50) | YES | UNI | NULL | |
| Ssex | enum('男','女') | YES | | NULL | |
| Sage | smallint | YES | | NULL | |
| Sdept | varchar(50) | YES | | 计算机 | |
+-------+-------------------+------+-----+-----------+-------+
5 rows in set (0.00 sec)3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX 。
mysql> create table sc (
-> sno int comment '学号',
-> cno int comment '课程号',
-> score int comment '成绩'
-> primary key(sno,cno)
-> index(SC_INDEX)
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> desc SC;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Sno | int | NO | PRI | NULL | |
| Cno | int | NO | PRI | NULL | |
| Score | int | YES | | NULL | |
+-------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> insert into SC values (1, 1, 90),(2, 1, 85),(3, 2, 92),(4, 2, 88),(5, 3, 95);
Query OK, 0 rows affected (0.04 sec)
mysql> select * from SC;
+-----+-----+-------+
| Sno | Cno | Score |
+-----+-----+-------+
| 1 | 1 | 90 |
| 2 | 1 | 85 |
| 3 | 2 | 92 |
| 4 | 2 | 88 |
| 5 | 3 | 95 |
+-----+-----+-------+
5 rows in set (0.00 sec)4.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
先创建一个课程表,并插入一些数据
mysql> create table course (
-> cno int primary key comment '课程号',
-> cname varchar(50) comment '课程名'
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> desc Course;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Con | int | YES | | NULL | |
| Cname | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> insert into Course values (1, '云原生'),(2, '云原生'),(3, '高等数学'),(4, '高等数学'),(5, '英语');
Query OK, 0 rows affected (0.04 sec)
mysql> select * from Course;
+------+--------------+
| Con | Cname |
+------+--------------+
| 1 | 云原生 |
| 1 | 云原生 |
| 2 | 高等数学 |
| 2 | 高等数学 |
| 3 | 英语 |
+------+--------------+
5 rows in set (0.01 sec)
创建视图并查询:
mysql> create view stu_info as select sname,ssex,cname,score from student stu inner join sc s on stu.sno=s.sno inner join course c on s.cno=c.cno;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from stu_info;
+--------+--------+--------------+--------+
| 姓名 | 性别 | 课程名 | 成绩 |
+--------+--------+--------------+--------+
| 张三 | 男 | 云原生 | 90 |
| 李四 | 女 | 云原生 | 85 |
| 王五 | 男 | 高等数学 | 92 |
| 赵六 | 女 | 高等数学 | 88 |
| 孙七 | 男 | 英语 | 95 |
+--------+--------+--------------+--------+
5 rows in set (0.00 sec)