练习题目:
data 数据库中表student和score,其数据定义和表中的记录如下。*
建表:
create database choose;
use choose;
create table student(
id int(10) primary key not null unique,
name varchar(20) not null,
sex varchar(4),
birth year,
department varchar(20) not null,
address varchar(50)
);
create table score(
id int(10) primary key not null unique auto_increment,
stu_id int(10) not null,
c_name varchar(20),
grade int(10)
);
insert into student values('901','张老大','男','1985','计算机系','北京市海淀区');
insert into student values('902','张老二','男','1986','中文系','北京市昌平区');
insert into student values('903','张三','女','1990','中文系','湖南省永州市');
insert into student values('904','李四','男','1990','英语系','辽宁省阜新市');
insert into student values('905','王五','女','1991','英语系','福建省厦门市');
insert into student values('906','王六','男','1988','计算机系','湖南省衡阳市');
insert into score values('1','901','计算机','98');
insert into score values('2','901','英语','80');
insert into score values('3','902','计算机','65');
insert into score values('4','902','中文','88');
insert into score values('5','903','中文','95');
insert into score values('6','904','计算机','70');
insert into score values('7','904','英语','92');
insert into score values('8','905','英语','94');
insert into score values('9','906','计算机','90');
insert into score values('10','906','英语','85');
请完成以下数据查询:*
1. 查询student表的第2到第4条记录;
select * from student
where id between 902 and 904;
select * from student limit 1,3;
2. 查询所有学生的学号、姓名和院系的信息;
select id,name,department
from student;
3. 查询计算机系和英语系的学生信息;
select * from student
where department in('计算机系','英语系');
4. 查询湖南省的学生的姓名和院系。
select name,department from student
where address like '湖南省%';
5. 查询年龄在18-22岁的学生的院系、姓名和年龄;
select department,name,(2023-birth) from student
where (2023-birth) between 18 and 22;
6. 查询每个院系有多少人;
select department,count(department) from student group by department;
7. 查询每个科目的最高分;
select c_name,max(grade) from score group by c_name;
8. 计算每个学生的总成绩;
select a.name,a.id,sum(grade)
from student a
left join score b
on a.id=b.stu_id
group by a.id;
9. 查询每门课程的平均成绩;
select c_name,avg(grade)from score group by c_name;
10. 查询计算机课程的考试信息并将结果按成绩从高到底排序;
select b.c_name,b.grade,a.name from student a left join score b on a.id=b.stu_id where c_name='计算机' group by grade,name desc;
程序题:
有一个小型超市管理系统,其数据库supermarket中有三张基本表,简化后的结构如下:
商品表goods(gid为商品唯一编号,gname为商品名,gprice为商品价格,gnum为商品库存数量,id为商品的供应商编号)
销售表sale(sid为销售流水号,gid同商品表中gid的含义,snum为销售数量,date为日期)
供应商表supplier(id为供应商编号,name为供应商名称,tel为供应商电话)
1.写出创建数据库supermarket库的语句。
create database supermarket;
2.写出创建goods表的语句(数据类型依次为char(5),char(20),smallint,tinyint,char(4) )。
use supermarket;
create table goods(
gid char(5),
gname char(20),
gprice smallint,
gnum tinyint,
id char(4)
);
3.如果超市要新采购一种新商品(gid为g0303,商品名为“古城蚊香”,单价为8元,数量为120盒,供应商编号为1001),请写出向goods表插入该商品的语句。
insert into goods values('g0812','飘柔洗发水','20','60','1001');
insert into goods values('g0813','美赞臣奶粉','130','20','1002');
insert into goods values('g0814','飞鹤奶粉','160','36','1003');
insert into goods values('g0815','雷达蚊香','10','55','1004');
insert into goods values('g0816','Deli便签纸','3','80','1005');
insert into goods values('g0817','双汇火腿肠','13','60','1006');
insert into goods values('g0303','古城蚊香','8','120','1001');
4.查询超市中所有奶粉的平均价格。
select avg(gprice) from goods
where gname like '%奶粉';
5.查询库存数量小于5或库存数量大于100的商品的记录。
select * from goods
where gnum<5 or gnum>100;
6.将“古城蚊香”的单价改为7元。
update goods
set gprice=7
where gname='古城蚊香';
7.在goods表中删除id为“1005”供应商提供的所有商品。
delete from goods
where gid='1005';
8.查出所有商品的价格并按价格从低到高排序。
select * from goods
order by gprice asc;
9.备份supermarket到D盘(文件名为bak.sql,假设数据库无密码)
show variables like 'datadir';(查询数据库的路径)
mysqldump -u root -p1234 supermarket>D:\\bak.sql;