SQL语句练习一/学生选课系统查询

学生选课系统

1.创建学生选课系统

2.切换数据库

3.创建学生表 tbstudent

主键stuid ,姓名stuname,性别stusex,生日stubirth,电话stutel,住址stuaddr,照片shuphoto(以二进制存)

4.创建课程表tbcourse

主键cosid,班级名称cosname,学分coscredit,课程描述cosintro

5.学生选课记录表tbsc

主键scid,学生外键sid,班级外键cid,创建日期scdate,分数score

-----------------------------------------------------------------------------------------------------------------------------

查看MySQL服务器所有数据库

show databases;

删除数据库srs

drop database if exists srs;

创建学生选课系统srs数据库并制定默认字符集

create database srs default charset urf8;

切换至srs数据库

use srs;

查看当前数据库中所有表

show tables;

创建学生表

create table tbstudent (stuid int primary key auto_increment, 
stuname varchar(10) not null, stusex int(1) default 0, stubirth date not NULL

, stutel varchar(11) not null, stuaddr varchar(10), stuphoto blob);

    修改表中stuaddr的结构

    alter table tbstudent change stuaddr stuaddr varchar(50);

    修改学生表删除stutel列

    alter table tbstudent drop stutel;

创建课程表

create table tbcourse (cosid int primary key auto_increment, cosname varchar(10)
, coscredit int(3), cosintro text);

创建学生选课记录表

create table tbsc (scid int primary key auto_increment, sid int not null, cid 
int not null, scdate date, score decimal(3,1),foreign key(sid) references tbstudent(stuid)

, foreign key(cid) references tbcourse(cosid))

多种方式添加学生记录:

insert into tbstudent values(1001,'张三丰', default,'1978-1-1','成都市一环路西二段17号',
null);
insert into tbstudent (stuid,stuname,stubirth) values (1002, '郭靖', '1980-2-2');
insert into tbstudent (stuid, stuname,stusex,stubirth, stuaddr) values(1003,
'黄蓉', 0, '1982-3-3', '成都市二环路南四段123号');
insert into tbstudent values (1004,'张无忌', 1,'1990-4-4', null,null);
insert into tbstudent values
(1005,'丘处机', 1, '1983-5-5','北京市海淀区宝盛北里西区28号',null),
(1006,'王处一', 1, '1985-6-6', '深圳市宝安区宝安大道5010号', null),
(1007,'刘处玄', 1, '1987-7-7', '郑州市金水区纬五路21号', null),
(1008,'孙不二', 0, '1989-8-8', '武汉市光谷大道61号', null),
(1009, '平一指', 1, '1992-9-9', '西安市雁塔区高新六路52号', null),
(1010, '老不死', 1, '1993-10-10', '广州市天河区元岗路310号', null),
(1011, '王大锤', 0, '1994-11-11', null, null),
(1012, '隔壁老王', 1, '1995-12-12', null, null),

(1013, '郭啸天', 1, '1977-10-25', null, null);

删除学生记录

delete from tbstudent where stuid=1004;

更新学生记录

update tbstudent set stubirth='1980-12-12',

stuaddr='上海市宝山区同济支路199号' where stuid=1002;

添加课程记录

insert into tbcourse values(1111,'C语言程序设计',3,'大神级讲师授课需要抢座'),
(2222, 'java程序设计', 3, null),
(3333, '数据库盖伦', 2, null),

(4444, '操作系统原理', 4, null);

添加学生选课记录

insert into tbsc VALUES
(default, 1001, 1111, '2016-9-1', 95),
(default, 1002, 1111, '2016-9-1', 94),
(default, 1001, 2222, now(), null),
(default, 1001, 3333, '2017-3-1', 85),
(default, 1001, 4444, now(), null),
(default, 1002, 4444, now(), null),
(default, 1003, 2222, now(), null),
(default, 1003, 3333, now(), null),
(default, 1005, 2222, now(), null),
(default, 1006, 1111, now(), null),
(default, 1006, 2222, '2017-3-1', 80),
(default, 1006, 3333, now(), null),
(default, 1006, 4444, now(), null),
(default, 1007, 1111, '2016-9-1', null),
(default, 1007, 3333, now(), null),
(default, 1007, 4444, now(), null),
(default, 1008, 2222, now(), null),

