数据库练习

默认 3个月前 散逸
浏览:185 2

【例4.1】查询OnLineDB数据库中GoodsType(商品类别表)中所有的商品类别信息。
USE OnLineDB;
SELECT * FROM GoodsType;

【例4.2】查询Goods(商品信息表)中所有的商品编号,名称,价格和销售数量。
SELECT gdCode,gdName,gdPrice,gdSaleQty
FROM Goods;

【例4.2】查询Goods(商品信息表)中所有的商品编号,名称,价格和销售数量。
SELECT gdCode,gdName,gdPrice,gdSaleQty
FROM Goods;

【例4.4】查询Goods中每件商品的销售总价,其中销售总价=销售数量*价格,显示商品名称和销售总价
SELECT gdName,gdSaleQty*gdPrice
FROM Goods;

【例4.5】查询Users(用户信息表)中,用户名和年龄。
SELECT uName, year(now())-year(uBirth)
FROM Users;

【例4.6】查询Goods表中的商品名称,价格和所处城市,结果集中各列的标题指定为商品名,价格,城市。
SELECT gdName as 商品名,gdPrice as 价格,gdCity as 城市
FROM Goods;

【例4.8】查询Users表中uID为8的用户姓名。
SELECT uID,uName
FROM Users
WHERE uID=8;

【例4.9】查询Users表中2000年后出生的用户,显示姓名,电话号码,出生年月。
SELECT uID,uName,uPhone
FROM Users
WHERE uBirth>=’2000-1-1’;

【例4.10】查询Users表中2000年后出生的用户,且性别为“男”的用户姓名,电话号码,出生年月。
SELECT uID,uName,uPhone
FROM Users
WHERE uBirth>=’2000-1-1 ' AND uSex='男';

【例4.12】查询Goods表中gdPriced不大于50的商品名称。
SELECT gdName,gdPrice
FROM goods
WHERE NOT(gdPrice>=50) ;

【例4.13】查询Goods表中gdCity值为“长沙或“西安”,且gdPriced小于等于50的商品名称。
SELECT gdName,gdPrice ,gdCity
FROM goods
WHERE gdCity='长沙' AND gdCity='西安' OR gdPrice<=50; 【例4.14】查询Goods表中gdPriced在100到500元的商品名称。 SELECT gdName,gdPrice FROM goods WHERE gdPrice BETWEEN 100 AND 500 ; 【例4.15】查询Goods表中gdCity为长沙、西安、上海三个城市的商品名称。 SELECT gdName,gdCity FROM goods WHERE gdCity in ('长沙','西安','上海'); 【例4.17】查询Users表中gdName第2个字为“湘”的用户姓名、性别和手机号。 SELECT uName,uSex,uPhone FROM users WHERE uName LIKE '_湘%’ ; 【例4.16】查询Users表中gdName为“李”开头的用户姓名、性别和手机号。 SELECT uName,uSex,uPhone FROM users WHERE uName LIKE '李%' ; 【例4.19】查询Goods表中gdName以“华为P9_”开头的gdCode。 SELECT gdCode,gdName,gdPrice FROM goods WHERE gdName LIKE '华为P9\_%' ; SELECT gdCode,gdName,gdPrice FROM goods WHERE gdName LIKE '华为P9|_%' ESCAPE'|' ; 【例4.15】查询Goods表中gdCity为长沙、西安、上海三个城市的商品名称。 SELECT gdName,gdCity FROM goods WHERE gdCity in ('长沙','西安','上海'); 【例4.20】查询Users表中uPhone以“5”结尾用户的姓名,性别和电话。 SELECT uName,uSex,uPhone FROM users WHERE uPhone REGEXP '5$' ; 【例4.21】查询Users表中uPhone以“16,17,18”开头用户的姓名,性别和电话。 SELECT uName,uSex,uPhone FROM users WHERE uPhone REGEXP '^1[678]' ; 【例4.22】查询Users表中uImage为空的用户姓名和性别。 SELECT uName,uSex,uImage FROM users WHERE uImage IS NULL; 【例4.23】查询Goods表中gdPrice大于200的商品来源哪些城市。 SELECT DISTINCT gdCity FROM goods WHERE gdPrice>200;

