From e017aff7230934b6615c80e4111b1d517fd34d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=A3=9E=E9=B9=8F?= <2879562915@qq.com> Date: Thu, 19 Sep 2024 17:30:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\272\223\350\256\276\350\256\241(1).md" | 0 ...345\272\223\350\256\276\350\256\241(3).md" | 0 ...345\272\223\350\256\276\350\256\241(4).md" | 320 ++++++++++++++++++ 3 files changed, 320 insertions(+) rename "\350\202\226\351\243\236\351\271\217/20240912\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\347\232\204\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(1).md" => "\350\202\226\351\243\236\351\271\217/20240912\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(1).md" (100%) rename "\350\202\226\351\243\236\351\271\217/20240913\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(3).md" => "\350\202\226\351\243\236\351\271\217/20240914\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(3).md" (100%) create mode 100644 "\350\202\226\351\243\236\351\271\217/20240919\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(4).md" diff --git "a/\350\202\226\351\243\236\351\271\217/20240912\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\347\232\204\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(1).md" "b/\350\202\226\351\243\236\351\271\217/20240912\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(1).md" similarity index 100% rename from "\350\202\226\351\243\236\351\271\217/20240912\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\347\232\204\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(1).md" rename to "\350\202\226\351\243\236\351\271\217/20240912\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(1).md" diff --git "a/\350\202\226\351\243\236\351\271\217/20240913\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(3).md" "b/\350\202\226\351\243\236\351\271\217/20240914\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(3).md" similarity index 100% rename from "\350\202\226\351\243\236\351\271\217/20240913\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(3).md" rename to "\350\202\226\351\243\236\351\271\217/20240914\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(3).md" diff --git "a/\350\202\226\351\243\236\351\271\217/20240919\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(4).md" "b/\350\202\226\351\243\236\351\271\217/20240919\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(4).md" new file mode 100644 index 0000000..b93ce35 --- /dev/null +++ "b/\350\202\226\351\243\236\351\271\217/20240919\347\275\221\344\270\212\344\271\246\345\272\227\347\256\241\347\220\206\347\263\273\347\273\237\346\225\260\346\215\256\345\272\223\350\256\276\350\256\241(4).md" @@ -0,0 +1,320 @@ +# 日记 + +今天是个上分的好日子 + +# 课堂笔记 + +完善网上书店数据库设计管理系统 + +# 作业 + +## 三大模型 + +1.CDM + +![image-20240919170316552](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240919_1726736596.png) + +2.LDM + +![image-20240919170339344](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240919_1726736619.png) + +3.PDM + +![image-20240919170354917](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240919_1726736635.png) + +## sql语句及插入数据 + +```sql +CREATE DATABASE if not EXISTS shop_db; +USE shop_db; + +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2024-09-14 10:21:41 */ +/*==============================================================*/ + + +drop table if exists address; + +drop table if exists area; + +drop table if exists author; + +drop table if exists book; + +drop table if exists category; + +drop table if exists city; + +drop table if exists man_book; + +drop table if exists management; + +drop table if exists order01; + +drop table if exists pitcure; + +drop table if exists publisher; + +drop table if exists role; + +drop table if exists role_man; + +drop table if exists sheng; + +drop table if exists user; + +/*==============================================================*/ +/* Table: address */ +/*==============================================================*/ +create table address +( + address_id int not null, + sheng_id int not null, + city_id int not null, + area_id int not null, + address_detail varchar(255), + primary key (address_id) +); + +/*==============================================================*/ +/* Table: area */ +/*==============================================================*/ +create table area +( + area_id int not null, + area_name varchar(255), + primary key (area_id) +); + +/*==============================================================*/ +/* Table: author */ +/*==============================================================*/ +create table author +( + autohr_id int not null, + author_name varchar(255), + gender int, + author_detail varchar(255), + primary key (autohr_id) +); + +/*==============================================================*/ +/* Table: book */ +/*==============================================================*/ +create table book +( + book_id int not null, + cate_id int not null, + publisher_id int not null, + autohr_id int not null, + book_name varchar(255), + price_r int, + ISBN int, + publish_date varchar(255), + primary key (book_id) +); + +/*==============================================================*/ +/* Table: category */ +/*==============================================================*/ +create table category +( + cate_id int not null, + cate_name varchar(255), + primary key (cate_id) +); + +/*==============================================================*/ +/* Table: city */ +/*==============================================================*/ +create table city +( + city_id int not null, + city_name varchar(255), + primary key (city_id) +); + +/*==============================================================*/ +/* Table: man_book */ +/*==============================================================*/ +create table man_book +( + book_id int not null, + management_id int not null, + primary key (book_id, management_id) +); + +/*==============================================================*/ +/* Table: management */ +/*==============================================================*/ +create table management +( + management_id int not null, + management_name varchar(255), + primary key (management_id) +); + +/*==============================================================*/ +/* Table: "order" */ +/*==============================================================*/ +create table order01 +( + order_id int not null, + address_id int not null, + user_id int not null, + book_id int not null, + order_num int, + order_price int, + primary key (order_id) +); + +/*==============================================================*/ +/* Table: pitcure */ +/*==============================================================*/ +create table pitcure +( + pitcure_id int not null, + book_id int not null, + autohr_id int not null, + pitcure_path varchar(255), + primary key (pitcure_id) +); + +/*==============================================================*/ +/* Table: publisher */ +/*==============================================================*/ +create table publisher +( + publisher_id int not null, + publisher_name varchar(255), + publisher_phone int, + primary key (publisher_id) +); + +/*==============================================================*/ +/* Table: role */ +/*==============================================================*/ +create table role +( + role_id int not null, + role_name varchar(255), + primary key (role_id) +); + +/*==============================================================*/ +/* Table: role_man */ +/*==============================================================*/ +create table role_man +( + management_id int not null, + role_id int not null, + primary key (management_id, role_id) +); + +/*==============================================================*/ +/* Table: sheng */ +/*==============================================================*/ +create table sheng +( + sheng_id int not null, + sheng_name varchar(255), + primary key (sheng_id) +); + +/*==============================================================*/ +/* Table: user */ +/*==============================================================*/ +create table user +( + user_id int not null, + role_id int not null, + user_name varchar(255), + user_account int, + user_pwd int, + user_phone int, + primary key (user_id) +); + +alter table address add constraint FK_Relationship_1 foreign key (sheng_id) + references sheng (sheng_id) on delete restrict on update restrict; + +alter table address add constraint FK_Relationship_2 foreign key (city_id) + references city (city_id) on delete restrict on update restrict; + +alter table address add constraint FK_Relationship_3 foreign key (area_id) + references area (area_id) on delete restrict on update restrict; + +alter table book add constraint FK_Relationship_7 foreign key (cate_id) + references category (cate_id) on delete restrict on update restrict; + +alter table book add constraint FK_Relationship_8 foreign key (publisher_id) + references publisher (publisher_id) on delete restrict on update restrict; + +alter table book add constraint FK_Relationship_9 foreign key (autohr_id) + references author (autohr_id) on delete restrict on update restrict; + +alter table man_book add constraint FK_man_book foreign key (book_id) + references book (book_id) on delete restrict on update restrict; + +alter table man_book add constraint FK_man_book2 foreign key (management_id) + references management (management_id) on delete restrict on update restrict; + +alter table order01 add constraint FK_Relationship_14 foreign key (address_id) + references address (address_id) on delete restrict on update restrict; + +alter table order01 add constraint FK_Relationship_15 foreign key (user_id) + references user (user_id) on delete restrict on update restrict; + +alter table order01 add constraint FK_Relationship_16 foreign key (book_id) + references book (book_id) on delete restrict on update restrict; + +alter table pitcure add constraint FK_Relationship_11 foreign key (book_id) + references book (book_id) on delete restrict on update restrict; + +alter table pitcure add constraint FK_Relationship_13 foreign key (autohr_id) + references author (autohr_id) on delete restrict on update restrict; + +alter table role_man add constraint FK_role_man foreign key (management_id) + references management (management_id) on delete restrict on update restrict; + +alter table role_man add constraint FK_role_man2 foreign key (role_id) + references role (role_id) on delete restrict on update restrict; + +alter table user add constraint FK_Relationship_4 foreign key (role_id) + references role (role_id) on delete restrict on update restrict; + + INSERT INTO `address` VALUES (1, 1, 1, 1, '闽大14栋'); + INSERT INTO `area` VALUES (1, '新罗区'); + INSERT INTO `author` VALUES (1, '卢比', 1, '装呗哥'); + INSERT INTO `book` VALUES (1, 1, 1, 1, '水浒传', 1, 666, '2020年11月'); + INSERT INTO `category` VALUES (1, '武侠'); + INSERT INTO `city` VALUES (1, '龙岩'); + INSERT INTO `management` VALUES (1, '购买'); + INSERT INTO `pitcure` VALUES (1, 1, 1, 'book/001.jpg'); + INSERT INTO `publisher` VALUES (1, '闽达出版社', 10086); + INSERT INTO `role` VALUES (1, '会员'); + INSERT INTO `sheng` VALUES (1, '福建'); + INSERT INTO `user` VALUES (1, 1, '小臂轮', 2344111, 123345, 10085); +``` + + + +## navicat执行 + +![image-20240919171558765](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240919_1726737359.png) + + + +## 简单需求执行 + +```sql +-- 查询用户购买了什么图书及其信息 + +SELECT user_name,book_name,ISBN,publish_date FROM user u +JOIN order01 o on u.user_id=o.user_id +JOIN book b on b.book_id=o.book_id +JOIN author a on a.autohr_id=b.autohr_id; +``` + +![image-20240919172553534](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240919_1726737953.png) \ No newline at end of file -- Gitee From ccd395689d571796affd52668b9bae727a1c052d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=A3=9E=E9=B9=8F?= <2879562915@qq.com> Date: Fri, 20 Sep 2024 17:34:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\345\217\212\344\275\234\344\270\232.md" | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 "\350\202\226\351\243\236\351\271\217/20240920\346\225\260\346\215\256\345\272\223\350\247\206\345\233\276\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" diff --git "a/\350\202\226\351\243\236\351\271\217/20240920\346\225\260\346\215\256\345\272\223\350\247\206\345\233\276\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" "b/\350\202\226\351\243\236\351\271\217/20240920\346\225\260\346\215\256\345\272\223\350\247\206\345\233\276\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5a87a23 --- /dev/null +++ "b/\350\202\226\351\243\236\351\271\217/20240920\346\225\260\346\215\256\345\272\223\350\247\206\345\233\276\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" @@ -0,0 +1,180 @@ +# 课堂笔记 + +## 视图 + +#### 认识视图 + +​ 视图是一个虚拟表,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据。但是,数据库中只存放了视图的定义,而并没有存放视图中的数据,这些数据存放在原来的表中。使用视图查询数据时,数据库系统会从原来的表中取出对应的数据。因此,视图中的数据是依赖于原来的表中的数据的。一旦表中的数据发生改变,显示在视图中的数据也会发生改变。同样对视图的更新,会影响到原来表的数据。 + +#### 视图的优点 + +​ 1.减少网络开销 + +​ 2.便于用户查询 + +​ 3.安全性高 + +#### 视图的基本操作 + +##### 创建视图 + +```sql +create or replace view 视图名 select 查询语句; +``` + +##### 修改视图 + +```sql +alter view 视图名 新的 select查询语句; +create or replace view 视图名 新的 select查询语句; +``` + +##### 查询视图 + +```sql +select 字段 from 视图名; +``` + +##### 删除视图 + +```sql +drop table if exists 视图名; +``` + + + +# 作业 + +### 一.课堂练习 + +1. 创建一个视图,显示所有部门的名称以及属于该部门的员工姓名 + + ```sql + CREATE or replace view v_test1 AS SELECT dname 部门名称,ename 员工姓名 FROM dept JOIN emp ON dept.deptno=emp.deptno; + SELECT*FROM v_test1; + ``` + + ![image-20240920111355253](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240920_1726802035.png) + +2. 创建一个视图,显示员工姓名、职位、薪水和佣金。 + + ```sql + CREATE or replace view v_test2 AS SELECT ename 员工姓名,job 职位,sal 薪资,comm 佣金 FROM emp; + SELECT*FROM v_test2; + ``` + + ![image-20240920111629945](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240920_1726802190.png) + +3. 创建一个视图,显示所有薪水大于 20000 的员工姓名、职位和薪水。 + + ```sql + CREATE or replace view v_test3 AS SELECT ename 员工姓名,job 职位,sal 薪资 FROM emp WHERE sal>20000; + SELECT*FROM v_test3; + ``` + + ![image-20240920111929097](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240920_1726802369.png) + +4. 创建按部门分类的员工数量视图 + + ```sql + CREATE or replace view v_test4 AS SELECT count(ename) 员工数量,dname 部门 FROM emp JOIN dept on dept.deptno=emp.deptno GROUP BY dname; + SELECT*FROM v_test4; + ``` + + ![image-20240920112433040](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240920_1726802673.png) + +5. 创建一个视图,显示员工姓名、薪水及其所属的薪资等级。 + + ```sql + CREATE or replace view v_test5 AS SELECT ename 员工姓名 ,sal 薪水, grade 等级 FROM emp JOIN salgrade sg on emp.sal BETWEEN sg.losal and sg.hisal; + SELECT*FROM v_test5; + ``` + + ![image-20240920112913493](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/img/upgit_20240920_1726802953.png) + +6. 创建一个视图,显示每个经理及其直接下属员工的姓名。 + + ```sql + CREATE or replace view v_test1 AS SELECT e2.ename 经理,e1.ename 下属 FROM emp e1 JOIN (SELECT * FROM emp WHERE job = '经理') e2 + on e2.empno=e1.mgr; + SELECT * FROM v_test1; + ``` + + ![image-20240920163147844](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201631946.png) + +7. 创建一个视图,显示每个部门的名称及其平均薪水。 + + ```sql + CREATE or replace view v_test2 AS SELECT avg(sal) 部门平均薪资, dname 部门 FROM emp join dept ON emp.deptno= dept.deptno GROUP BY dname; + SELECT * FROM v_test2; + ``` + + ![image-20240920163921064](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201639660.png) + +8. 创建一个视图,显示在 2001 年以后入职的员工姓名和入职日期。 + + ```sql + CREATE or replace view v_test3 AS SELECT ename 员工姓名, hiredate 入职日期 FROM emp WHERE hiredate>'2001-01-01'; + SELECT * FROM v_test3; + ``` + + ![image-20240920164335565](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201643599.png) + +9. 删除一个已创建的视图。 + + ```sql + drop table if exists v_test1; + ``` + + ![image-20240920164433263](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201644371.png) + +10. 修改第一个视图的名称 + + ```sql + rename table v_test1 to v_test0 + ``` + +11. 修改第二个视图,显示员工姓名、职位、部门。 + + ```sql + CREATE or replace view v_test2 AS SELECT ename 员工姓名,job 职位, dname 部门 FROM emp + join dept on emp.deptno=dept.deptno; + SELECT*FROM v_test2; + ``` + + ![image-20240920164853318](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201648436.png) + + + + + +### 二.周末作业 + +-- 1:查询部门平均薪水最高的部门名称 + +```sql +SELECT dname 部门 FROM dept d join (SELECT avg(sal) avg_sal,deptno FROM emp GROUP BY deptno) e1 ON e1.deptno= d.deptno + ORDER BY avg_sal DESC LIMIT 1 ; +``` + +![image-20240920170408746](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201704843.png) + +-- 2:查询员工比所属领导薪资高的部门名、员工名、员工领导编号 + +```sql +SELECT e1.ename 员工 ,dname 部门, e2.empno FROM emp e1 JOIN (SELECT * FROM emp WHERE job = '经理') e2 +on e2.empno=e1.mgr +JOIN dept ON e1.deptno=dept.deptno +WHERE e1.sal>e2.sal; +``` + +![image-20240920172416749](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201724791.png) + +-- 3:查询工资等级为4级,2000年以后入职的工作地点为上海的员工编号、姓名和工资 + +```sql +SELECT empno ,ename ,sal FROM emp JOIN salgrade sg on emp.sal BETWEEN sg.losal AND sg.hisal WHERE sg.grade =4 and emp.hiredate>'2000-01-01'; + +``` + +![image-20240920172920510](https://gitee.com/feibang_1_0/xiao-feipengs-picture-bed/raw/master/images/202409201729608.png) -- Gitee