建立數據庫
1.建立一個數據庫
create database work;
2.進入數據庫work
use work;
3.數據庫默認編碼可能不支持中文,可以在這里設置下
set names gbk;
4.建立student表
屬性有:編號:id (主鍵,自動增長),姓名:sname,出生年月:sage,性別:ssex(枚舉)
create table student(sid int primary key auto_increment,
sname varchar(20),
sage date,
ssex enum(‘男’,’女’));
5.第二個課程表中使用了外鍵教師標號,因而需要先建立教師表
create table teacher(tid int primary key auto_increment,
tname varchar(20));
6.建立課程表
create table course(cid int primary key auto_increment,
cname varchar(20),
tid int,
foreign key(tid) references teacher(tid));
7.建立成績表
create table sc(sid int,
cid int,
score int);
8.show tables; //可查看建立的四個表格
9.插入數據,因為里面有主鍵鏈接,表格插入數據也要有順序(注意題目圖片上都是字節引號,應該為int,不要單引號)
a,先給student表插入數據
insert into student values(1,'趙雷','1990-01-01','男'),
(2,'錢電','1990-12-21','男'),
(3,'孫風','1990-05-20','男'),
(4,'李雲','1990-08-06','男'),
(5,'周梅','1991-12-01','女'),
(6,'吳蘭','1992-03-01','女'),
(7,'鄭竹','1989-07-01','女'),
(8,'王菊','1990-01-20','女');
b, 給teacher表插入數據,這里不可以先給course表插入數據,因為course表外鏈接到teacher的主鍵
insert into teacher values(1,'張三'),
(2,'李四'),
(3,'王五');
c, 給course表插入數據
insert into course values(1,'語文',2),
(2,'語文',1),
(3,'語文',3);
d, 最后給sc表插入數據(題目圖片少了第1個學生成績,在這加上 1,1,90; 1,2,80; 1,3,90)
insert into sc values(1,1,90),
(1,2,80),
(1,3,90),
(2,1,70),
(2,2,60),
(2,3,80),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,20),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);
———————–數據庫建立完成—————————————
1、查詢”01”課程比”02”課程成績高的學生的信息及課程分數
select s.sid,s.sname,s.sage,s.ssex,sc1.score,sc2.score from student s,sc sc1,sc sc2 where sc1.cid=1 and sc2.cid=2 and sc1.score>sc2.score and sc1.sid=sc2.sid and s.sid=sc1.sid;
2、查詢同時存在”01”課程和”02”課程的情況
select * from course c1,course c2;
3.查詢平均成績大於等於60分的同學的學生編號和學生姓名和平均成績
select s.sid,s.sname,avg(score) from student s,sc group by s.sid having avg(score)>60;
4、查詢名字中含有”風”字的學生信息
select * from student where sname like ‘%風%’;
5、查詢課程名稱為”數學”,且分數低於60的學生姓名和分數
select s.sname,score from student s,sc where s.sid=sc.sid and cid=2 and score<60;
6、查詢所有學生的課程及分數情況;
select cname,score from sc,course where sc.cid=course.cid;
7、查詢沒學過”張三”老師授課的同學的信息
select s.* from student s where s.sid not in(select sc1.sid from sc sc1,course c,teacher t where t.tid=c.tid and sc1.cid=c.cid and t.tname=’張三’);
8.查詢學過”張三”老師授課的同學的信息
select s.* from student s ,sc sc1,course c,teacher t where s.sid=sc1.sid and sc1.cid=c.cid and c.tid=t.tid and t.tname=’張三’;
9、查詢學過編號為”01”並且也學過編號為”02”的課程的同學的信息
student(sid) sc(sid cid tid) sc2(sid cid tid) course(cid tid cname)
select s.* from student s,sc sc1,sc sc2 where s.sid=sc1.sid and sc1.sid=sc2.sid and sc1.cid=1 and sc2.cid=2;
10、查詢學過編號為”01”但是沒有學過編號為”02”的課程的同學的信息
select distinct s.* from student s,sc sc1,sc sc2,sc sc3 where s.sid=sc1.sid and sc1.sid=sc2.sid and sc1.cid=1 and sc2.cid!=2;
11、查詢沒有學全所有課程的同學的信息
select s.* from student s where s.sid not in(select sc1.sid from sc sc1,sc sc2,sc sc3 where sc1.cid=1 and sc2.cid=2 and sc3.cid =3 and sc1.sid=sc2.sid and sc1.sid=sc3.sid) group by s.sid;
12、查詢至少有一門課與學號為”01”的同學所學相同的同學的信息
select distinct s.* from student s,sc sc1 where s.sid=sc1.sid and sc1.cid in(select cid from sc where sid=1) and s.sid<> 1;
13、查詢和”01”號的同學學習的課程完全相同的其他同學的信息
select s.* from student s where s.sid in(select distinct sc.sid from sc where sid<>1 and sc.cid in(select distinct cid from sc where sid=1)group by sc.sid having count(1)=(select count(1) from sc where s.sid=1));
14、查詢沒學過”張三”老師講授的任一門課程的學生姓名
select s.* from student s where s.sid not in(select sc1.sid from sc sc1,course c,teacher t where sc1.cid=c.cid and c.tid=t.tid and t.tname=’張三’);
15、查詢出只有兩門課程的全部學生的學號和姓名
select s.* from student s,sc group by sc.sid having count(sc.sid)=2 and s.sid=sc.sid;
16、查詢1990年出生的學生名單(注:Student表中Sage列的類型是datetime)
select * from student where sage>=’1900-01-01’ and sage<=’1900-12-31’;
select s.* from student s where s.sage like ‘1900-%’;(方法2)
17、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select sc.cid,avg(score) from sc group by sc.cid order by avg(score) DESC , sc.cid;
18、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
select s.sname,c.cname,score from student s,sc,course c where s.sid=sc.sid and sc.cid=c.cid and score>70;
19、查詢平均成績大於等於85的所有學生的學號、姓名和平均成績
select s.sname,avg(score) from sc,student s where s.sid=sc.sid group by sc.sid having avg(score)>=85;
20、查詢不及格的課程
select s.sname,c.cname,score from student s,sc,course c where s.sid=sc.sid and sc.cid=c.cid and score<60;
21、查詢課程編號為01且課程成績在80分以上的學生的學號和姓名;
select s.sid,s.sname from student s,sc where sc.sid=s.sid and sc.cid=1 and score>80;
22、求每門課程的學生人數
select cid,count(sid) from sc group by sc.cid;
23、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
select cid,count(sid) from sc group by cid having count(sid)>5 order by count(sid),cid ASC;
24、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
select s1.sid,s2.sid,sc1.cid,sc1.score,sc2.score from student s1,student s2,sc sc1,sc sc2 where s1.sid!=s2.sid and s1.sid=sc1.sid and s2.sid=sc2.sid and sc1.cid!=sc2.cid and sc1.score=sc2.score;
25、檢索至少選修兩門課程的學生學號
select sid from sc group by sid having count(cid)>=2;
26、查詢選修了全部課程的學生信息
select s.* from sc,student s where s.sid=sc.sid group by sid having count
(cid)=3;
27、查詢各學生的年齡
select s.sname,(TO_DAYS(‘2017-09-07’)-TO_DAYS(s.sage))/365 as age from student s;
28、查詢本月過生日的學生
select s.sname from student s where s.sage like ‘_____07%’;
29、查詢下月過生日的學生
select s.sname from student s where s.sage like ‘_____08%’;
30、查詢學全所有課程的同學的信息
select s.* from student s,sc sc1,sc sc2,sc sc3 where sc1.cid=1 and sc2.cid=2 and sc3.cid=3 and sc1.sid=sc2.sid and sc1.sid=sc3.cid and s.sid =sc1.sid group by s.sid;