【例4.24】查询Goods表tID为1的商品编号、名称和价格,并按价格升序排列。
SELECT gdCode,gdName,gdPrice
FROM goods
WHERE tID=1
ORDER BY gdPrice;

【例4.25】查询Goods表tID为1的商品编号、名称、价格和销售量,并先按销售量降序,再价格升序排列
SELECT gdCode,gdName,gdSaleQty,gdPrice
FROM goods
WHERE tID=1
ORDER BY gdSaleQty DESC,gdPrice;

【例4.26】查询Goods表前3行记录的商品编号、名称和价格。
SELECT gdCode,gdName,gdPrice
FROM goods
LIMIT 3;

【例4.28】查询Goods表,统计所有商品的总销售量。
SELECT SUM(gdSaleQty) FROM goods;

【例4.29】查询Goods表,显示商品的最高价格。
SELECT MAX(gdPrice) FROM goods;

【例4.31】查询Orders表,显示购买过商品的用户人数。
SELECT COUNT(DISTINCT uID) FROM orders;

【例4.32】查询Users表,按uCity列进行分组。
SELECT uID,uName,uSex,uCity
FROM users
GROUP BY uCity;

【例4.33】查询Users表,统计各城市的用户人数。
SELECT uCity,COUNT(*)
FROM users
GROUP BY uCity;

【例4.34】查询Users表,将同一城市的uID值用逗号“,”连接起来,列名为uIDs。
SELECT uCity,GROUP_CONCAT(uID) as uIDs
FROM users
GROUP BY uCity;

【例4.35】查询Users表,将同一城市的uID值用下划线“_”连接起来,列名为uIDs。
SELECT uCity,GROUP_CONCAT(uID ORDER BY uID SEPARATOR '_') as uIDs
FROM users
GROUP BY uCity;

【例4.36】查询Users表,统计“上海”和“长沙”两地用户人数。
SELECT uCity,COUNT(*)
FROM users
WHERE uCity in ('长沙','上海')
GROUP BY uCity
WITH ROLLUP;

【例4.38】查询Goods表中商品类别为“服饰”的商品ID、名称、价格及类别名称。
SELECT tName,gdCode,gdName,gdPrice
FROM goodstype JOIN goods
ON goodstype.tID= goods.tID
WHERE tName=’服饰’;

【例4.40】查询uName值为“段湘林”的购物车中的商品名称,价格及购买数量。
SELECT g.gdID,gdName,gdPrice,scNum
FROM users s JOIN scar t on s.uid=t.uid
JOIN goods g on g.gdID=t.gdID
WHERE uName='段湘林';

【例4.42】查询每个用户的订单金额,列出uID,uName,oTotal。
SELECT s.uID,uName,oTotal
FROM users s LEFT JOIN orders t
ON s.uID=t.uID;

【例4.43】查询每个用户的订单数,列出uID,uName,ordernum(订单数)。
SELECT s.uID,uName,count(t.uID) as orderNum
FROM orders t RIGHT JOIN users s
ON s.uID=t.uID
GROUP BY s.uID;

【例4.46】联合查询tid值为1和2的商品信息,列出tid,gdName,gdPrice,并按gdPrice从高到低排序,显示前三行记录。
SELECT tId,gdName,gdPrice
FROM goods
WHERE tId=1
UNION
SELECT tId,gdName,gdPrice
FROM goods
WHERE tId=2
ORDER BY gdPrice DESC LIMIT 3;

【例4.47】查询商品类别为“服饰”的商品ID、名称、价格及销售量。
第1步:先在goodstype表中查出类别名称tName为“服饰”的ID。
SELECT tId
FROM goodstype
WHERE tName='服饰’;
第2步:根据类别的ID值,在goods表中筛选商品的指定信息。
SELECT gdId,gdName,gdprice,gdSaleQty
FROM goods
WHERE tId=1;

