0
点赞
收藏
分享

微信扫一扫

MySQL小练习(仅适合初学者,非初学者勿进)

12a597c01003 2022-04-22 阅读 153

 题目:

 

 

 

 

 

1.创建数据库

create database  if not exists  db2 ;

 

 

2.创建表

1.创建学生表

drop table if exists student;
create table student
(
    id     varchar(10) comment '学号',
    name   varchar(10) NOT NULL comment '姓名',
    gender char(1) comment '性别',
    class  varchar(20) comment '专业班级',
    date   date comment '出生日期',
    iphone varchar(11) comment '联系电话'
)
    comment '学生表';

select * from student;

 

2.创建课程表

 

 

drop table if exists student_course;
create table student_course
(
    course_id     varchar(10)  comment '课程号',
    course_name   varchar(15) comment '课程名',
    course_number double unsigned comment '学分数',
    student_time  int unsigned comment '学时数',
    teacher       varchar(10) comment '任课教师'
)
    comment '课程表';
select *
from student_course;

 

3.学生作业表

 

drop table if exists student_homework;
create table student_homework
(
    course_id  varchar(10) comment '课程号',
    id      varchar(10)    comment '学号',
    homework_1 int comment '作业1成绩',
    homework_2 int comment '作业2成绩',
    homework_3 int comment '作业3成绩'

)
    comment '学生作业表';
select *
from student_homework;

 

3.添加数据

 1.学生表

insert into student
values ('0433', '张艳', '女', '生物04', '1986-9-13', null),
       ('0496', '李越', '男', '电子04', '1984-2-23', '1381290xxxx'),
       ('0529', '赵欣', '男', '会计05', '1984-1-27', '1350222xxxx'),
       ('0531', '张志国', '男', '生物05', '1986-9-10', '1331256xxxx'),
       ('0538', '于兰兰', '女', '生物05', '1984-2-20', '1331200xxxx'),
       ('0591', '王丽丽', '女', '电子05', '1984-3-20', '1332080xxxx'),
       ('0592', '王海强', '男', '电子05', '1986-11-1', null);

 查询一下:

select * from student;

 

 

2.课程表

 

INSERT INTO student_course
values ('K001', '计算机图形学', 2.5, 40, '胡晶晶'),
       ('K002', '计算机应用基础', 3, 48, '任泉'),
       ('K006', '数据结构', 4, 64, '马跃先'),
       ('M001', '政治经济学', 4, 64, '孔繁新'),
       ('S001', '高等数学', 3, 48, '赵晓尘');

查询一下:

select *
from student_course;

 

3.学生作业表

 

 

insert into student_homework values
('K001','0433',60,75,75),
('K001','0529',70,70,60),
('K001','0531',70,80,80),
('K001','0591',80,90,90),
('K002','0496',80,80,90),
('K002','0529',70,70,85),
('K002','0531',80,80,80),
('K002','0538',65,75,85),
('K002','0592',75,85,85),
('K006','0531',80,80,90),
('K006','0591',80,80,80),
('M001','0496',70,70,80),
('M001','0591',65,75,75),
('S001','0531',80,80,80),
('S001','0538',60,null,80);

查询一下:

select *
from student_homework;

 

 

4.开始做题

 

select id,class,name from student;

 

 

select *
from student_course;

 

 

 

select  class from student;

select course_id,course_name from student_course where student_time>60;

 

select id,name,date from student where date>=('1986-1-1') AND date<('1987-1-1');

 

 

 

select id,name,class from  student where name like '张%';

 

 ​​​​​​​

 

 

select * from student where class like '%05' and gender='男';

 

 

select id,course_id from student_homework where homework_1 is null or homework_2 is null or homework_3 is null ;

 ​​​​​​​

 

 

 

select sum(homework_1) '总分' from student_homework where id='0538';

 

select count(*) from student_homework where course_id='K001';

 

 

select count(*) from student where class is not null ;

 

select student.id, avg(homework_1), avg(homework_2), avg(homework_3)
from student
         left join student_homework on student.id = student_homework.id
group by student.id
having count(course_id) >= 3;

 

 

举报

相关推荐

0 条评论