1. 程式人生 > >數據庫簡單練習 建表+select

數據庫簡單練習 建表+select

ref order by date 存在 char 數字 insert form avg

create table student (
sno int primary key,
sname char(20),
sex char(2),
birthday datetime,
class int
)
create table teacher (
tno int primary key,
tname char(20),
sex char(2),
birthday datetime,
prof char(10),
depart char(20)
)
create table course (
cno char(20) primary key,
cname char(20),
tno int foreign key references teacher(tno)
)
create table score (
sno int foreign key references student(sno),
cno char(20) foreign key references course(cno),
degree int
)
insert into student values(108,‘曾華‘,‘男‘,‘09/01/1977‘,95033);
insert into student values(105,‘匡明‘,‘男‘,‘10/02/1975‘,95031);
insert into student values(107,‘王麗‘,‘女‘,‘01/23/1976‘,95033);
insert into student values(101,‘李軍‘,‘男‘,‘02/20/1976‘,95033);
insert into student values(109,‘王芳‘,‘女‘,‘02/10/1975‘,95031);
insert into student values(103,‘陸軍‘,‘男‘,‘06/03/1974‘,95031); insert into teacher values(804,‘李誠‘,‘男‘,‘12/02/1958‘,‘副教授‘,‘計算機系‘); insert into teacher values(856,‘李旭‘,‘男‘,‘03/12/1969‘,‘講師‘,‘電子工程系‘); insert into teacher values(825,‘王萍‘,‘女‘,‘05/05/1972‘,‘助教‘,‘計算機系‘);

insert into teacher values(831,‘劉冰‘,‘女‘,‘08/14/1977‘,‘助教‘,‘電子工程系‘);

insert into course values(‘3-105‘,‘計算機導論‘,825); insert into course values(‘3-245‘,‘操作系統‘,804); insert into course values(‘6-166‘,‘數字電路‘,856); insert into course values(‘9-888‘,‘高等數學‘,825);
insert into score values(103,‘3-245‘,86);
insert into score values(109,‘3-245‘,68); insert into score values(105,‘3-245‘,75); insert into score values(103,‘3-105‘,92);

insert into score values(105,‘3-105‘,88);

insert into score values(109,‘3-105‘,76); insert into score values(101,‘3-105‘,64); insert into score values(107,‘3-105‘,91); insert into score values(108,‘3-105‘,78); insert into score values(101,‘6-166‘,85); insert into score values(107,‘6-166‘,79); insert into score values(108,‘6-166‘,81 ); drop table student
drop table teacher
drop table course
drop table score
--1、列出student表中所有記錄的
--sname、sex和class列。
select sname,sex,class from student
--2、顯示教師所有的單位即不重復的depart列。 --select distinct depart from teacher
--3、顯示學生表的所有記錄。 --select * from student --4.顯示score表中成績在60到80之間的所有記錄
--select * from score where degree>=60 and degree<=80 --5.顯示score表中成績在為85,86,88的所有記錄
--select * from score where degree in (85,86,88) --6.顯示 表中 班或性別為女的同學的記錄
--select *from student where class=‘95033‘and sex=‘女‘ --7.以class降序顯示 表中所有記錄
-- select * from student order by class desc --8.以cno升序 degree降序顯示 表中所有記錄
-- select * from student order by cno asc,degree desc --9.顯示 班的學生人數
--select COUNT(*),COUNT(sno)from student where class=‘95033‘ --10.顯示score表中的最高分的學生學號和課程號
--select max(degree),nim(degree),avg(degree) from score
--select * from score where degree=(select max(degree) from score) --11.顯示“3-105”號課程的平均分 --12.顯示score表中至少有5名學生選修並以3開頭的課程號的平均分數
--select cno,count(*),AVG(degree) from score where cno like ‘3%‘ group by cno having count(cno)>=5 --13.顯示最低分大於70,最高分小於90的sno列
--select sno from score group by sno having max(degree)<90 and min(degree)>70
--select sno from score where max(degree)<90 and min(degree)>70 --14.顯示所有學生的sname、cno和degree列
--select sname、cno、degree from score,student where student.sno=score.sno
--select sname、cno、degree from score join student on student.sno=score.sno --15.顯示所有學生的 sname,cname 和degree
--select sname,cname,DEGREE from student,score,course where student.sno=score.sno and score.cno=course.cno
--select sname,cname,DEGREE from student join score on student.sno=score.sno join course on score.cno=course.cno --16.列出“95033”班所選課程的平均分
--select avg(degree) from score where sno in (select sno from student where class=‘95033‘) --17.顯示選修"3-105"課程的成績高於“109”號同學成績的所有同學的記錄
--select * from score where degree>(select degree from score where sno=109 and cno=‘3-105‘) and cno =‘3-105‘ --18.顯示score中選修多門課程的同學中分數為非最高分成績的記錄 select * from score where sno in (select sno from score group by sno having count(sno)>1 ) and degree not in (select max(degree) from score group by cno) --20.顯示出和學號“108”號同學同年出生的所有學生的sno、sname、birthday列
--select sno,sname,birthday from student where day(birthday)= (select day(birthday) from student where sno=108) --21.顯示“張旭”老師上課的學生的成績
--select * from score where cno=(select cno form course where tno=(select tno from teacher where tname=‘XXX‘))
--select tracher.* ,course.* ,score.* from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno and tname=‘XXX‘
--select tracher.* ,course.* ,score.* from teacher join course on teacher.tno=course.tno join score on course.cno=score.cno where tname=‘XXX‘
--22.顯示選修某課程的同學人數多余5人的老師姓名
--select tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having count(*)>=5)) --23.顯示“95033”班和“95031”班全體學生的記錄
--select * from student where class =‘95033‘ or class=‘95031‘ --24.顯示存在有85分以上成績的課程編號
--select distindt cno from score where degree>85
-- 相同的顯示一次 --25.顯示“計算機系”老師所教課程的成績表
--select * from score where cno in(select cno from course where tno in(select tno from teacher where depart =‘計算機系‘)) --27.顯示選修編號為“3-105”課程且成績至少高於“3-245”課程的同學的cno、sno和degree,並按degree從高到低次序排列
--select cno,sno,degree from score where cno=‘3-105‘ and degree>any(select degree from c=score where cno=‘2_245‘)
--select cno,sno,degree from score where cno=‘3-105‘ and degree>(select min(degree) from c=score where cno=‘2_245‘) --28.
--29、列出所有任課老師的tname和depart
--select tname,depart from teacher where tno in(select tno from course) --30、列出所有未講課老師的tname和depart
--select tname,depart from teacher where tno not in(select tno from course) --31、列出所有老師和同學的 姓名、性別和生日。
--select sname,sex,birthday from student union select tname,sex,birthday from teacher --*32、檢索所學課程包含學生“103”所學課程的學生學號
--select distinct sno from score x where not exists(select * from score y where y.sno=103 and not exists(select * from score z where z.sno=x.sno and z.cno=y.cno)) --*33、檢索選修所有課程的學生姓名
--select sname from student where sno in(select sno from score group by sno having count(*)= (select count(*) from course)) 不一定都對,有錯歡迎指正

數據庫簡單練習 建表+select