1.(1)创建名为Student(学生信息)的表,表中的各列要求如下:
字段名称 | 字段类型 | 大小 | 说明 |
Sno | char | 10 | 主键 |
Sname | char | 8 |
|
sex | char | 2 | 默认值为男,只能输入男或女 |
birthday | datetime |
|
|
Sdept | char | 20 |
|
(2)创建名为Course(课程信息)的表,表中的各列要求如下:
字段名称 | 字段类型 | 大小 | 说明 |
Cno | char | 10 | 主键 |
Cname | char | 30 | 惟一性 |
Ceredit | real |
|
|
(3)建名为Score(学生成绩)的表,表中的各列要求如下:
字段名称 | 字段类型 | 大小 | 取值范围 | 说明 |
Sno | char | 10 | 数据来自学生信息表 | 主键 |
Cno | char | 10 | 数据来自课程信息表 | 主键 |
Grade | real |
| 0--100 |
|
2. 增加、修改和删除字段,要求:
(1) 给Student表增加一个memo(备注)字段,类型为Varchar(200)
(2)将memo字段的类型修改为Varchar(300)
(3)删除memo 字段
3、
(实验表结构如下:
学生表: Student(Sno, Sname, Ssex ,Sage, Sdept) ,其中Sno为主键
课程表:Course(Cno,Cname, Ccredit),其中Cno为主键
学生选课表:SC(Sno,Cno,Grade),其中Sno,Cno的组合为主键
要求:首先创建数据库XSGL,在该数据库中创建以上三表,在各表中输入一些记录,然后进行下面的操作,写出相应的命令序列:
1)查询全体学生的学号和姓名。
2)查询全体学生的姓名、学号、所在系。
3)查询全体学生的详细信息。
4)查询全体学生的姓名及其出生年份。
5)查询软件工程系全体学生的名单。
6)查询所有年龄在20岁以下的学生姓名以及年龄。
7)查询考试成绩不及格的学生的学号。
8)查询年龄在20-23岁(包括20,23)之间的学生的姓名、系别和年龄。
9)查询不在信息系、数学系、也不在软件工程系学生的姓名和性别。
10)查询所有姓刘的学生的姓名、学号和性别。
11)查询姓“欧阳”且全名为三个汉字的学生的姓名。
12)查询姓名中第2个字为“阳”字的学生的姓名和学号。
13)查询所有不姓刘的学生的姓名、学号。
14)查询缺少成绩的学生的学号和相应的课程号。
15)查询软件工程系年龄在20岁以下的学生姓名。
16)查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。
17)查询全体学生情况,结果按所在系的升序排列,同一系的按年龄降序排列。
18)统计学生总人数。
19)查询选修了课程的学生人数。
20)计算选修了1号课程的学生平均成绩。
21)查询选修了1号课程的学生最高分数。
22)求各课程号及相应的选课人数。
23)查询选修3门以上课程的学生学号。
24)查询每个学生及其选修课程的情况。
25)查询选修2号课程且成绩在90分以上的所有学生。
26)查询每个学生的学号,姓名,选修的课程名和 成绩。
27)查询所有选修了1号课程的学生姓名。
28)查询选修了课程名为“数据库”的学生的学号和姓名。
mysql> create database xsgl
-> character set gb2312;
Query OK, 1 row affected (0.00 sec)
mysql> use xsgl
Database changed
mysql> create table student
-> (sno char(10) primary key,sname char(8),sex char(2) default '男',
-> birthday date,sdept char(20),constraint ck_sex check(sex in ('男','女')));
Query OK, 0 rows affected (0.09 sec)
mysql> create table course
-> (cno char(10) primary key,cname char(30) unique,
-> ceredit real);
Query OK, 0 rows affected (0.05 sec)
mysql> create table score
-> (sno char(10),cno char(10),grade real check(grade between 0 and 100),
-> primary key(sno,cno));
Query OK, 0 rows affected (0.06 sec)
mysql> alter table score
-> add constraint fk_sno foreign key (sno)
-> references student(sno);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table score
-> add constraint fk_cno foreign key (cno)
-> references course(cno);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into student
-> values('01','张三',default,'1990-2-2','软件系');
Query OK, 1 row affected (0.05 sec)
mysql> insert into student
-> values('02','李四',default,'1990-5-2','软件系');
Query OK, 1 row affected (0.02 sec)
mysql> insert into student
-> values('03','李阳','女','1992-8-2','网络系');
Query OK, 1 row affected (0.05 sec)
mysql> insert course
-> values('01','PHP',5.5);
Query OK, 1 row affected (0.03 sec)
mysql> insert course
-> values('02','javascript',7.5);
Query OK, 1 row affected (0.02 sec)
mysql> insert course
-> values('03','mysql',9.5);
Query OK, 1 row affected (0.02 sec)
mysql> insert course
-> values('04','html5',9.5);
Query OK, 1 row affected (0.01 sec)
mysql> insert course
-> values('05','linux',19.5);
Query OK, 1 row affected (0.05 sec)
mysql> insert into score
-> values('01','01',95);
Query OK, 1 row affected (0.03 sec)
mysql> insert into score
-> values('01','02',85);
Query OK, 1 row affected (0.02 sec)
mysql> insert into score
-> values('01','03',85);
Query OK, 1 row affected (0.02 sec)
mysql> insert into score
-> values('01','05',95);
Query OK, 1 row affected (0.03 sec)
mysql> insert into score
-> values('02','05',85);
Query OK, 1 row affected (0.03 sec)
mysql> insert into score
-> values('02','01',85);
Query OK, 1 row affected (0.02 sec)
mysql> insert into score
-> values('02','03',75);
Query OK, 1 row affected (0.02 sec)
mysql> insert into score
-> values('03','03',75);
Query OK, 1 row affected (0.03 sec)
mysql> alter table student
-> add memo varchar(200);
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+-----+-------+------+------------+--------+------+
| sno | sname | sex | birthday | sdept | memo |
+-----+-------+------+------------+--------+------+
| 01 | 张三 | 男 | 1990-02-02 | 软件系 | NULL |
| 02 | 李四 | 男 | 1990-05-02 | 软件系 | NULL |
| 03 | 李阳 | 女 | 1992-08-02 | 网络系 | NULL |
+-----+-------+------+------------+--------+------+
3 rows in set (0.03 sec)
mysql> desc student;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| sno | char(10) | NO | PRI | NULL | |
| sname | char(8) | YES | | NULL | |
| sex | char(2) | YES | | 男 | |
| birthday | date | YES | | NULL | |
| sdept | char(20) | YES | | NULL | |
| memo | varchar(200) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
6 rows in set (0.02 sec)
mysql> alter table student
-> modify memo varchar(300);
Query OK, 3 rows affected (0.11 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> desc student;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| sno | char(10) | NO | PRI | NULL | |
| sname | char(8) | YES | | NULL | |
| sex | char(2) | YES | | 男 | |
| birthday | date | YES | | NULL | |
| sdept | char(20) | YES | | NULL | |
| memo | varchar(300) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> alter table student
-> drop memo;
Query OK, 3 rows affected (0.13 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select sno,sname from student;
+-----+-------+
| sno | sname |
+-----+-------+
| 01 | 张三 |
| 02 | 李四 |
| 03 | 李阳 |
+-----+-------+
3 rows in set (0.00 sec)
mysql> select sno,sname,sdept from student;
+-----+-------+--------+
| sno | sname | sdept |
+-----+-------+--------+
| 01 | 张三 | 软件系 |
| 02 | 李四 | 软件系 |
| 03 | 李阳 | 网络系 |
+-----+-------+--------+
3 rows in set (0.00 sec)
mysql> select * from student;
+-----+-------+------+------------+--------+
| sno | sname | sex | birthday | sdept |
+-----+-------+------+------------+--------+
| 01 | 张三 | 男 | 1990-02-02 | 软件系 |
| 02 | 李四 | 男 | 1990-05-02 | 软件系 |
| 03 | 李阳 | 女 | 1992-08-02 | 网络系 |
+-----+-------+------+------------+--------+
3 rows in set (0.00 sec)
mysql> select sname,birthday from student;
+-------+------------+
| sname | birthday |
+-------+------------+
| 张三 | 1990-02-02 |
| 李四 | 1990-05-02 |
| 李阳 | 1992-08-02 |
+-------+------------+
3 rows in set (0.00 sec)
mysql> select sname,year(birthday) from student;
+-------+----------------+
| sname | year(birthday) |
+-------+----------------+
| 张三 | 1990 |
| 李四 | 1990 |
| 李阳 | 1992 |
+-------+----------------+
3 rows in set (0.00 sec)
mysql> select sname,day(birthday) from student;
+-------+---------------+
| sname | day(birthday) |
+-------+---------------+
| 张三 | 2 |
| 李四 | 2 |
| 李阳 | 2 |
+-------+---------------+
3 rows in set (0.02 sec)
mysql> select sname,month(birthday) from student;
+-------+-----------------+
| sname | month(birthday) |
+-------+-----------------+
| 张三 | 2 |
| 李四 | 5 |
| 李阳 | 8 |
+-------+-----------------+
3 rows in set (0.00 sec)
mysql> select * from student
-> where sdept='软件系';
+-----+-------+------+------------+--------+
| sno | sname | sex | birthday | sdept |
+-----+-------+------+------------+--------+
| 01 | 张三 | 男 | 1990-02-02 | 软件系 |
| 02 | 李四 | 男 | 1990-05-02 | 软件系 |
+-----+-------+------+------------+--------+
2 rows in set (0.02 sec)
mysql> select now();
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: xsgl
+---------------------+
| now() |
+---------------------+
| 2012-05-29 08:55:48 |
+---------------------+
1 row in set (0.20 sec)
mysql> select sname,year(now())-year(birthday) as 年龄 from student;
+-------+------+
| sname | 年龄 |
+-------+------+
| 张三 | 22 |
| 李四 | 22 |
| 李阳 | 20 |
+-------+------+
3 rows in set (0.00 sec)
mysql> select sname,year(now())-year(birthday) as 年龄 from student
-> where year(now())-year(birthday)<22;
+-------+------+
| sname | 年龄 |
+-------+------+
| 李阳 | 20 |
+-------+------+
1 row in set (0.00 sec)
mysql> insert into score
-> values('03','01',50);
Query OK, 1 row affected (0.05 sec)
mysql> select sno from score where grade<60;
+-----+
| sno |
+-----+
| 03 |
+-----+
1 row in set (0.00 sec)
mysql> insert into score
-> values('03','02',50);
Query OK, 1 row affected (0.05 sec)
mysql> insert into score
-> values('02','02',50);
Query OK, 1 row affected (0.05 sec)
mysql> insert into score
-> values('02','04',50);
Query OK, 1 row affected (0.03 sec)
mysql> select sno from score where grade<60;
+-----+
| sno |
+-----+
| 02 |
| 02 |
| 03 |
| 03 |
+-----+
4 rows in set (0.00 sec)
mysql> select distinct sno from score where grade<60;
+-----+
| sno |
+-----+
| 02 |
| 03 |
+-----+
2 rows in set (0.00 sec)
mysql> select sno,count(sno) from score
-> group by sno;
+-----+------------+
| sno | count(sno) |
+-----+------------+
| 01 | 4 |
| 02 | 5 |
| 03 | 3 |
+-----+------------+
3 rows in set (0.00 sec)
mysql> select sno,count(sno) from score
-> where grade <60
-> group by sno;
+-----+------------+
| sno | count(sno) |
+-----+------------+
| 02 | 2 |
| 03 | 2 |
+-----+------------+
2 rows in set (0.00 sec)
mysql> select sname,sdept,year(now())-year(birthday) as 年龄
-> from student
-> where year(now())-year(birthday) between 20 and 23;
+-------+--------+------+
| sname | sdept | 年龄 |
+-------+--------+------+
| 张三 | 软件系 | 22 |
| 李四 | 软件系 | 22 |
| 李阳 | 网络系 | 20 |
+-------+--------+------+
3 rows in set (0.00 sec)
mysql> select sname,sex from student
-> where sdept in ('数学系','信息系','软件系');
+-------+------+
| sname | sex |
+-------+------+
| 张三 | 男 |
| 李四 | 男 |
+-------+------+
2 rows in set (0.00 sec)
mysql> select sname,sex from student
-> where sdept not in ('数学系','信息系','软件系');
+-------+------+
| sname | sex |
+-------+------+
| 李阳 | 女 |
+-------+------+
1 row in set (0.00 sec)
mysql> select sname,sex from student
-> where sdept!='数学系' and sdept!='信息系' and sdept!='软件系';
+-------+------+
| sname | sex |
+-------+------+
| 李阳 | 女 |
+-------+------+
1 row in set (0.00 sec)
mysql> select sname,sex from student
-> where not (sdept='数学系' or sdept='信息系' or sdept='软件系');
+-------+------+
| sname | sex |
+-------+------+
| 李阳 | 女 |
+-------+------+
1 row in set (0.00 sec)
mysql> insert into student
-> values('04','刘留','女','1990-3-5','信息系');
Query OK, 1 row affected (0.05 sec)
mysql> select sname,sno,sex from student
-> where sname like '刘%';
+-------+-----+------+
| sname | sno | sex |
+-------+-----+------+
| 刘留 | 04 | 女 |
+-------+-----+------+
1 row in set (0.00 sec)
mysql> insert into student
-> values('05','欧阳妍妍','女','1990-3-5','信息系');
Query OK, 1 row affected (0.06 sec)
mysql> insert into student
-> values('05','欧阳峰','男','1990-3-5','信息系');
ERROR 1062 (23000): Duplicate entry '05' for key 'PRIMARY'
mysql> insert into student
-> values('06','欧阳峰','男','1990-3-5','信息系');
Query OK, 1 row affected (0.05 sec)
mysql> select sname,sno,sex from student
-> where sname like '欧阳_';
+--------+-----+------+
| sname | sno | sex |
+--------+-----+------+
| 欧阳峰 | 06 | 男 |
+--------+-----+------+
1 row in set (0.00 sec)
mysql> select sname,sno,sex from student
-> where sname like '_阳%';
+----------+-----+------+
| sname | sno | sex |
+----------+-----+------+
| 李阳 | 03 | 女 |
| 欧阳妍妍 | 05 | 女 |
| 欧阳峰 | 06 | 男 |
+----------+-----+------+
3 rows in set (0.00 sec)
mysql> select sname,sno,sex from student
-> where sname not like '刘%';
+----------+-----+------+
| sname | sno | sex |
+----------+-----+------+
| 张三 | 01 | 男 |
| 李四 | 02 | 男 |
| 李阳 | 03 | 女 |
| 欧阳妍妍 | 05 | 女 |
| 欧阳峰 | 06 | 男 |
+----------+-----+------+
5 rows in set (0.00 sec)
mysql> select sname,sno,sex from student
-> where not sname like '刘%';
+----------+-----+------+
| sname | sno | sex |
+----------+-----+------+
| 张三 | 01 | 男 |
| 李四 | 02 | 男 |
| 李阳 | 03 | 女 |
| 欧阳妍妍 | 05 | 女 |
| 欧阳峰 | 06 | 男 |
+----------+-----+------+
5 rows in set (0.00 sec)
mysql> select * from score;
+-----+-----+-------+
| sno | cno | grade |
+-----+-----+-------+
| 01 | 01 | 95 |
| 01 | 02 | 85 |
| 01 | 03 | 85 |
| 01 | 05 | 95 |
| 02 | 01 | 85 |
| 02 | 02 | 50 |
| 02 | 03 | 75 |
| 02 | 04 | 50 |
| 02 | 05 | 85 |
| 03 | 01 | 50 |
| 03 | 02 | 50 |
| 03 | 03 | 75 |
+-----+-----+-------+
12 rows in set (0.00 sec)
mysql> update score
-> set grade=null
-> where grade=50;
Query OK, 4 rows affected (0.06 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from score;
+-----+-----+-------+
| sno | cno | grade |
+-----+-----+-------+
| 01 | 01 | 95 |
| 01 | 02 | 85 |
| 01 | 03 | 85 |
| 01 | 05 | 95 |
| 02 | 01 | 85 |
| 02 | 02 | NULL |
| 02 | 03 | 75 |
| 02 | 04 | NULL |
| 02 | 05 | 85 |
| 03 | 01 | NULL |
| 03 | 02 | NULL |
| 03 | 03 | 75 |
+-----+-----+-------+
12 rows in set (0.00 sec)
mysql> select sno,cno from score where grade is null;
+-----+-----+
| sno | cno |
+-----+-----+
| 02 | 02 |
| 02 | 04 |
| 03 | 01 |
| 03 | 02 |
+-----+-----+
4 rows in set (0.00 sec)
mysql> select sname from student
-> where sdept='软件系' and year(now())-year(birthday)<=22;
+-------+
| sname |
+-------+
| 张三 |
| 李四 |
+-------+
2 rows in set (0.00 sec)
mysql> select sno,grade from score
-> where cno='03';
+-----+-------+
| sno | grade |
+-----+-------+
| 01 | 85 |
| 02 | 75 |
| 03 | 75 |
+-----+-------+
3 rows in set (0.00 sec)
mysql> select sno,grade from score
-> where cno='03'
-> order by grade desc;
+-----+-------+
| sno | grade |
+-----+-------+
| 01 | 85 |
| 02 | 75 |
| 03 | 75 |
+-----+-------+
3 rows in set (0.00 sec)
mysql> select * from student
-> order by sdept asc,year(now())-year(birthday) desc;
+-----+----------+------+------------+--------+
| sno | sname | sex | birthday | sdept |
+-----+----------+------+------------+--------+
| 01 | 张三 | 男 | 1990-02-02 | 软件系 |
| 02 | 李四 | 男 | 1990-05-02 | 软件系 |
| 03 | 李阳 | 女 | 1992-08-02 | 网络系 |
| 04 | 刘留 | 女 | 1990-03-05 | 信息系 |
| 05 | 欧阳妍妍 | 女 | 1990-03-05 | 信息系 |
| 06 | 欧阳峰 | 男 | 1990-03-05 | 信息系 |
+-----+----------+------+------------+--------+
6 rows in set (0.00 sec)
mysql> select * from student
-> order by sdept asc,sno desc;
+-----+----------+------+------------+--------+
| sno | sname | sex | birthday | sdept |
+-----+----------+------+------------+--------+
| 02 | 李四 | 男 | 1990-05-02 | 软件系 |
| 01 | 张三 | 男 | 1990-02-02 | 软件系 |
| 03 | 李阳 | 女 | 1992-08-02 | 网络系 |
| 06 | 欧阳峰 | 男 | 1990-03-05 | 信息系 |
| 05 | 欧阳妍妍 | 女 | 1990-03-05 | 信息系 |
| 04 | 刘留 | 女 | 1990-03-05 | 信息系 |
+-----+----------+------+------------+--------+
6 rows in set (0.00 sec)
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
1 row in set (0.00 sec)
mysql> select count(sno) from score;
+------------+
| count(sno) |
+------------+
| 12 |
+------------+
1 row in set (0.00 sec)
mysql> select count(distinct sno) from score;
+---------------------+
| count(distinct sno) |
+---------------------+
| 3 |
+---------------------+
1 row in set (0.00 sec)
mysql> select avg(grade) from score
-> where cno='01';
+------------+
| avg(grade) |
+------------+
| 90 |
+------------+
1 row in set (0.00 sec)
mysql> select avg(grade) from score
-> group by cno
-> having cno='01';
+------------+
| avg(grade) |
+------------+
| 90 |
+------------+
1 row in set (0.00 sec)
mysql> select max(grade) from score
-> where cno='01';
+------------+
| max(grade) |
+------------+
| 95 |
+------------+
1 row in set (0.00 sec)
mysql> select max(grade) from score
-> group by cno
-> having cno='01';
+------------+
| max(grade) |
+------------+
| 95 |
+------------+
1 row in set (0.00 sec)
mysql> select cno,count(*) from score
-> group by cno;
+-----+----------+
| cno | count(*) |
+-----+----------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 1 |
| 05 | 2 |
+-----+----------+
5 rows in set (0.00 sec)
mysql> select sno from score
-> group by sno
-> having count(cno)>=3;
+-----+
| sno |
+-----+
| 01 |
| 02 |
| 03 |
+-----+
3 rows in set (0.00 sec)
mysql> select s.sno,sname,cname,grade from
-> student s join score sc on s.sno=sc.sno
-> join course c on sc.cno=c.cno;
+-----+-------+------------+-------+
| sno | sname | cname | grade |
+-----+-------+------------+-------+
| 01 | 张三 | PHP | 95 |
| 01 | 张三 | javascript | 85 |
| 01 | 张三 | mysql | 85 |
| 01 | 张三 | linux | 95 |
| 02 | 李四 | PHP | 85 |
| 02 | 李四 | javascript | NULL |
| 02 | 李四 | mysql | 75 |
| 02 | 李四 | html5 | NULL |
| 02 | 李四 | linux | 85 |
| 03 | 李阳 | PHP | NULL |
| 03 | 李阳 | javascript | NULL |
| 03 | 李阳 | mysql | 75 |
+-----+-------+------------+-------+
12 rows in set (0.00 sec)
mysql> select s.sno,sname,cname,grade from
-> student s join score sc on s.sno=sc.sno
-> join course c on sc.cno=c.cno
-> where sc.cno='02' and grade>90;
Empty set (0.00 sec)
mysql> select s.sno,sname,cname,grade from
-> student s join score sc on s.sno=sc.sno
-> join course c on sc.cno=c.cno
-> where sc.cno='01' and grade>90;
+-----+-------+-------+-------+
| sno | sname | cname | grade |
+-----+-------+-------+-------+
| 01 | 张三 | PHP | 95 |
+-----+-------+-------+-------+
1 row in set (0.00 sec)
mysql> select sno,sname from student
-> where sno = any(select sno from score where cno='02'
-> and grade>70);
+-----+-------+
| sno | sname |
+-----+-------+
| 01 | 张三 |
+-----+-------+
1 row in set (0.00 sec)
mysql> select sno,sname from student
-> where sno = any(select sno from score where cno='01'
-> and grade>90);
+-----+-------+
| sno | sname |
+-----+-------+
| 01 | 张三 |
+-----+-------+
1 row in set (0.00 sec)
mysql> select sname from
-> student s join score sc on s.sno=sc.sno
-> join course c on sc.cno=c.cno
-> where sc.cno='01';
+-------+
| sname |
+-------+
| 张三 |
| 李四 |
| 李阳 |
+-------+
3 rows in set (0.00 sec)
mysql> select sname from student
-> where sno=any(select sno from score where cno='01');
+-------+
| sname |
+-------+
| 张三 |
| 李四 |
| 李阳 |
+-------+
3 rows in set (0.00 sec)
mysql> select s.sno,sname from
-> student s join score sc on s.sno=sc.sno
-> join course c on sc.cno=c.cno
-> where cname='mysql';
+-----+-------+
| sno | sname |
+-----+-------+
| 01 | 张三 |
| 02 | 李四 |
| 03 | 李阳 |
+-----+-------+
3 rows in set (0.02 sec)
mysql> select sno,sname from student
-> where sno=any(select sno from score
-> where cno=(select cno from course where cname='mysql'));
+-----+-------+
| sno | sname |
+-----+-------+
| 01 | 张三 |
| 02 | 李四 |
| 03 | 李阳 |
+-----+-------+
3 rows in set (0.00 sec)
mysql> notee