MYSQL数据库项目实战

#1)创建数据库
#创建学生信息管理数据库gradem。
CREATE DATABASE gradem;
use gradem;
#2)创建数据表
student表的表结构
CREATE TABLE student
(
  sno char(10) not null,
  sname varchar(8),
  ssex char(3),
  birthday datetime,
  saddress varchar(50),
  sdept char(16),
  speciality varchar(20),
  CONSTRAINT pk01 PRIMARY key (sno) 
)
 course表(课程名称表)的表结构
CREATE TABLE course
(
   cno char(5) not null,
   cname varchar(20) not null,
   CONSTRAINT pk02 PRIMARY key (cno)
)
  sc表(成绩表)的表结构
CREATE TABLE sc
(
  sno char(10) not null,
  cno char(5) not null,
  degree decimal,
  CONSTRAINT pk03 PRIMARY key (sno,cno),
  CONSTRAINT fk01 FOREIGN key (sno) REFERENCES student (sno),
  CONSTRAINT fk02 FOREIGN key (cno) REFERENCES course (cno)
)
    teacher表(教师表)的表结构
create TABLE teacher
(
  tno char(3) not null,
  tname varchar(8),
  tsex char(3),
  tbirthday date,
  tdept char(16),
  CONSTRAINT pk04 PRIMARY KEY (tno)
);
teaching表(授课表)的表结构
CREATE TABLE teaching
(
  cno char(5) not null,
  tno char(3) not null,
  cterm tinyint,
  CONSTRAINT pk05 PRIMARY KEY (cno,tno),
  CONSTRAINT fk03 FOREIGN KEY (cno) REFERENCES course (cno),
  CONSTRAINT fk04 FOREIGN KEY (tno) REFERENCES teacher(tno)
)
INSERT INTO student VALUES
(
  20050101,'李勇','男','1987-01-12','山东济南','计算机工程系','计算机应用'
);
INSERT INTO student VALUES
(
  20050201,'刘晨','女','1988-06-04','山东青岛','信息工程','电子商务'
);
INSERT INTO student VALUES
(
  20050301,'王敏','女','1989-12-23','江苏苏州','数学系','数学'
);
INSERT INTO student VALUES
(
  20050202,'张立','男','1988-08-25','河北唐山','信息工程系','电子商务'
);
INSERT INTO course (cno,cname) VALUES
(
  'C01','数据库'
);
INSERT INTO course (cno,cname) VALUES
(
  'C02','数学'
);
INSERT INTO course (cno,cname) VALUES
(
  'C03','信息系统'
);
INSERT INTO course (cno,cname) VALUES
(
  'C04','操作系统'
);
INSERT INTO sc VALUES
(
  20050101,'C01',92
);
INSERT INTO sc VALUES
(
  20050101,'C02',85
);
INSERT INTO sc VALUES
(
  20050101,'C03',88
);
INSERT INTO sc VALUES
(
  20050201,'C02',90
);
INSERT INTO sc VALUES
(
  20050201,'C03',80
);
INSERT into teacher VALUES
(
  101,'李新','男','1977-01-12','计算机工程系'
)
INSERT into teacher VALUES
(
  102,'钱军','女','1968-06-04','计算机工程系'
);
INSERT into teacher VALUES
(
  201,'王小花','女','1979-12-23','信息工程系'
);
INSERT into teacher VALUES
(
  202,'张小青','男','1968-08-25','信息工程系'
);