2019-2020-1学期mysql数据库期终测试试题一
要求:1、将操作语句或者软件操作窗口截图到题目下面。
2、将输出的结果集窗口截图粘贴在题目下面。
评分标准:共10个操作题,每个题目10分,操作窗口图5分,结果集窗口截图5分。
一、 建立自己的数据库,将两个脚本文件导入navicat管理软件。

二、创建个人信息表。要求:①能准确描述个人的信息;②成功添加主键和创建索引。
create unique index suoyin123 on student123(sno(20) asc);

1、create table student(
sno varchar(20) primary key not null comment'学号(主码)',
sname varchar(20) not null comment'学生姓名',
ssex varchar(20) not null comment'学生性别',
sbirthday datetime comment'学生出生年月',
class varchar(20) comment'学生所在班级'
);

insert into student values
('108','曾华','男','1977-09-01','95033'),
('105','匡明','男','1977-09-01','95031'),
('107','王丽','女','1977-09-01','95033'),
('101','李军','男','1977-09-01','95033'),
('109','王芳','女','1977-09-01','95031'),
('103','陆君','男','1977-09-01','95031');

三、修改个人信息表。要求:①成功添加两个同学的记录信息;②查看信息表记录是否发生变化。

四、删除个人信息表。要求:①删除个人信息并查看;②清空个人信息表并查看。
1、drop table 表名称 eg: drop table dbo.Sys_Test
2、truncate table 表名称 eg: truncate table dbo.Sys_Test
3、delete from 表名称 where 列名称 = 值 eg: delete from dbo.Sys_Test where test='test'

五、查询users信息表中的所有记录。
select * from student
六、联合查询uID值为1和2的用户信息,列出uID,uName,uSex.
select sname,ssex,class from student;

七、查询Goods表中gdPrice不大于50的商品名称和价格。

八、查询Goods表显示从第4行开始连续3行记录的编号、名称和价格。
select gdCode,gdName,gdPrice from goods limit 3,3;

九、创建名为user9的用户,密码为user1111,其主机名为localhost。授予用户user9对个人数据库所有表有select、update的权限。
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO ‘aaa’@‘%’;//表示给用户aaa授权,让aaa能给所有库所有表实行所有的权力。
GRANT select,update ON TO ‘aaa’@‘%’;

GRANT SELECT,UPDATE ON kaoshi.* TO ‘user9’@’localhost’;

十、将个人数据库全部数据表转存出一个脚本文件(文件名格式:学号.sql)。

