数据库相关 面试题 I
面试题
1.表设计面试题
2021年过年时小明在这些天都收到了许多亲戚\朋友还有同事的红包,也发出了一些红包,有的是微信,有的是支付宝也有现金,请参考下面的题目帮小明设计表格保存红包的信息
(至少包含一张流水表) 先列出需要保存的数据有哪几种
关系,红包类型,红包金额,时间,性别,名字
2.答案:
\1. 设计表
流水表: id,红包金额,时间,红包类型,人物id
create table trade(id int primary key auto_increment,money int,time date,type varchar(10),person_id int)charset=utf8;
人物表: id,名字,性别,关系
create table person(id int primary key auto_increment,name varchar(50),gender char(1),rel varchar(5))charset=utf8;
l 准备数据
- 插入人物数据: 人物表,刘德华 男 亲戚 ,杨幂 女 亲戚 ,马云 男 同事,特朗普 男 朋友, 貂蝉 女 朋友
insert into person values(null,‘刘德华’,‘男’,‘亲戚’),(null,‘杨幂’,‘女’,‘亲戚’),(null,‘马云’,‘男’,‘同事’),(null,‘特朗普’,‘男’,‘朋友’),(null,‘貂蝉’,‘女’,‘朋友’);
- 插入流水数据:
刘德华 微信 收1000 2021-03-20
杨幂 现金 收500 发50 2021-04-14
马云 支付宝 收20000 发5 2021-03-11
特朗普 微信 收2000 2021-05-18
貂蝉 微信 发20000 2021-07-22
insert into trade values(null,1000,“2021-03-20”,“微信”,1),
(null,500,“2021-04-14”,“现金”,2),(null,-50,“2021-04-14”,“现金”,2),
(null,20000,“2021-03-11”,“支付宝”,3),(null,-5,“2021-03-11”,“支付宝”,3),(null,2000,“2021-05-18”,“微信”,4),
(null,-20000,“2021-07-22”,“微信”,5);
\2. 统计2021年2月15号到现在的所有红包收益
select sum(money) from trade where time>“2021-2-15”;
\3. 查询2021年2月15号到现在 金额大于100 所有女性亲戚的名字和金额
select name,money from person p join trade t on p.id=t.person_id where time>“2021-2-15” and money not between -100 and 100 and gender=“女” and rel=“亲戚”;
\4. 查询三个平台(微信,支付宝,现金)分别收入的红包金额
select type,sum(money) from trade where money>0 group by type;