INSERT INTO teaching VALUES
(
 'C01',101,2
);
INSERT INTO teaching VALUES
(
 'C02',102,1
);
INSERT INTO teaching VALUES
(
 'C03',201,3
);
INSERT INTO teaching VALUES
(
 'C04',201,3
);
INSERT INTO teaching VALUES
(
 'C01',202,4
);
#① 向student表中增加“入学时间”列,其数据类型为日期时间型。
alter table student add 入学时间 datetime;
#② 将student表中的sdept字段长度改为20。
alter TABLE student MODIFY sdept char(20);
#③ 将student表中的speciality字段删除。
ALTER TABLE student DROP COLUMN speciality;
#④ 删除student表。
DROP TABLE student;
#(5)对表进行数据的修改操作:
#① 向Student表中插入记录("20050203","张静","1981-3-21","女","CS","电子商务")。
INSERT into student (sno,sname,ssex,birthday,saddress,sdept) VALUES
(
 20050203,'张静','女','1981-3-21','CS','电子商务'
);
#② 插入学号为“20050302”、姓名为“李四”的学生信息。
INSERT into student (sno,sname) VALUES
(
 20050302,'李四'
);
#③ 将学号为“20050202”的学生姓名改为“张华”,系别改为“CS”,专业改为“多媒体技术”。
UPDATE student SET sname='张华',saddress='多媒体技术' where sno=20050202;
#④ 将“李勇”同学的专业改为“计算机信息管理”。
UPDATE student SET sdept='计算机信息管理' WHERE sname='李勇';
#(1) 查询所有学生的基本信息、所有课程的基本信息和所有学生的成绩信息(用三条SQL语句)。

SELECT * FROM student;
SELECT * FROM  course;
SELECT * from sc;

