新人刚刚加入csdn 有问题可以在下面留言 请多多指教
1. 创建数据库xscj,创建数据表xs,并插入数据,参考课本321页。
create database xscj CHARACTER SET gbk;
use xscj;
create table xs
(
学号 char(6) not null primary key,
姓名 char(8) not null,
专业名 char(10) null,
性别 tinyint(1) not null default 1,
出生日期 date not null,
总学分 tinyint(1) null,
照片 blob null ,
备注 text null
) engine = innodb;
insert into xs values
('081101','王林','计算机',1,'1994-02-10',50,null,null),
('081102','程明','计算机',1,'1995-02-01',50,null,null),
('081103','王燕','计算机',0,'1993-10-06',50,null,null),
('081104','韦平平','计算机',1,'1994-08-26',50,null,null),
('081106','李方方','计算机',1,'1994-11-20',50,null,null),
('081107','李明','计算机',1,'1994-05-01',54,null,'提前修完“数据结构,并获得学分”'),
('081108','林一帆','计算机',1,'1993-08-05',52,null,'已提前修完一门课'),
('081109','张强明','计算机',1,'1993-08-11',50,null,null),
('081110','张蔚','计算机',0,'1995-07-22',50,null,'三好生'),
('081111','赵琳','计算机',0,'1994-03-18',50,null,null),
('081113','严红','计算机',0,'1993-08-11',48,null,'有一门课不及格,待补考'),
('081201','王敏','通信工程',1,'1993-06-10',42,null,null),
('081202','王林','通信工程',1,'1993-01-29',40,null,'有一门课不及格,待补考'),
('081204','马琳琳','通信工程',0,'1993-01-29',42,null,null),
('081206','李计','通信工程',1,'1993-09-20',42,null,null),
('081210','李红庆','通信工程',1,'1993-05-01',44,null,'已提前修完一门课,并获得学分'),
('081216','孙祥欣','通信工程',1,'1993-03-09',42,null,null),
('081218','孙研','通信工程',1,'1994-10-09',42,null,null),
('081220','吴薇华','通信工程',0,'1994-03-18',42,null,null),
('081221','刘燕敏','通信工程',0,'1993-11-12',42,null,null),
('081241','罗林琳','通信工程',0,'1994-01-30',50,null,'转专业学习');
2. 创建表kc和xs_kc,并插入数据,参考课本321页。
create table kc
(
课程号 char(3) not null primary key,
课程名 char(16) not null,
开课学期 tinyint(1) not null default 1,
学时 tinyint(1) not null,
学分 tinyint(1) null
);
create table xs_kc
(
学号 char(6) not null,
课程号 char(3) not null,
成绩 tinyint(1) null,
学分 tinyint(1) null,
primary key(学号,课程号)
);
insert into kc values
('101','计算机基础',1,80,5),
('102','程序设计与语言',2,68,4),
('206','离散数学',4,68,4),
('208','数据结构',5,68,4),
('209','操作系统',6,68,4),
('210','计算机原理',5,85,5),
('212','数据库原理',7,68,4),
('301','计算机网络',7,51,3),
('302','软件工程',7,51,3);
insert into xs_kc values ('081101','101',80,5);
insert into xs_kc values ('081101','102',78,4);
insert into xs_kc values ('081101','206',76,4);
insert into xs_kc values ('081102','102',78,4);
insert into xs_kc values ('081102','206',78,4);
insert into xs_kc values ('081103','101',62,5);
insert into xs_kc values ('081103','102',70,4);
insert into xs_kc values ('081103','206',81,4);
insert into xs_kc values ('081104','101',90,5);
insert into xs_kc values ('081104','102',84,4);
insert into xs_kc values ('081104','206',65,4);
insert into xs_kc values ('081106','101',65,5);
insert into xs_kc values ('081106','102',71,4);
insert into xs_kc values ('081106','206',80,4);
insert into xs_kc values ('081107','101',78,5);
insert into xs_kc values ('081107','102',80,4);
insert into xs_kc values ('081107','206',68,4);
insert into xs_kc values ('081108','101',85,5);
insert into xs_kc values ('081108','102',64,4);
insert into xs_kc values ('081108','206',87,4);
insert into xs_kc values ('081109','101',66,5);
insert into xs_kc values ('081109','102',83,4);
insert into xs_kc values ('081109','206',70,4);
insert into xs_kc values ('081110','101',95,5);
insert into xs_kc values ('081110','102',90,4);
insert into xs_kc values ('081110','206',89,4);
insert into xs_kc values ('081111','101',91,5);
insert into xs_kc values ('081111','102',70,4);
insert into xs_kc values ('081111','206',76,4);
insert into xs_kc values ('081113','101',63,5);
insert into xs_kc values ('081113','102',79,4);
insert into xs_kc values ('081113','206',60,4);
insert into xs_kc values ('081201','101',80,5);
insert into xs_kc values ('081202','101',65,5);
insert into xs_kc values ('081203','101',87,5);
insert into xs_kc values ('081204','101',91,5);
insert into xs_kc values ('081210','101',76,5);
insert into xs_kc values ('081216','101',81,5);
insert into xs_kc values ('081218','101',70,5);
insert into xs_kc values ('081220','101',82,5);
insert into xs_kc values ('081221','101',76,5);
insert into xs_kc values ('081241','101',90,5);
3.查询xs表中的所有记录。
select * from xs;
4. 查询xs表中各个学生的学号、姓名、专业名和总学分。
select 学号,姓名,专业名,总学分 from xs;
5. 查询xs表中通信工程专业学生的学号学号、姓名和总学分,结果中各列的标题分别指定为number 、name和 mark。
select 学号 as number,姓名 as name,总学分 as mark
from xs
where 专业名='通信工程';
6. 查询xs表中通信工程专业学生的学号学号、姓名和总学分,结果中各列的标题分别指定为student number 、student name和 mark。
select 学号 as 'student number',姓名 as 'student name',总学分 as mark
from xs
where 专业名='通信工程';
7. 查询xs表中计算机专业各学生的学号、姓名和总学分,并对总学分按如下规则进行替换:若总分为空值,替换为“未选课”;若总学分小于50,替换为“不及格”;若总学分为50~52,替换为“合格”;若总学分大于52,替换为“优秀”。总学分列的标题更改为“等级”。
select 学号,姓名,总学分,
case
when 总学分 is null then'未选课'
when 总学分 <50 then'不及格'
when 总学分 >=50 and 总学分 <=52 then'合格'
else'优秀'
end as 等级
from xs
where 专业名='计算机';
8.查询kc 和xs_kc表中的所有记录。
select * from kc;
select * from xs_kc;
9. 查询kc表中各个课程的课程号、课程名和开课学期。
select 课程号,课程名,开课学期 from kc;
10. 查询kc表中各个课程的课程号、课程名和开课学期,结果中各列的标题分别指定为number 、name和 term。
select 课程号 as number,课程名 as name,开课学期 as term
from kc;
11. 查询kc表中各个课程的课程号、课程名和开课学期,结果中各列的标题分别指定为course number 、course name和 school term。
select 课程号 as 'course number',课程名 as 'course name',开课学期 as 'school term'
from kc;
12. 查询xs_kc表中课程号为101各学生的学号和成绩,并对成绩按如下规则进行替换:若成绩小于60,替换为“不及格”;若成绩为60-70,替换为“及格”;若成绩为70-80,替换为“中”;若成绩为80~90,替换为“良好”;若成绩大于90,替换为“优秀”。成绩列的标题更改为“成绩等级”。
select 学号,成绩,
case
when 成绩 <60 then'不及格'
when 成绩 >=60 and 成绩 <=70 then'合格'
when 成绩 >=70 and 成绩 <=80 then'中'
when 成绩 >=80 and 成绩 <=90 then'合格'
else'优秀'
end as 成绩等级
from xs_kc
where 课程号='101';