MySql(9)--MySQL 数据库信息更新与查询

一:更新数据库信息
首先,我们插入多组数据来测试一下
注意插入之前,把四张表格数据先清空一下,按下图这个键删除之前的数据
这里写图片描述
然后:
– 删除图书信息表的外键
alter table bookinfo drop foreign key fk_bcid;

– 为图书类别表添加自动编号的功能
alter table bookcategory modify category_id int auto_increment;

– 恢复关联
alter table bookinfo add constraint fk_bcid foreign key(book_category_id)references bookcategory(category_id);

– 设置起始值为1
alter table bookcategory_tmp auto_increment = 1;

然后插入这些信息,如果不会的话看之前的博文
/图书类别表/
insert into bookcategory(category,parent_id)values(‘计算机’,0),(‘医学’,0),(‘编程语言’,1),(‘数据库’,1),(‘儿科学’,2);

/图书信息表/
insert into bookinfo(book_id,book_category_id,book_name,author,price,press,pubdate,store)
values
(20150201,3, ‘Java编程思想’, ‘(美)埃克尔’, 79.8, default, ‘2007-04-01’, 5),
(20150202,4, ‘PHP和MySQL Web开发’, ‘Luke Welling等’,95, default, ‘2009-04-01’, 2),
(20150301,3, ‘Spring源码深度解析’, ‘郝佳’,69, ‘人民邮电出版社’, ‘2013-09-01’, 3),
(20160801,5, ‘中医儿科学’, ‘汪受传’, 136, ‘人民卫生出版社’, ‘2011-04-01’, 1),
(20170401,5, ‘小儿推拿秘笈’, ‘李德修’, 24.5, ‘人民卫生出版社’, ‘2011-04-01’,4);

/读者信息表/
insert into readerinfo(card_id,name,sex,age,tel,balance)
values
(210210199901011111,’张飞’,’女’,18,’13566661111’,300),
(210210199901012222,’李月’,’女’,19,’13566662222’,200),
(210210199901013333,’王鹏’,’男’,20,’13566663333’,300),
(210210199901014444,’刘鑫’,’男’,21,’13566664444’,400),
(210210199901015555,’杨磊’,’男’,22,’13566665555’,500);

/*
身份证号为210210199901011111的读者,2017-11-29借走了图书编号为20150201的这本书,
根据业务需求向借阅信息表插入一条借阅信息,更新读者信息表中的余额和图书信息表的库存,要求如下:
1、借书的期限为1个月。
2、借书的费用为:书的价格*5%
3、图书信息表中图书编号为20150201这本书的库存-1
*/

– 向借阅信息表插入一条借阅信息
insert into borrowinfo(book_id,card_id,borrow_date,return_date,status)values(20150201,210210199901011111,’2017-11-29’,’2017-12-29’,’否’);

– 更新读者信息表中的余额
– 查看书的价格 79.80
select price from bookinfo where book_id = 20150201;
这里写图片描述
– 更新余额
update readerinfo set balance = balance - 79.80*0.05 where card_id = ‘210210199901011111’;
select * from readerinfo;
这里写图片描述
– 更新图书信息表的库存
update bookinfo set store = store -1 where book_id = 20150201;
select * from bookinfo;
这里写图片描述

二:删除数据记录
– 删除指定条件的记录
delete from readerinfo where card_id = ‘210210199901011111’;
– 删除表中所有记录
delete from readerinfo;或者truncate table readerinfo;
注意这里如果报错,说you are using safeupdate mode,去Edit下面的preference,SQL editing下面把safeupdate模式去掉,关闭软件再重新连接就可以了

扫描二维码关注公众号,回复: 2791280 查看本文章

– 查询儿科学的类别编号 5,也就是说书的类别为儿科学的标号都是5
select category_id from bookcategory where category=’儿科学’;

– 删除图书编号为5的图书信息
delete from bookinfo where book_category_id = 5;
这样图书标号为5的类别都没了
这里写图片描述
– 删除图书类别表中儿科学这个类别
delete from bookcategory where category = ‘儿科学’;
这里写图片描述

三:查询数据记录
– 查询所有列
select * from bookcategory;

select category_id,category,parent_id from bookcategory;

– 查询指定列
select category from bookcategory;

select category_id,category from bookcategory;

– 查询指定条件的记录
select book_id,book_name,price from bookinfo where press=’机械工业出版社’;

– 查询结果不重复的记录
select distinct press from bookinfo;

– 查看空值
select * from readerinfo where age is null;

四:分组查询
select count(*) from readerinfo where sex=’男’;
这里写图片描述
select sex from readerinfo group by sex;
这里写图片描述
select sex,count(*) from readerinfo group by sex;
这里写图片描述
select sex from readerinfo group by sex having count(sex)>2;
这里写图片描述
select * from bookinfo order by price;
这里写图片描述
select * from bookinfo order by price,store;
这里写图片描述
select * from bookinfo order by price,store desc; desc表示降续
这里写图片描述
– 前3行记录
select * from bookinfo limit 3;
这里写图片描述
– 从第3条记录开始的后2条记录
select * from bookinfo limit 2,2;或者select * from bookinfo limit 2 offset 2;
这里写图片描述
插入更多信息查询
insert into bookinfo(book_id,book_category_id,book_name,author,price,press,pubdate,store)
values
(20160011,4, ‘高性能MySQL’, ‘Baron Schwartz等’,85.8, ‘电子工业出版社’, ‘2013-04-01’, 10),
(20160210,4, ‘数据库系统基础教程’, ‘[美]厄尔曼等’, 35.5, ‘机械工业出版社’, ‘2009-08-01’, 20),
(20170050,4, ‘数据库系统实现’, ‘[美]加西亚·莫利纳 ‘, 46.6, ‘机械工业出版社’, ‘2010-05-01’,8);

– 将图书信息按照库存进行分组,统计每组库存下的个数,图书库存为3的图书有两种图书
select store,count(*)from bookinfo group by store order by store;
这里写图片描述
– 然后按库存进行降序排序,并查看结果中的前四条记录。
select store,count(*)from bookinfo group by store order by store desc limit 4;
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42350428/article/details/81712964