diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230915 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247.md" "b/09 \346\233\271\346\255\243\346\263\242/20230915 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247.md" new file mode 100644 index 0000000000000000000000000000000000000000..510f63407ae29287b8373969d7db731606435799 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230915 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247.md" @@ -0,0 +1,165 @@ +# 笔记 + +多注意表与表之间的关系 + +# 作业 + + + +```mysql +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023/9/17 18:11:14 */ +/*==============================================================*/ +create database qiche charset utf8; +use qiche; + +drop table if exists automobile; + +drop table if exists client; + +drop table if exists record; + +drop table if exists salesman; + +/*==============================================================*/ +/* Table: automobile */ +/*==============================================================*/ +create table automobile #汽车 +( + a_id int(3) not null auto_increment, #汽车编号 + a_name varchar(10) not null, #汽车名称 + a_sellingprice decimal(7,1) not null, #汽车售价 + a_brand varchar(10) not null, #汽车品牌 + primary key (a_id) +); +insert into automobile values +(01,'五菱神光mini',20000.0,'五菱宏光'), +(02,'玛拉莎蒂2023',900000.0,'玛莎拉蒂'), +(03,'宝牛SUV',250000.0,'宝马'), +(04,'五菱宏光2077',10000.0,'五菱宏光'); + +/*==============================================================*/ +/* Table: client */ +/*==============================================================*/ +create table client #顾客 +( + c_id int(2) not null auto_increment, #顾客编号 + c_name varchar(10) not null, #顾客姓名 + c_sex varchar(2) not null, #顾客性别 + primary key (c_id) +); +insert into client values +(2001,'德莱厄斯','男'), +(2002,'塞恩','男'), +(2003,'蒙多','男'); +/*==============================================================*/ +/* Table: record */ +/*==============================================================*/ + + +/*==============================================================*/ +/* Table: salesman */ +/*==============================================================*/ +create table salesman #销售员 +( + s_id int(2) not null auto_increment, #工号 + s_name varchar(5) not null, #姓名 + s_sex varchar(2) not null, #性别 + primary key (s_id) +); +insert into salesman values +(1001,'梦泪','男'), +(1002,'坤哥','男'), +(1003,'厄斐琉斯','男'); + +create table record #销售记录 +( + r_id int(2) not null auto_increment, #销售编号 + s_id int not null, #工号 + c_id int not null, #顾客编号 + a_id int not null, #汽车编号 + primary key (r_id) +); +insert into record values +(3001,1001,2002,02), +(3002,1002,2001,01), +(3003,1001,2002,03), +(3004,1003,2003,04); + +alter table record add constraint FK_Relationship_2 foreign key (s_id) + references salesman (s_id) on delete restrict on update restrict; + +alter table record add constraint FK_Relationship_3 foreign key (c_id) + references client (c_id) on delete restrict on update restrict; + +alter table record add constraint FK_Relationship_4 foreign key (a_id) + references automobile (a_id) on delete restrict on update restrict; + -- 1.查询特定销售员的销售记录 +SELECT + s.s_id 工号, + s_name 销售员, + r_id 销售编号, + a_name 汽车型号, + a_sellingprice 售价, + a_brand 汽车品牌 +FROM + salesman s + JOIN record r + JOIN automobile a ON s.s_id = r.s_id + AND a.a_id = r.a_id +WHERE + s.s_id =( + SELECT + s_id + FROM + salesman + WHERE + s_name = '梦泪' + ); + -- 2.查找销售记录中销售价格最高的汽车 + select a.* from record r join automobile a on r.a_id=a.a_id where r.a_id = (select a_id from automobile where a_brand=(select max(a_brand) from automobile)); + -- 3.统计某个销售员的销售总额 +SELECT + s_name 销售员, + sum(a_sellingprice) 销售总额 +FROM + salesman s + JOIN record r + JOIN automobile a ON s.s_id = r.s_id + AND a.a_id = r.a_id + group by s.s_name having s.s_name='梦泪'; + -- 4.根据客户信息查询其购买过的汽车 + select c_name 顾客,c_sex 性别,a_name 汽车型号, a_sellingprice 售价,a_brand 型号 from client c join record r join automobile a on c.c_id=r.c_id and r.a_id = a.a_id where c.c_name='塞恩'; + -- 5.分析特定品牌汽车的销售情况,统计每个品牌的销售数量和总销售额 +SELECT + a_brand 品牌, + count( r.a_id) 销售数量, + sum( a.a_sellingprice ) 销售总额 +FROM + record r + JOIN automobile a ON r.a_id = a.a_id +GROUP BY + a_brand; + -- 6.检索特定日期范围内的销售了哪些汽车 + -- 7.查找某车型的销售历史。 + SELECT + a_name 汽车型号, + a_brand 品牌, + r.a_id 销售数量, + a.a_sellingprice 销售总额 +FROM + record r + JOIN automobile a ON r.a_id = a.a_id where a.a_name='五菱神光mini'; + -- 8.统计每个销售员的销售数量 +SELECT + s_name 销售员, + count(r.a_id) 销售总额 +FROM + salesman s + JOIN record r + JOIN automobile a ON s.s_id = r.s_id + AND a.a_id = r.a_id + group by s.s_name; +``` +