头歌MySQL数据库实训答案2022

答案由本人收集+自己写的,仅作参考,帮助写不完作业的小伙伴

实训1 数据库、表与完整性约束的定义(Create)

指路头歌MySQL数据库实训答案 有目录,实训一的都有,后面的题有些换了

实训2 数据库、表与完整性约束的定义(Create)

第1关 修改表名

USE TestDb1;
#请在以下空白处添加恰当的语句,将表名your_table更改为my_table:
alter table your_table rename my_table;

第2关 添加与删除字段

use MyDb;
#请在以下空白处添加适当的SQL代码,实现编程要求
#语句1:删除表orderDetail中的列orderDate
alter table orderDetail drop orderDate;
#语句2:添加列unitPrice
alter table orderDetail add unitPrice decimal(10,2) after quantityOrdered;

第3关 修改字段

use MyDb;
#请在以下空白处添加适当的SQL语句,实现编程要求
alter table addressBook modify QQ char(12);
alter table addressBook rename column weixin to wechat;

第4关 添加、删除与修改约束

use MyDb;
#请在以下空白处填写适当的诘句,实现编程要求。
#(1) 为表Staff添加主码
alter table Staff add constraint primary key(staffNo);
#(2) Dept.mgrStaffNo是外码,对应的主码是Staff.staffNo,请添加这个外码,名字为FK_Dept_mgrStaffNo:
alter table Dept add constraint FK_Dept_mgrStaffNo foreign key(mgrStaffNo) references Staff(staffNo);
#(3) Staff.dept是外码,对应的主码是Dept.deptNo. 请添加这个外码,名字为FK_Staff_dept:
alter table Staff add constraint FK_Staff_dept foreign key(dept) references Dept(deptNo);
#(4) 为表Staff添加check约束,规则为:gender的值只能为F或M;约束名为CK_Staff_gender:
alter table Staff add constraint CK_Staff_gender check(gender='F'or gender='M');
#(5) 为表Dept添加unique约束:deptName不允许重复。约束名为UN_Dept_deptName:
alter table Dept add constraint UN_Dept_deptName UNIQUE(deptName);

实训3 基于金融应用的数据查询(Select)(1-11关)

8关后来源自指路数据库实验,不仅有题解还有很多笔记,谢谢大佬

第1关 金融应用场景介绍,查询客户主要信息

-- 1) 查询所有客户的名称、手机号和邮箱信息。查询结果按照客户编号排序。
--    请用一条SQL语句实现该查询:
select c_name,c_phone,c_mail from client ORDER BY c_id;
/*  end  of  your code  */

第2关 邮箱为null的客户

-- 2) 查询客户表(client)中邮箱信息为null的客户的编号、名称、身份证号、手机号。
--    请用一条SQL语句实现该查询:
select c_id,c_name,c_id_card,c_phone from client where c_mail is null;
/*  end  of  your code  */

第3关 既买了保险又买了基金的客户

-- 3) 查询既买了保险又买了基金的客户的名称、邮箱和电话。结果依c_id排序
-- 请用一条SQL语句实现该查询:
select c_name,c_mail,c_phone from client where c_id in
    (select pro_c_id from property where pro_type=3)
    and c_id in(select pro_c_id from property where pro_type=2)
    order by c_id;
/*  end  of  your code  */

第4关 办理了储蓄卡的客户信息

-- 4) 查询办理了储蓄卡的客户名称、手机号、银行卡号。 查询结果结果依客户编号排序。
--    请用一条SQL语句实现该查询:
select c_name,c_phone,b_number from client,bank_card
where b_type='储蓄卡' and b_c_id=c_id
order by c_id;
/*  end  of  your code  */

第5关 每份金额在30000~50000之间的理财产品

-- 5) 查询理财产品中每份金额在30000~50000之间的理财产品的编号,每份金额,理财年限,并按照金额升序排序,金额相同的按照理财年限降序排序。
--    请用一条SQL语句实现该查询:
select p_id,p_amount,p_year from finances_product 
where p_amount between 30000 and 50000
order by p_amount,p_year desc;
/*  end  of  your code  */

第6关 商品收益的众数

-- 6) 查询资产表中所有资产记录里商品收益的众数和它出现的次数。
--    请用一条SQL语句实现该查询:
select pro_income,count(*) as presence
from property group by pro_income 
having count(*)>=all(select count(*) from property group by pro_income);
/*  end  of  your code  */