(default, 1010, 1111, now(), null);

-----------------------------------------以上为建表语句----------------------------------------------------------

1.查询所有学生信息

select * from tbstudent;

2.查询所有课程名称及学分

select cosname,coscredit from tbcourse;

3.查询所有女学生的姓名和出生日期

select stuname,stusex,stubirth from tbstudent where stusex=0;

4.查询所有80后学生姓名,性别和出生日期

select stuname,stusex,stubirth from tbstudent where stubirth < '1990-01-01' and stubirth >='1980-01-01';

5.查询姓王的学生姓名和性别.

select stuname,stusex from tbstudent where stuname like '王%';

6.查询姓郭的总共两个字的学生的姓名.

select stuname,stusex from tbstudent where stuname like '郭_';

7.查询姓郭的总共三个字的学生的姓名.

select stuname,stusex from tbstudent where stuname like '郭__';

8.查询名字中没有王字的学生的姓名

select stuname from tbstudent where stuname not like '王%';

9.查询没有录入家庭住址和照片的学生姓名.

select stuname from tbstudent where stuaddr is null and stuphoto is null;

10.查询学生选课的所有日期.

select distinct scdate from tbsc ;

11.查询学生姓名和生日按年龄从大到小排列

select stuname, stubirth from tbstudent order by stubirth asc;

12.查询所有录入了家庭住址的男学生的姓名,出生日期和家庭住址按年龄从小到大排列.

select stuname, stubirth, stuaddr from tbstudent WHERE
stusex=1 and stuaddr is not null order by stubirth;

13.查询年龄最大的学生的出生日期.

select s1.stubirth, s1.stuname from
(select min(stubirth) m from tbstudent) s
join tbstudent s1 on s1.stubirth = s.m;

14.查询年龄最小的学生的出生日期.

select s1.stubirth, s1.stuname from
(select max(stubirth) m from tbstudent) s
join tbstudent s1 on s1.stubirth = s.m;

15.查询男女学生的人数.

select stusex as '性别', count(*) from tbstudent  group by stusex;

16.查询课程编号为1111的课程的平均成绩.

select avg(score) from tbsc where cid=1111;

17.查询学号为1001的学生所有课程的总成绩

select sum(score) from tbsc where sid=1001;

18.查询每个学生的学号和平均成绩,null值处理为0

select sid,ifnull(avg(score),0) from tbsc group by sid;

19.查询平均成绩大于等于90分的学生的学好和平均成绩.

select t.sid, t.a from
(select sid,avg(score) a from tbsc group by sid) t
 where t.a>=90; 

20.查询年龄最大的学生的姓名.

select t1.stuname, t1.stubirth from
(select min(stubirth) m from tbstudent) t
join tbstudent t1 on t.m=t1.stubirth;

21.查询选了两门以上的课程的学生姓名

select t2.stuname, t1.x as '课程数目' from
(select count(*) x,sid  from tbsc group by sid) t1
join tbstudent t2 on t1.x >= 2 and t1.sid=t2.stuid;

22.查询选课学生的姓名和平均成绩

select t2.stuname, t1.x as '平均成绩' from
(select avg(score) x,sid  from tbsc group by sid) t1
join tbstudent t2 on t1.sid=t2.stuid;

23.查询学生姓名,所选课程名称和成绩

select t1.stuname, t2.cosname, t3.score from tbstudent 
t1 join tbsc t3 on t3.sid=t1.stuid join tbcourse t2 ON
t2.cosid=t3.cid order by t1.stuid;

24.查询每个学生的姓名和选课数量

select t2.stuname,ifnull(t1.c,0) as '课程数目'from
(select sid,count(*) c from tbsc group by sid) t1 right
join tbstudent t2 on t2.stuid=t1.sid;

你可能感兴趣的:(SQL)