#(2) 查询所有学生的学号、姓名、性别和出生日期。
SELECT sno,sname,ssex,birthday from student;
#(3) 查询所有课程的课程名称。
SELECT cname from course;
#(4) 查询前10门课程的课号及课程名称。
SELECT cno,cname from course;
#(5) 查询所有学生的姓名及年龄。
SELECT sname 姓名,year(now())-year(birthday) as 年龄 from student;
#(6) 查询所有年龄大于18岁的女生的学号和姓名。
SELECT sno,sname from student WHERE ssex='女' and year(NOW())-year(birthday)>18;
#(7) 查询所有男生的信息。
SELECT * from student WHERE ssex='男';
#(8) 查询所有任课教师的姓名(tname)和所在系别(tdept)。
SELECT tname,tdept from teacher;
#(9) 查询“电子商务”专业的学生姓名、性别和出生日期。
SELECT sname,ssex,birthday from student WHERE sdept='电子商务';
#(10) 查询Student表中的所有系名。
SELECT saddress from student;
#(11) 查询“C01”课程的开课学期。
SELECT cterm from teaching WHERE cno='C01';
#(12) 查询成绩在80~90分之间的学生学号及课号。
SELECT sno,cno from sc WHERE degree>=80 and degree<=90;
#(13) 查询在1970年1月1日之前出生的男教师信息。
SELECT * FROM teacher WHERE  tbirthday<'1970-01-01' and tsex='男';
#(14) 输出有成绩的学生学号。
SELECT degree from sc WHERE degree>0;
#(15) 查询所有姓“刘”的学生信息。
SELECT *  from student where sname like'刘%';
#(16) 查询生源地不是山东省的学生信息。
SELECT * from student where saddress like'山东%';
#(17) 查询成绩为79分、89分或99分的记录。
SELECT * from sc where degree in(79,89,99);
#(18) 查询名字中第二个字是“小”字的男生的学生姓名和地址。
SELECT sname,saddress from student where sname like'_小';
#(19) 查询名称以“计算机”开头的课程名称。
SELECT sdept from student where sdept like'计算机%';
#(20) 查询计算机工程系和软件工程系的学生信息。 
SELECT * from student where sdept in('计算机工程系','软件工程系');
#分组和排序:
#(1) 统计有学生选修的课程的门数。
SELECT sno,count(cno) from sc GROUP BY sno;
#(2) 计算“c01”课程的平均成绩。
SELECT AVG(degree) from sc where cno='C01';
#(3) 查询选修了“c03”课程的学生的学号及其成绩,查询结果按分数降序排列。
SELECT sno,degree from sc where cno='C03' ORDER BY degree DESC;
#(4) 查询各个课程号及相应的选课人数。
SELECT course.cno,count(*) from course,sc,student where course.cno=sc.cno and student.sno=sc.sno GROUP BY course.cno;   分组; 
#(5) 统计每门课程的选课人数和最高分。
SELECT count(sc.cno),max(sc.degree) from sc,course,student where course.cno=sc.cno and student.sno=sc.sno GROUP BY sc.cno ;
#(6) 统计每个学生的选课门数和考试总成绩,并按选课门数降序排列。
SELECT count(cno),sum(degree) from sc GROUP BY sno;
#(7) 查询选修了3门以上课程的学生学号。
SELECT sno from sc GROUP BY sno having count(sno)>3;
#(8) 查询成绩不及格的学生学号及课号,并按成绩降序排列。
SELECT sno,cno from sc where degree<60 ORDER BY desc;
#(9) 查询至少选修一门课程的学生学号。
SELECT sno from sc GROUP BY sno HAVING count(sno)>=1; 
#(10) 统计输出各系学生的人数
SELECT sdept,count(sdept) from student GROUP BY sdept;
#(11) 统计各系的男、女生人数。
SELECT ssex,count(ssex) from student GROUP BY ssex;
#(12) 统计各班人数。
SELECT sdept,count(sdept) from student GROUP BY sdept;
#(13)统计各班男、女生人数。
SELECT ssex,count(ssex) from student GROUP BY ssex;
#(14)统计各系的老师人数,并按人数升序排序。
SELECT tdept,count(tdept) from teacher GROUP BY tdept;
#(15)统计不及格人数超过10人的课程号。
SELECT cno from sc GROUP BY cno HAVING count(degree<60)>10;
#(16)统计选修人数超过10人的课程号。
SELECT cno from sc GROUP BY cno HAVING count(cno)>10;
#多表连接查询:
#(1) 查询计算机工程系女学生的学生学号、姓名及考试成绩。
SELECT student.sno,student.sname,degree from student,sc where sdept='计算机工程系' and ssex='女';
#(2) 查询“李勇”同学所选课程的成绩。(不考虑重名)
SELECT cno,degree from sc where sno in(SELECT sno from student where sname='李勇');
#(3) 查询“李新”老师所授课程的课程名称。
SELECT cname from course where cno in (SELECT cno from teaching where tno in(SELECT tno from teacher where tname='李新'));
#(4) 查询女教师所授课程的课程号及课程名称。
SELECT cno,cname from course where cno in (SELECT cno from teaching where tno in (SELECT tno from teacher where tsex='女'));
#(5) 查询至少选修一门课程的女学生姓名。
SELECT sname from student where ssex='女' and  sno in(SELECT sno from sc GROUP BY sno HAVING count(cno)>=1);
#(6) 查询姓“王”的学生所学的课程名称。
SELECT cname from course where cno in(SELECT cno from sc where sno in(SELECT sno from student where sname like'王%') );
#(7) 查询选修“数据库”课程且成绩在80~90分之间的学生学号及成绩
SELECT sno,degree from sc where degree>=80 and degree<=90 and cno in(SELECT cno from course where cname='数据库');
#(8) 查询课程成绩及格的男同学的学生信息及课程号与成绩。
SELECT student.sno,sname,ssex,birthday,saddress,sdept,入学时间,cno,degree from student,sc WHERE student.sno=sc.sno and degree>60 AND ssex='男';
#(9) 查询选修“c04”课程的学生的平均年龄。
SELECT AVG(year(NOW())-year(birthday)) as 平均年龄 from student where sno in (SELECT sno from sc WHERE cno='C04');
#(10) 查询学习课程名为“数学”的学生学号和姓名。
SELECT sno,sname from student where sno in (SELECT sno from sc where cno in (SELECT cno from course where cname='数学'));
#(11) 查询“钱军”教师任课的课程号,选修其课程的学生的学号和成绩。
SELECT sno,degree from sc where cno in (SELECT cno from teaching where tno in (SELECT tno from teacher where tname='钱军'));
#(12)查询“钱军”教师任课的课程号,选修其课程的学生的姓名。
SELECT sname from student where sno in (SELECT sno from sc where cno in(SELECT cno from teaching where tno in (SELECT tno from teacher where tname='钱军')));
#(13)查询在第3学期所开课程的课程名称及成绩。
SELECT course.cname,sc.degree FROM course,sc WHERE sc.cno=course.cno and course.cno in(SELECT cno from teaching WHERE cterm=3);
#(14)查询“c02”号课程不及格的学生信息。
SELECT * FROM student where sno in(SELECT sno from sc WHERE cno='C02' and degree<60);
#(15) 查询同时选修了“c04”和“c02”课程的学生姓名和成绩。
SELECT student.sname 姓名,sc.degree 成绩 from student,sc where student.sno=sc.sno and sc.cno='C04' and sc.cno='C02';
#嵌套查询:
#(1) 查询“李勇”同学所选课程的成绩。
SELECT course.cname,sc.degree FROM sc,course where sc.cno=course.cno and sc.sno in(SELECT sno from student where sname='李勇');
#(2) 查询“李新”老师所授课程的课程名称。
SELECT cname from course where cno in(SELECT cno FROM teaching where tno in (SELECT tno FROM teacher WHERE tname='李新'));
#(3) 查询女教师所授课程的课程号及课程名称。
SELECT cno,cname from course WHERE cno in (SELECT cno FROM teaching where tno in (SELECT tno FROM teacher WHERE tsex='女'));
#(4) 查询姓“王”的学生所学的课程名称。
#SELECT cname from course where cno in(SELECT cno FROM sc where sno in (SELECT sno from student WHERE sname LIKE'王%'));
#(5) 查询选修“数据库”课程且成绩在80~90分之间的学生学号及成绩。
#SELECT sno,degree from sc where degree>=80 and degree<=90 and cno in (SELECT cno from  course WHERE cname='数据库');
#(6) 查询学习课程名为“数学”的学生学号和姓名。
SELECT sno,sname from student where sno in(SELECT sno from sc WHERE cno in(SELECT cno from course WHERE cname='数学'));
#(7) 查询选修“C04”课程的学生的平均年龄。
SELECT avg(year(NOW())-year(birthday)) 平均年龄 from student WHERE sno in(SELECT sno from sc WHERE cno='C04');
#(8) 查询在第3学期所开课程的课程名称及成绩。
SELECT course.cname,sc.degree from course,sc WHERE course.cno=sc.cno and  sc.cno in (SELECT cno from teaching WHERE cterm=3);
#(9) 查询“钱军”教师任课的课程号,选修其课程的学生的学号和成绩。
SELECT sno,degree from sc where cno in(SELECT cno from teaching where tno in (SELECT tno from teacher WHERE tname='钱军'));
#(10) 查询与“李勇”同一个系的同学姓名。
SELECT sname from student WHERE sdept in(SELECT sdept from student WHERE sname='李勇');
#(11) 查询学号比“刘晨”同学大,而出生日期比他小的学生姓名。
SELECT sname from student WHERE sno>sno in(SELECT sno from student WHERE sname='刘晨') and  birthday>birthday in(SELECT birthday from student WHERE sname='刘晨');
#(12) 查询出生日期大于所有女同学出生日期的男同学的姓名及系别。
SELECT sname,sdept from student where ssex='男' and birthday>birthday in (SELECT max(birthday) from student WHERE ssex='女');
#(13) 查询成绩比该课程平均成绩高的学生的学号及成绩。
SELECT sno,degree from sc  GROUP BY cno HAVING  degree>degree in (SELECT avg(degree) from sc);
#(14) 查询不讲授“C01”课的教师姓名。
SELECT tname from teacher WHERE  not tno in(SELECT tno from teaching WHERE cno='C01'); 
#(15) 查询“C02”号课程不及格的学生信息。
SELECT * FROM student WHERE sno in(SELECT sno FROM sc WHERE cno='C02' and degree<60);
#(16) 查询没有选修“C02”课程的学生学号及姓名。
SELECT * FROM student WHERE not sno in(SELECT sno FROM sc WHERE cno='C02');




猜你喜欢

转载自blog.csdn.net/qq_46232829/article/details/107365138