第7关 未购买任何理财产品的武汉居民

-- 7) 查询身份证隶属武汉市没有买过任何理财产品的客户的名称、电话号、邮箱。
--    请用一条SQL语句实现该查询:
select c_name,c_phone,c_mail from client
where(select left(client.c_id_card, 4)=4201)
and c_id not in(select pro_c_id from property where pro_type=1);
/*  end  of  your code  */

第8关 持有两张信用卡的用户

-- 8) 查询持有两张(含)以上信用卡的用户的名称、身份证号、手机号。
--    请用一条SQL语句实现该查询:
select c_name,c_id_card,c_phone from client
where (c_id,"信用卡")in
(select b_c_id,b_type from bank_card group by b_c_id,b_type
having count(*) > 1);
/*  end  of  your code  */

第9关 购买了货币型基金的客户信息

-- 9) 查询购买了货币型(f_type='货币型')基金的用户的名称、电话号、邮箱。
--   请用一条SQL语句实现该查询:
select c_name,c_phone,c_mail from client where c_id in
(select pro_c_id from property where pro_type=3
and pro_pif_id in(select f_id from fund where f_type='货币型'))
order by c_id;
/*  end  of  your code  */

第10关 投资总收益前三名的客户

-- 10) 查询当前总的可用资产收益(被冻结的资产除外)前三名的客户的名称、身份证号及其总收益,按收益降序输出,总收益命名为total_income。不考虑并列排名情形。
--    请用一条SQL语句实现该查询:
select c_name,c_id_card,sum(pro_income)as total_income from client 
inner join property on pro_c_id=c_id and pro_status="可用" 
group by c_id 
order by total_income desc 
limit 3;
/*  end  of  your code  */ 

第11关 黄姓客户持卡数量

-- 11) 给出黄姓用户的编号、名称、办理的银行卡的数量(没有办卡的卡数量计为0),持卡数量命名为number_of_cards,
--     按办理银行卡数量降序输出,持卡数量相同的,依客户编号排序。
-- 请用一条SQL语句实现该查询:
select c_id,c_name,count(b_c_id) as number_of_cards from client left join bank_card on c_id=b_c_id where c_name like "黄%"group by c_id
order by number_of_cards desc, c_id;
/*  end  of  your code  */ 

实训4 数据的插入、修改与删除(Insert,Update,Delete)

第1关 插入多条完整的客户信息

use finance1;
-- 用insert语句向客户表(client)插入任务要求的3条数据:
INSERT INTO client(c_id,c_name,c_mail,c_id_card,c_phone,c_password)VALUES
(1,'林惠雯','960323053@qq.com',411014196712130323,15609032348,'Mop5UPkl'),
(2,'吴婉瑜','1613230826@gmail.com',420152196802131323,17605132307,'QUTPhxgVNlXtMxN'),
(3,'蔡贞仪','252323341@foxmail.com',160347199005222323,17763232321,'Bwe3gyhEErJ7');
/* end of you code */

第2关 插入不完整的客户信息

use finance1;
-- 已知33号客户部分信息如下:
-- c_id(编号):33
-- c_name(名称):蔡依婷
-- c_phone(电话):18820762130
-- c_id_card(身份证号):350972199204227621
-- c_password(密码):MKwEuc1sc6
-- 请用一条SQL语句将这名客户的信息插入到客户表(client):
INSERT INTO client(c_id,c_name,c_mail,c_id_card,c_phone,c_password)VALUES
(33,'蔡依婷',NULL,350972199204227621,18820762130,'MKwEuc1sc6')
/* end of you code */

第3关 批量插入数据

use finance1;
 -- 已知表new_client保存了一批新客户信息,该表与client表结构完全相同。请用一条SQL语句将new_client表的全部客户信息插入到客户表(client):
insert into client select * from new_client;
/* end of you code */

第4关 删除没有银行卡的客户信息

use finance1;
-- 请用一条SQL语句删除client表中没有银行卡的客户信息:
-- 经评论区提醒正规写法:
DELETE FROM client where not exists (select b_c_id from bank_card where b_c_id = c_id);
DELETE FROM client WHERE c_id_card=null;
/* end of you code */

你直接DELETE FROM client WHERE c_id=2100;也可以过

  • 29
    点赞
  • 173
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值