当前位置:首页 > mysql查询语句综合实例
查询综合实例
? 假设学生选课数据库有三个表即学生表S、课程表C和学生选课表SC,它们的结构如下所示,请根据所给的每种功能写出相应的查询语句。 S(S# ,SN ,SEX ,AGE ,DEPT) C(C# ,CN)
SC(S# ,C# ,GRADE)
其中:S#为学号,SN为姓名,SEX为性别,AGE为年龄,DEPT为系别,C#为课程号,CN为课程名,GRADE为课程成绩。
1、查询所有姓王的学生的姓名和性别。 2、统计学生选课数据库中开出的课程总数。
3、查询每个学生选修每门课程的有关课程数据(姓名、课程名和成绩等) 。 4、从学生选课库中查询出被2名以上(含2名)学生选修的所有课程信息。。
5、从学生选课库中查询出最多选修了1门课(含未选任何课程)的全部学生信息。 6、查询所有与“张鲁”同一性别的学生姓名、年龄和性别(假设库中只有一个学生的姓名为“张鲁”)。
7、从学生选课库中查询出每门课程被选修的学生人数,并按所选人数的降序排列出课程号和选课人数。
答案:
1.建表脚本:
/*==============================================================*/ /* DBMS name: MySQL 5.0 */ /* Created on: 2013-11-04 15:14:23 */ /*==============================================================*/
drop table if exists course;
drop table if exists sc;
drop index Index_2 on student;
drop table if exists student;
/*==============================================================*/ /* Table: course */ /*==============================================================*/ create table course (
couid int not null auto_increment, cno varchar(10) not null, cname varchar(30) not null, primary key (couid) );
/*==============================================================*/ /* Table: sc */ /*==============================================================*/ create table sc (
sid int not null auto_increment,
stuid int, couid int,
grade varchar(10) not null, primary key (sid) );
/*==============================================================*/ /* Table: student */ /*==============================================================*/ create table student (
stuid int not null auto_increment, stuno varchar(10) not null, stuname varchar(10) not null, sex varchar(10) not null, age int,
dept varchar(20), primary key (stuid) );
/*==============================================================*/ /* Index: Index_2 */ /*==============================================================*/ create unique index Index_2 on student (
stuname );
alter table sc add constraint FK_c_sc foreign key (couid)
references course (couid) on delete restrict on update restrict;
alter table sc add constraint FK_s_sc foreign key (stuid)
references student (stuid) on delete restrict on update restrict;
共分享92篇相关文档