数据库练习题2

这里写图片描述

drop database if exists SRS;
create database SRS default charset utf8;
use SRS;

-- 创建学生表
drop table if exists tbstudent;
create table tbstudent
(
stuid integer not null,
stuname varchar(20) not null,
stusex bit default 1,
stubirth datetime not null,
stutel char(11),
stuaddr varchar(255),
stuphoto longblob,
primary key (stuid)
);
-- 修改学生表,删除stutel列
alter table tbstudent drop column stutel;
-- 查看表结构
desc tbstudent;

-- 创建课程表
drop table if exists tbcourse;
create table tbcourse
(
cosid integer not null,
cosname varchar(50) not null,
coscredit tinyint not null,
cosintro varchar(255)
);
-- 给课程表指定主键
alter table tbcourse add constraint pk_course primary key(cosid);

-- 创建学生选课记录表
drop table if exists tbsc;
create table tbsc
(
scid integer primary key auto_increment,
sid int not null,
cid int,
scdate datetime not null,
score float
);
-- 给表tbsc添加外键约束
alter table tbsc add constraint fk_sid foreign key (sid) references tbstudent(stuid) on delete cascade on update cascade;
alter table tbsc add constraint fk_cid foreign key (cid) references tbcourse(cosid) on delete set null on update cascade;

-- 添加学生记录
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);
select * from tbstudent;

-- 删除学生记录
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 * from tbcourse;
3.select stuname,stubirth from tbstudent where stusex=0;
4.select stuname, stusex ,stubirth from tbstudent where stubirth between '1980-1-1' and'1989-12-31';
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,stusex from tbstudent where stuname like '%王%';
9.select stuname from tbstudent where stuaddr is null and stuphoto is null;
10.select distinct scdate datetime from tbsc;
11.select stuname,stubirth from tbstudent ORDER BY(stubirth);
12.select stuname,stuaddr,stubirth from tbstudent where stuaddr is not null and stusex='1' ORDER BY(stubirth);
13.select stuname,stubirth from tbstudent s where s.stubirth =(select min(stubirth) from tbstudent);
14.select stuname,stubirth from tbstudent s where s.stubirth =(select max(stubirth) from tbstudent);
15.select if(stusex, '男', '女') as 性别, count(stusex) as 人数 from tbstudent group by stusex;
16.select avg(score) from tbsc where cid=1111;
17.select sum(score) from tbsc where sid=1001;
18.select sid as 学号, ifnull(avg(score), 0) as 平均成绩 from tbsc group by sid;
19.select sid as 学号,avg(score) as 平均成绩 from tbsc group by sid  having avg(score)>=90;
20.select stuname from tbstudent s where s.stubirth =(select min(stubirth) from tbstudent);
21.select stuname from tbstudent where stuid in (select sid from tbsc group by sid having count(*) >2);
22.select stuname,avgscore from tbstudent t1 join (select sid,avg(score) as avgscore from tbsc where score is not null group by sid) t2 where t1.stuid=t2.sid;
23.select stuname, cosname, score from  tbstudent t1, tbcourse t2, tbsc t3 where t1.stuid=t3.sid and t2.cosid=t3.cid and t3.score is not null;
24.select stuname as 姓名, ifnull(coscount, 0) as 选课数 from tbstudent t1 left  join (select sid, count(sid) as coscount from tbsc group by sid) t2 on t1.stuid=t2.sid;

猜你喜欢

转载自blog.csdn.net/qq_41768400/article/details/79987215