MySQL建表查询

2022-08-06

现在有一教学管理系统,具体的关系模式如下:

Student (no, name, sex, birthday, class)

学号,姓名,性别,生日,班级

Teacher (no, name, sex, birthday, prof, depart)

教师号,姓名,性别,生日,职称,院系

Course (cno, cname, tno)

课程号,课程名,教师编号

Score (no, cno, degree)

学号,课程号,成绩

 

1.写出上述表的建表语句。

create table student (

no varchar(10) primary key,

name varchar(20),

sex varcahr(5),

birthday datetime,

class varchar(10)

);

create table teacher(

no varchar(10) primary key,

name varchar(20),

sex varchar(5),

birthday datetime,

prof varcahr(10),

depart varcahr(10)

);

creat table course(

cno varchae(10),

canme varcahr(20),

tno varcahr(10)

);

creat table score(

no varcahr(10),

cno varchar(10),

degree int

);

2.给出相应的INSERT语句来完成题中给出数据的插入。

再插入时,遇到汉字会乱码无法执行。就算插入后也可能会出现乱码。究其原因是编码不对,使用MySQL需要utf-8(国际)编码,然而自带的cmd为gbk(汉字)编码。自然会出现乱码情况。

可以先输入以下可临时解决问题,但在重启后问题又会回来。若需要永久解决建议另寻办法。

set character_set_client=gbk;set character_set_results=gbk;

 

insert student values ('107','杨康','男','1987-9-24','95001');

3.以class降序输出student的所有记录(student表全部属性)

select * from student order by class desc;

4.列出教师所在的单位depart(不重复)。

select distinct depart from teacher;

5.列出student表中所有记录的name、sex和class列

select name,sex,class from student;

6.输出student中不姓王的同学的姓名。

select name from student where name not like '王%';

7.输出成绩为85或86或88或在60-80之间的记录(no,cno,degree)

select no,cno,degree from score where degree in (85,86,88) or degree between 60 and 80;

8.输出班级为95001或性别为‘女’ 的同学(student表全部属性)

select * from student where class='95001' or sex='女';

9.以cno升序、degree降序输出score的所有记录。(score表全部属性)

select * from score order by cno asc,degree desc;

10.输出男生人数及这些男生分布在多少个班级中       

select count(*) '男生人数',count(DISTINCT class)'班级' from student where sex='男';

11.列出存在有85分以上成绩的课程编号。

select distinct cno from score where degree>85;

12.输出95001班级的学生人数

select count(*) '人数' from student where class='95001';

13.输出‘3-105’号课程的平均分 

select avg(degree)'平均分' from score where cno='3-105';

14.输出student中最大和最小的birthday日期值

select max(birthday)'年龄最小', min(birthday)'年龄最大' from student;

15.显示95001和95004班全体学生的全部个人信息(不包括选课)。(student表全部属性)

select * from student where class='95001' or class='95004';

16.输出至少有5个同学选修的并以3开头的课程的课程号,课程平均分,课程最高分,课程最低分。

select cno, avg(degree),max(degree),min(degree) from score where cno like '3%' group by cno having count(cno)>=5;

 

 

本文地址:https://blog.csdn.net/weixin_44576890/article/details/107301672