=============================================================================================================================
表一:student学生use)
1、create table student(
sno varchar(20) primary key not null comment'学号(主码)',
sname varchar(20) not null comment'学生姓名',
ssex varchar(20) not null comment'学生性别',
sbirthday datetime comment'学生出生年月',
class varchar(20) comment'学生所在班级'
);
表(二)Course(课程表)
create table course(
cno varchar(20) primary key not null comment'课程号(主码)',
cname varchar(20) not null comment'课程名称',
tno varchar(20) not null comment'教工编号'
);
表(三)Score(成绩表)
create table score(
id int primary key auto_increment comment'主键自增',
sno varchar(20) not null comment'学号',
cno varchar(20) not null comment'课程号',
degree Decimal(4,1) comment'成绩'
);
表四 teacher(教师表)
create table teacher(
tno varchar(20) primary key not null comment'教工编号(主码)',
tname varchar(20) not null comment'教工姓名',
tsex varchar(20) not null comment'教工性别',
tbirthday datetime comment'教工出生年月',
prof varchar(20) comment'职称',
depart varchar(20) not null comment'教工所在部门'
学生表数据的插入:
insert into student values
('108','曾华','男','1977-09-01','95033'),
('105','匡明','男','1977-09-01','95031'),
('107','王丽','女','1977-09-01','95033'),
('101','李军','男','1977-09-01','95033'),
('109','王芳','女','1977-09-01','95031'),
('103','陆君','男','1977-09-01','95031');
课程表数据的插入:
insert into course values
('3-105','计算机导论','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','831');
成绩表数据的插入:
select cno from score group by cno; #找出这个表中所有的班级
insert into score(sno,cno,degree) values
('103','3-245','86'),
('105','3-245','75'),
('109','3-245','68'),
('103','3-105','92'),
('105','3-105','88'),
('109','3-105','76'),
('101','3-105','64'),
('107','3-105','91'),
('108','3-105','78'),
('101','6-166','85'),
('107','6-166','79'),
('108','6-166','81');
教师表数据的插入:
insert into teacher values
('804','李诚','男','1958-12-02','副教授','计算机系'),
('856','张旭','男','1969-03-12','讲师','电子工程系'),
('825','王萍','女','1972-05-05','助教','计算机系'),
('831','刘冰','女','1977-08-14','助教','电子工程系');
查询:
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student;
2、查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher;
3、 查询Student表的所有记录。
select * from student;
4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree>60 and degree<80; 5、 查询Score表中成绩为85,86或88的记录。 select * from score where degree in (85,86,88); 6、 查询Student表中“95031”班或性别为“女”的同学记录。 select * from student where class like '95031' or ssex like '女'; 7、 以Class降序查询Student表的所有记录。 select * from student order by class desc; 8、 以Cno升序、Degree降序查询Score表的所有记录。 select * from score order by cno asc , degree desc; 9、 查询“95031”班的学生人数。 select count(*) from student group by class; 10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序) select * from score order by degree desc limit 1; 下面这个使用的连接查询 select max(degree) from score #先写出score的最高分 select * from score where degree = (select max(degree) from score); select sno,cno from score where degree = (select max(degree) from score); 11、 查询每门课的平均成绩。 select cno,count(*),avg(degree) from score group by cno having count(*); 多条查询 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 select cno,avg(degree) from score where cno like '3-105' and cno like '3%'; select * from score group by degree; select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;
select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;
13、查询分数大于70,小于90的Sno列。
select sno,degree from score where degree>70 and degree<90; select group_concat(sno) from score where degree>70 and degree<90; 14、查询所有学生的Sname、Cno和Degree列。(多表查询) student.sname,course.cno,score.degree select student.sname,course.cno,score.degree from student,course,score; select sname,cno,degree from student join score on student.sno = score.sno; 15、查询所有学生的Sno、Cname和Degree列。 student.sno,course.cname,score.degree select student.sno,course.cname,score.degree from student,course,score; select sno,cname,degree from score join course on course.cno = score.cno; 16、查询所有学生的Sname、Cname和Degree列。 student.sname,course.cname,score.degree; select student.sname,course.cname,score.degree from student,course,score; select student.sname,course.cname,score.degree from student,course,score where sname between '李军' and '王丽'; select student.sname,cname,degree from student join score on student.sno = score.sno join course on course.cno = score.cno; 17、查询“95033”班学生的平均分。 select sno from student where class = '95033'; select avg(degree) from score where sno in(select sno from student where class = '95033'); 18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 select degree from score where sno = '109' and cno = '3-105'; select sno,degree from score where degree > (select degree from score where sno = '109' and cno = '3-105');
19、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select cno from score where (cno = '3-245' and cno = '3-105') or (cno = '3-245' and cno = '6-166') or (cno = '3-245' and cno = '6-166'); 这是个错误的
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score b where b.Cno = a.Cno)
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ))
20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select degree from score where sno = '109' and cno = '3-105';
select * from score where degree > (select degree from score where sno = '109' and cno = '3-105');
21、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sbirthday from student where sno = '108';
select sno,sname,sbirthday from student where sbirthday = (select sbirthday from student where sno = '108');
22、查询“张旭“教师任课的学生成绩(姓名)。
select tno from teacher where tname = '张旭'; #找出教师编号
select cno from course where tno = (select tno from teacher where tname = '张旭'); #找出课程编号
select sno,degree from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭'));
select student.sno,degree,sname from score join student on score.sno = student.sno where student.sno in (select sno from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭')));
23、查询考计算机导论的学生成绩
select cno from course where cname = '计算机导论'; #找到课程编号3-105
select sno,degree from score where cno = (select cno from course where cname = '计算机导论');
24、查询李诚老师教的课程名称
select tno from teacher where tname = '李诚'; ##找到教师编号
select cname from course where tno = (select tno from teacher where tname = '李诚');
25、教高等数学的老师是哪个系的
select tno from course where cname = '高等数学';
select depart from teacher where tno = (select tno from course where cname = '高等数学');
26、查询选修某课程的同学人数多于5人的教师姓名。
select cno,count(*) from score group by cno having count(*)>=5; #找出课程编号
select tno from course where cno = (select cno from score group by cno having count(*)>=5);
select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(*)>=5));
27、查询95033班和95031班全体学生的记录。
select * from student group by class having count(*);
select * from student order by class desc;
28、查询成绩表中存在有85分以上成绩的课程Cno.
select cno,degree from score where degree>85;
29、查询出“计算机系“教师所教课程的成绩表。
select tno,tname from teacher where depart = '计算机系' #查出教师编号
select cno from course where tno in (select tno from teacher where depart = '计算机系'); #查出课程编号
select sno,cno,degree from score where cno in (select cno from course where tno in (select tno from teacher where depart = '计算机系'));
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的 Cno、Sno和Degree.
select max(degree) from score where cno = '3-245'; #先把选修编号为3-245课程的同学的最高成绩查询出来
select cno,sno,degree from score where cno = '3-105' and degree > (select max(degree) from score where cno = '3-245');
31、查询所有教师和同学的name、sex和birthday.
select tname as name,tsex as sex,tbirthday as birthday from teacher
union
select sname,ssex,sbirthday from student;
32、查询所有“女”教师和“女”同学的name、sex和birthday.
select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex = '女'
union
select sname,ssex,sbirthday from student where ssex = '女';
33、查询所有成绩比3-105课程平均成绩低的同学的成绩表。
select avg(degree) from score where cno = '3-105';
select degree from score where degree < (select avg(degree) from score where cno = '3-105'); 34、查询所有任课教师的Tname和Depart. select tname,depart from teacher; 35、查询所有未讲课的教师的Tname和Depart. select tno from course group by tno; #找出有课的老师的编号 select tname,depart from teacher where not exists(select tno from course group by tno); 36、查询至少有2名男生的班号。#####################################################3 select ssex,class from student where ssex = '男' group by class; select class from student where exists ((select ssex,class from student where ssex = '男' group by class) * 2); 37、查询Student表中不姓“王”的同学记录。 select sname from student where sname like '王%' select sname from student where sname not in (select sname from student where sname like '王%'); 38、查询Student表中每个学生的姓名和年龄。 select sname, select floor(datediff(curdate(),@birthday)/365.2422) 39、查询Student表中最大和最小的Sbirthday日期值。 select max(sbirthday) as '最大日期' , min(sbirthday) as '最小日期' from student; update student set sbirthday = '1995-07-11' where sno = '108'; update student set sbirthday = '1820-05-01' where sno = '105'; 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。 select * from student order by class desc,sbirthday desc; 41、查询“男”教师及其所上的课程。 select tno from teacher where tsex = '男'; select cname from course where tno in (select tno from teacher where tsex = '男'); 42、查询最高分同学的Sno、Cno和Degree列。 select max(degree) from score select score.sno,cno,degree from student join score on student.sno = score.sno where degree = (select max(degree) from score); 43、查询和“李军”同性别的所有同学的Sname. select ssex from student where sname = '李军'; select sname from student where ssex = (select ssex from student where sname = '李军'); 44、查询和“李军”同性别并同班的同学Sname. select class from student where sname = '李军'; select sname from student where ssex = (select ssex from student where sname = '李军') and class = (select class from student where sname = '李军'); 45、查询所有选修“计算机导论”课程的“男”同学的成绩表。 select cno from course where cname = '计算机导论'; #根据课程表找到课程编号 select sno from score where cno = (select cno from course where cname = '计算机导论'); #根据课程编号找到成绩表里面的学生编号 select sname from student where sno in (select sno from score where cno = (select cno from course where cname = '计算机导论')) and ssex = '男';

版权声明:散逸 发表于 2020-01-01 11:02:33。
转载请注明:数据库练习 | 散逸

2 条评论