diff --git "a/\345\271\262\347\272\257\346\254\243/2024.0914 \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(3).md" "b/\345\271\262\347\272\257\346\254\243/2024.0914 \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(3).md" index 023ceb713ecf78048bd667cc6fd7004b75353309..8a8476eb67f4e4a1aeb09f61ffbc287aad39f7cd 100644 --- "a/\345\271\262\347\272\257\346\254\243/2024.0914 \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(3).md" +++ "b/\345\271\262\347\272\257\346\254\243/2024.0914 \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(3).md" @@ -1,4 +1,4 @@ -# 20240913网上书店管理系统的数据库设计(2) +# 20240913网上书店管理系统的数据库设计(3) ## 一、数据库设计 @@ -34,13 +34,10 @@ **4、图库** -图片编号 - -图片 - -图片路径 - -上传日期 +- 图片编号 +- 图片 +- 图片路径 +- 上传日期 **5、出版社** @@ -322,3 +319,7 @@ INSERT INTO `user` VALUES (7, '李四', 'lisi', 8515632, '购买者', 5555694, ' ``` +表格数据 + +![image-20240919171457270](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409191714379.png) + diff --git "a/\345\271\262\347\272\257\346\254\243/2024.0919 \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(4).md" "b/\345\271\262\347\272\257\346\254\243/2024.0919 \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(4).md" new file mode 100644 index 0000000000000000000000000000000000000000..a82edd244027c08e1d7858077da3f9bad71f95cd --- /dev/null +++ "b/\345\271\262\347\272\257\346\254\243/2024.0919 \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(4).md" @@ -0,0 +1,358 @@ +# 20240919网上书店管理系统的数据库设计(4) + +## 一、数据库设计 + +**1、分类表** + +- 分类编号 + +- 分类名称 + + +**2、图书信息表** + +- 图书编号 +- 图书名称 +- 图书封面 +- 图书价格 +- 作者 +- 出版日期 +- 定价 +- 库存 +- 状态(订单上下架) +- ISBN +- 图书简介 +- 销量 + +**3、作者信息表** + +- 作者编号 +- 作者姓名 +- 性别 +- 头像 +- 简介 + +**4、图库** + +- 图片编号 +- 图片 +- 图片路径 +- 上传日期 + +**5、出版社** + +- 出版社编号 +- 出版社名称 +- 出版社地址 +- 联系电话 +- 电子邮箱 + +**6、用户** + +- 用户编号 +- 用户姓名 +- 账号 +- 密码 +- 角色 +- 手机号码 +- 收货地址 + +**7、用户收货地址** + +- 收货地址编号 +- 用户编号 +- 收货地址 + +**8、图书简介表** + +- 图书简介编号 +- 图书简介 + +PD思路图 + +CDM + +![image-20240914112232968](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409141122083.png) + +LDM + +![image-20240914112313167](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409141123330.png) + +PDM + +![image-20240914112412452](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409141124584.png) + +SQL语句 + +```sql +create database book1_online; + use book1_online; +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2024/9/14 9:53:42 */ +/*==============================================================*/ + + +drop table if exists address; + +drop table if exists author; + +drop table if exists book_info; + +drop table if exists book_intro; + +drop table if exists category; + +drop table if exists gallery; + +drop table if exists orders; + +drop table if exists publisher; + +drop table if exists user; + +/*==============================================================*/ +/* Table: address */ +/*==============================================================*/ +create table address +( + address_id int not null auto_increment, + user_id int not null, + pub_id int not null, + address varchar(255), + primary key (address_id) +); + +/*==============================================================*/ +/* Table: author */ +/*==============================================================*/ +create table author +( + author_id int not null auto_increment, + author_name varchar(10), + gender varchar(2), + heart int, + author_intro varchar(255), + primary key (author_id) +); + +/*==============================================================*/ +/* Table: book_info */ +/*==============================================================*/ +create table book_info +( + book_id int not null auto_increment, + pub_id int not null, + author_id int not null, + cate_id int not null, + cate int not null, + book_name varchar(25), + boook_cover int, + author varchar(25), + pub_time varchar(255), + price decimal, + stock int, + state varchar(255), + ISBN longtext, + book_intro longtext, + sale int, + primary key (book_id) +); + +/*==============================================================*/ +/* Table: book_intro */ +/*==============================================================*/ +create table book_intro +( + intro_id int not null auto_increment, + book_id int not null, + book_intro longtext, + primary key (intro_id) +); + +/*==============================================================*/ +/* Table: category */ +/*==============================================================*/ +create table category +( + cate_id int not null auto_increment, + cate_name varchar(10), + primary key (cate_id) +); + +/*==============================================================*/ +/* Table: gallery */ +/*==============================================================*/ +create table gallery +( + picture_id varchar(255) not null , + book_id int not null, + picture varchar(255), + path varchar(255), + pic_data varchar(255), + primary key (picture_id) +); + +/*==============================================================*/ +/* Table: orders */ +/*==============================================================*/ +create table orders +( + order_id int not null auto_increment, + user_id int not null, + order_time varchar(255), + order_money decimal, + order_state varchar(255), + primary key (order_id) +); + +/*==============================================================*/ +/* Table: publisher */ +/*==============================================================*/ +create table publisher +( + pub_id int not null auto_increment, + address_id int not null, + pub_name varchar(25), + pub_phone int, + pub_email varchar(255), + primary key (pub_id) +); + +/*==============================================================*/ +/* Table: user */ +/*==============================================================*/ +create table user +( + user_id int not null auto_increment, + user_name varchar(25), + admin varchar(255), + password int, + role varchar(25), + user_phone int, + user_address varchar(255), + primary key (user_id) +); + +alter table address add constraint FK_user_address foreign key (user_id) + references user (user_id) on delete restrict on update restrict; + +alter table book_info add constraint FK_bookinfo_author foreign key (author_id) + references author (author_id) on delete restrict on update restrict; + +alter table book_info add constraint FK_bookinfo_cate foreign key (cate_id) + references category (cate_id) on delete restrict on update restrict; + +alter table book_info add constraint FK_publisher_bookinfo foreign key (pub_id) + references publisher (pub_id) on delete restrict on update restrict; + +alter table book_intro add constraint FK_bookintro_bookinfo foreign key (book_id) + references book_info (book_id) on delete restrict on update restrict; + +alter table gallery add constraint FK_gallery_bookinfo foreign key (book_id) + references book_info (book_id) on delete restrict on update restrict; + +alter table orders add constraint FK_user_order foreign key (user_id) + references user (user_id) on delete restrict on update restrict; +``` + +添加数据 + +```sql +INSERT INTO `address` VALUES (1, 1, 1, '中国'); +INSERT INTO `address` VALUES (2, 2, 2, '北京'); +INSERT INTO `address` VALUES (3, 3, 3, '沈阳'); +INSERT INTO `address` VALUES (4, 4, 4, '北京'); +INSERT INTO `address` VALUES (5, 5, 1, '广西'); +INSERT INTO `address` VALUES (6, 6, 3, '福建'); +INSERT INTO `address` VALUES (7, 7, 2, '安徽'); + + +INSERT INTO `author` VALUES (1, '马特·弗里斯比', '男', 5, '好'); +INSERT INTO `author` VALUES (2, '明日科技', '男', 6, '好'); +INSERT INTO `author` VALUES (3, '约翰·冯·诺依曼', '男', 7, '好'); +INSERT INTO `author` VALUES (4, '余华', '男', 8, '好'); + + +INSERT INTO `book_info` VALUES (1, 1, 1, 1, 1, 'javascript', 1, '马特·弗里斯比', '2020-09-01', 1, 5852, '上架', '9787115545381', '讲解很详细', 568); +INSERT INTO `book_info` VALUES (2, 2, 2, 1, 1, 'Java从入门到精通(第7版)', 2, '明日科技', '2023-06', 56, 5456, '上架', '9787302632627', '很好', 852); +INSERT INTO `book_info` VALUES (3, 3, 3, 2, 2, ' 博弈论(精装版)', 3, '约翰·冯·诺依曼', '2020-07-13', 20, 25555, '未上架', '9787544181846', '很好', 0); +INSERT INTO `book_info` VALUES (4, 4, 4, 3, 3, '余华:我们生活在巨大的差距里', 4, '余华', '2018-04-27', 56, 55218, '上架', '147258369', '经典', 5555); + + +INSERT INTO `book_intro` VALUES (1, 1, '好'); +INSERT INTO `book_intro` VALUES (2, 2, '很好'); +INSERT INTO `book_intro` VALUES (3, 3, '非常好'); +INSERT INTO `book_intro` VALUES (4, 4, '经典'); + + +INSERT INTO `category` VALUES (1, '计算机'); +INSERT INTO `category` VALUES (2, '经济'); +INSERT INTO `category` VALUES (3, '文学'); + + +INSERT INTO `gallery` VALUES (1, 1, '1', 'path1', '2020-09-01'); +INSERT INTO `gallery` VALUES (2, 2, '2', 'path2', '2006-05-30'); +INSERT INTO `gallery` VALUES (3, 3, '3', 'path3', '2020-07-13'); +INSERT INTO `gallery` VALUES (4, 4, '4', 'path4', '2018-04-27'); + + +INSERT INTO `orders` VALUES (1, 5, '2024-9-14', 129, '未发货'); +INSERT INTO `orders` VALUES (2, 6, '2024-8-12', 50, '已发货'); +INSERT INTO `orders` VALUES (3, 7, '2024-6-16', 56, '已发货'); + + +INSERT INTO `publisher` VALUES (1, 1, '人民邮电出版社', 21562, 'renmin@qq.com'); +INSERT INTO `publisher` VALUES (2, 2, '清华大学出版社', 542365, 'qinhua@qq.com'); +INSERT INTO `publisher` VALUES (3, 3, '沈阳出版社', 147258, 'shenyang@qq.com'); +INSERT INTO `publisher` VALUES (4, 4, '北京出版集团', 258369, 'beijin@qq.com'); + + +INSERT INTO `user` VALUES (1, '人民邮电出版社会', 'renminyoudian', 123456, '出版社', 21562, '中国'); +INSERT INTO `user` VALUES (2, '清华大学出版社', 'qinhua', 542365, '出版社', 12345, '北京'); +INSERT INTO `user` VALUES (3, '沈阳出版社', 'shenyang', 147258, '出版社', 147258, '沈阳'); +INSERT INTO `user` VALUES (4, '北京出版集团', 'beijin', 258369, '出版社', 258369, '北京'); +INSERT INTO `user` VALUES (5, '韦大力', 'yahu', 66666, '购买者', 1885654, '广西'); +INSERT INTO `user` VALUES (6, '张三', 'zhangsan', 88888, '购买者', 1884952, '福建'); +INSERT INTO `user` VALUES (7, '李四', 'lisi', 8515632, '购买者', 5555694, '安徽'); + + +``` + +表格数据 + +![image-20240919171457270](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409191714379.png) + +情景模拟 + +1.查询有哪些书可以卖? + +```sql +SELECT book_name,state from book_info where state = '上架'; +``` + +![image-20240919165354682](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409191653789.png) + +2.查询韦大力买了什么书? + +```sql +SELECT user_name, book_name from user u +join address ar on u.user_id = ar.user_id +join book_info bi on bi.pub_id = ar.pub_id +where user_name ='韦大力'; +``` + +![image-20240919170313316](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409191703370.png) + +3.查询韦大力买了什么书?同时想要知道书的评价 + +```sql +SELECT user_name, book_name,bo.book_intro from user u +join address ar on u.user_id = ar.user_id +join book_info bi on bi.pub_id = ar.pub_id +join book_intro bo on bo.book_id = bi.book_id +where user_name ='韦大力'; +``` + +![image-20240919170920912](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409191709987.png) + diff --git "a/\345\271\262\347\272\257\346\254\243/2024.0920 \350\247\206\345\233\276\347\232\204\344\274\230\347\202\271\343\200\201\345\210\233\345\273\272\343\200\201\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\345\271\262\347\272\257\346\254\243/2024.0920 \350\247\206\345\233\276\347\232\204\344\274\230\347\202\271\343\200\201\345\210\233\345\273\272\343\200\201\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..d45d1617fc401d1f89b2fc78947cde0921b1ecde --- /dev/null +++ "b/\345\271\262\347\272\257\346\254\243/2024.0920 \350\247\206\345\233\276\347\232\204\344\274\230\347\202\271\343\200\201\345\210\233\345\273\272\343\200\201\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,263 @@ +# #20240920视图的优点、创建、增删改查 + +### 一、**为什么要有视图** + +- 视图能够简化用户的操作; +- 视图使用户能以多种角度看待同一数据; +- 视图对重构[数据库](https://cloud.tencent.com/solution/database?from_column=20065&from=20065)提供了一定程度的逻辑独立性; +- 视图能够对机密数据提供安全保护; +- 适当的利用视图可以更清晰的表达查询。 + +### 二、**视图与表的区别** + +- 视图是一个虚拟表,基于查询结果而创建,不占用物理存储空间; +- 视图可以简化复杂查询和提供数据安全性,但不能直接存储数据 +- 表是实际存储数据的结构 + +### 三、**视图的好处** + +- 可以重复利用 +- 安全性高 +- 减少网络开销 + +**创建视图 视图本身就是一种sql语句,也是一种表的形式** + +```sql +create view 视图名 as(表示从哪个表来) select 语句 from 表名 where 条件; +create view v_text1 as empno,ename,job from emp; +``` + +**如何分清视图与表**:show full tables; + +**查看创表语句,能通过表名反查创表语句** + +```sql +show create table v_text1 +``` + +**如何直接修改 v_text1?结构,多一个列** + +```sql +create or replace view v_text1 as select empno,ename,job ,sal from emp; +``` + +**如何修改视图的名称,比如v_text1改成v_text2** + +```sql +rename table v_text1 to v_text2 +``` + +**修改视图内容,将甘宁改成韦大力**(成功修改了视图里的值,对应基表中的内容同步修改,但是不是每一次对视图的修改都能成功并且影响到基表) + +```sql +select * from v_text1 + +update v_text1 set ename = '韦大力' where empno = 1001; + +insert into v_text100 values (1015,'韦大力','马楼',1006,now(),3500,null,20) +``` + +**修改视图内容数据**:能否修改,主要看视图的数据,是否与基表有对应, + +1. 基表有非空字段,但视图不存在这字段, +2. 有聚合函数, +3. 有group by having,union,union all, +4. 有子查询, +5. 有join, +6. 有distinct, +7. 视图中有常量字段 + +总结: + +改名:rename table 旧名 to 新 + +修改结构:alter view 名称 as 新 select 语句 + +​ create or replace view 名称 as 新select 语句 + +删除视图:drop view if exists 名称 + +**sql语句** + +```sql +-- 准备数据 +create database if not exists db_view; +use db_view; + +# 部门表 +create table dept( + deptno int primary key, + dname varchar(20), + loc varchar(20) +); +insert into dept values(10, '教研部','北京'), +(20, '学工部','上海'), +(30, '销售部','广州'), +(40, '财务部','武汉'); + +# 员工表 +create table emp( + empno int primary key, + ename varchar(20), + job varchar(20), + mgr int, + hiredate date, + sal numeric(8,2), + comm numeric(8, 2), + deptno int, +-- FOREIGN KEY (mgr) REFERENCES emp(empno), + FOREIGN KEY (deptno) REFERENCES dept(deptno) ON DELETE SET NULL ON UPDATE CASCADE +); +insert into emp values +(1001, '甘宁', '文员', 1013, '2000-12-17', 8000.00, null, 20), +(1002, '黛绮丝', '销售员', 1006, '2001-02-20', 16000.00, 3000.00, 30), +(1003, '殷天正', '销售员', 1006, '2001-02-22', 12500.00, 5000.00, 30), +(1004, '刘备', '经理', 1009, '2001-4-02', 29750.00, null, 20), +(1005, '谢逊', '销售员', 1006, '2001-9-28', 12500.00, 14000.00, 30), +(1006, '关羽', '经理', 1009, '2001-05-01', 28500.00, null, 30), +(1007, '张飞', '经理', 1009, '2001-09-01', 24500.00, null, 10), +(1008, '诸葛亮', '分析师', 1004, '2007-04-19', 30000.00, null, 20), +(1009, '曾阿牛', '董事长', null, '2001-11-17', 50000.00, null, 10), +(1010, '韦一笑', '销售员', 1006, '2001-09-08', 15000.00, 0.00, 30), +(1011, '周泰', '文员', 1008, '2007-05-23', 11000.00, null, 20), +(1012, '程普', '文员', 1006, '2001-12-03', 9500.00, null, 30), +(1013, '庞统', '分析师', 1004, '2001-12-03', 30000.00, null, 20), +(1014, '黄盖', '文员', 1007, '2002-01-23', 13000.00, null, 10); + +# 工资等级表 +create table salgrade( + grade int primary key, + losal int, + hisal int +); +insert into salgrade values +(1, 7000, 12000), +(2, 12010, 14000), +(3, 14010, 20000), +(4, 20010, 30000), +(5, 30010, 99990); +``` + +1. 创建一个视图,显示所有部门的名称以及属于该部门的员工姓名 + + ```sql + create view v_view1 as select dname,ename from emp join dept on dept.deptno= emp.deptno; + ``` + + ![image-20240920141255854](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201412901.png) + + image-20240920141322284 + + + + + +2. 创建一个视图,显示员工姓名、职位、薪水和佣金。 + +```sql +create view v_view2 as select ename,job,sal,comm from emp join dept on dept.deptno= emp.deptno; +``` + +![image-20240920141457410](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201414442.png) + +3.创建一个视图,显示所有薪水大于 20000 的员工姓名、职位和薪水 + +```sql +create view v_view3 as select ename,job,sal from emp join dept on dept.deptno= emp.deptno where sal > 20000; +``` + +![image-20240920142305731](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201423753.png) + +4.创建按部门分类的员工数量视图 + +```sql +create view v_view4 as select dname,count(1) from emp join dept on dept.deptno= emp.deptno GROUP BY dept.dname ; +``` + +![image-20240920142643215](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201426233.png) + +5.创建一个视图,显示员工姓名、薪水及其所属的薪资等级。 + +```sql +create view v_view5 as select ename,sal,sa.grade from emp join salgrade sa on emp.sal BETWEEN sa.losal and sa.hisal; + +``` + +![image-20240920143146557](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201431607.png) + +6. 创建一个视图,显示每个经理及其直接下属员工的姓名。 + +```sql + create view v_view6 as select em1.ename 经理名字,em2.ename 下属名字 from emp em1 join emp em2 on em2.mgr=em1.empno where em1.job='经理'; +``` + +![image-20240920151910414](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201519437.png) + +7.创建一个视图,显示每个部门的名称及其平均薪水。 + +```sql + create view v_view7 as select dname,avg(emp.sal) from emp join dept on dept.deptno= emp.deptno GROUP BY emp.deptno; +``` + +![image-20240920143253017](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201433494.png) + +8.创建一个视图,显示在 2001 年以后入职的员工姓名和入职日期。 + +```sql +create view v_view8 as SELECT ename,hiredate from emp where hiredate > '2001-1-1'; +``` + +![image-20240920143544638](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201435661.png) + +9.删除一个已创建的视图。 + +```sql +drop view if exists v_view3; +``` + +![image-20240920143615244](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201436304.png) + +10.修改第一个视图的名称 + +```sql +rename table v_view1 to v_view10; +``` + +11.修改第二个视图,显示员工姓名、职位、部门。 + +```sql +create or replace view v_view20 as select ename,job,dname from emp join dept on dept.deptno= emp.deptno; +``` + +**-- 作业** +-- 1:查询部门平均薪水最高的部门名称 + +```sql +select dname,avg(sal) from emp +join dept on dept.deptno = emp.deptno +GROUP BY emp.deptno +order by avg(emp.sal) desc limit 1; +``` + +![image-20240920143907093](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201439114.png) + +-- 2:查询员工比所属领导薪资高的部门名、员工名、员工领导编号 + +```sql +select de.dname,em1.ename,em1.empno from emp em1 + join dept de on de.deptno= em1.deptno + where em1.sal > (select em2.sal from emp em2 where em1.mgr = em2.empno); +``` + +![image-20240920150414684](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201504731.png) + +-- 3:查询工资等级为4级,2000年以后入职的工作地点为上海的员工编号、姓名和工资 + +```sql +select sal.grade,em.empno, em.ename,em.sal from emp em +join dept de on de.deptno = em.deptno +join salgrade sal on em.sal between sal.losal and sal.hisal where sal.grade = 4 and em.hiredate > '200-1-1' and de.loc = '上海' ; + +``` + +![image-20240920150338462](https://gitee.com/gcxxl/note-sheet-bed/raw/master/images/202409201503487.png) \ No newline at end of file