From 5495d3af5a375bc5c6b0a2d8d204580d7e454738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Thu, 7 Sep 2023 14:00:36 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈梦梦 <3195337478@qq.com> --- .../20230907.md" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "01 \351\231\210\346\242\246\346\242\246/20230907.md" diff --git "a/01 \351\231\210\346\242\246\346\242\246/20230907.md" "b/01 \351\231\210\346\242\246\346\242\246/20230907.md" new file mode 100644 index 0000000..21f61b6 --- /dev/null +++ "b/01 \351\231\210\346\242\246\346\242\246/20230907.md" @@ -0,0 +1,103 @@ +2023年09月07日 + +#### 数据库的范式 + +##### 1.第一范式:要求字段的内容不可再分割,为的是保证数据的原子性 + +##### 2.第二范式:要求在满足第一范式的基础上,要求非主键字段完全依赖主键(非主键要依赖整个联合主键)而不能只依赖部分 + +##### 3.第三范式:满足第二范式的前提上,要求非主键属性要直接依赖于主键 + +```mysql +drop database if exists mxdx; +create database mxdx charset utf8; +use mxdx; + +#院系表 +create table college ( +col_id int primary key auto_increment, +col_name char(10) +); + +#专业表 +create table major ( +m_id int primary key auto_increment, +m_name char(10), +col_id int, +foreign key (col_id) references college (col_id) +); + +#班级表 +create table clazz ( +cl_id int primary key auto_increment, +cl_name char(10), +m_id int, +foreign key (m_id) references major (m_id) +); + +#课程信息 +create table course ( +cou_id int primary key auto_increment, +cou_name char(10) +); + + + +#教室 +create table classroom ( +cr_id int primary key auto_increment, +cr_address char(10) +); + + + +#课程表 +create table timetable ( +tt_id int primary key auto_increment, +tt_time char(3), +tt_order char(3), +cou_id int, +foreign key (cou_id) references course (cou_id), +cr_id int, +foreign key (cr_id) references classroom (cr_id) +); + + +#教师表 +create table teacher ( +te_id int primary key auto_increment, +te_name char(3), +te_sex char(1), +cou_id int, +foreign key (cou_id) references course (cou_id) +); + + + +#学生表 +create table student ( +st_id int primary key auto_increment, +st_name char(10), +st_sex char(1), +cl_id int, +foreign key (cl_id) references clazz (cl_id), +tt_id int, +foreign key (tt_id) references timetable (tt_id) +); + + +#选修表 +create table choose( +ch_id int primary key auto_increment, +ch_socre char(3), +tt_id int, +foreign key (tt_id) references timetable (tt_id), +st_id int, +foreign key (st_id) references student (st_id) +); + + + + +``` + -- Gitee From c1d1aed3f2b331eb2286e987f91801fd7b8f04e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Mon, 11 Sep 2023 12:37:22 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2001?= =?UTF-8?q?=20=E9=99=88=E6=A2=A6=E6=A2=A6/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "01 \351\231\210\346\242\246\346\242\246/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "01 \351\231\210\346\242\246\346\242\246/.keep" diff --git "a/01 \351\231\210\346\242\246\346\242\246/.keep" "b/01 \351\231\210\346\242\246\346\242\246/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From b3489a0172c8ccb0413ad93e6734a89fe6361e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=A2=A6?= <3195337478@qq.com> Date: Mon, 11 Sep 2023 12:38:08 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E8=BF=99=E6=98=AF=E9=99=88=E6=A2=A6?= =?UTF-8?q?=E6=A2=A6=E7=9A=84=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈梦梦 <3195337478@qq.com> --- .../20230908.md" | 242 ++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 "01 \351\231\210\346\242\246\346\242\246/20230908.md" diff --git "a/01 \351\231\210\346\242\246\346\242\246/20230908.md" "b/01 \351\231\210\346\242\246\346\242\246/20230908.md" new file mode 100644 index 0000000..4b47fa1 --- /dev/null +++ "b/01 \351\231\210\346\242\246\346\242\246/20230908.md" @@ -0,0 +1,242 @@ +2023 年09月08日 + + 今天上课进行了本周的专业课复习 + +主要复习了三个方面 + +### 1.表与表的关系 + +1.一对一 + +将任一表的主键放入另外一个表当外键 + +2.一对多 + +将一的主键当多的外键 + +3.多对多 + +引入第三张表(关系表)将前面两个表的主键当该表的外键 + +### 2.E_R图(实体关系图) + +实体、属性、关系 + +### 3.范式 + +1.第一范式 + +要求字段的内容,不可再分割,为的是保证数据的原子性 + +2.第二范式 + +要求在第一范式的基础上,要求非主键字段属性完全依赖主键(非主键要依赖整个联合主键)而不是只依赖部分(联合主键) + +完全函数依赖 +定义:设X,Y是关系R的两个属性集合,X’是X的真子集,存在X→Y,但对每一个X’都有X’!→Y,则称Y完全函数依赖于X。 + +比如通过学号->姓名 + +部分函数依赖 +定义:设X,Y是关系R的两个属性集合,存在X→Y,若X’是X的真子集,存在X’→Y,则称Y部分函数依赖于X。 + +需要借用知乎刘慰教师的例子用一下,自己也理解了很长时间。 + +![img](https://img-blog.csdnimg.cn/img_convert/0099ee963b99b2a08f78af70bbcab552.png) + +https://img-blog.csdnimg.cn/img_convert/0099ee963b99b2a08f78af70bbcab552.png + +码用(学号+课程),为什么要加课程呢?因为不同课程成绩是通过学号查不出来的。 + +不过用(学号+课程)当作码是不是有些问题? + +(学号+课程)->姓名,但是学号->姓名 + +(学号+课程)->系名,但是学号->系名 + +(学号+课程)->系主任,但是学号->系主任 + +这个就是部分依赖,说实话我看定义一脸懵逼。 + +要是上面那张表符合第二范式。需要将表拆分为两张表。 + +一张是 学号、课程、分数表 + +另外一张是 学号、姓名、系名、系主任表 + +![img](https://img-blog.csdnimg.cn/img_convert/94a4230520a842bf949eb83d490ec24e.png) + +https://img-blog.csdnimg.cn/img_convert/94a4230520a842bf949eb83d490ec24e.png + +3.第三范式 + +满足第二范式的前提下,要求非主键属性要直接依赖于主键(不能出现传递性依赖,被依赖可单独弄个表) + +传递函数依赖 +设X,Y,Z是关系R中互不相同的属性集合,存在X→Y(Y !→X),Y→Z,则称Z传递函数依赖于X。 + +https://blog.csdn.net/rl529014/article/details/48391465 + +采用这位大佬的例子 +在关系R(学号 ,宿舍, 费用)中,(学号)->(宿舍),宿舍!=学号,(宿舍)->(费用),费用!=宿舍,所以符合传递函数的要求 + +第三范式 +满足第二范式的条件下不存在传递函数依赖。 + +要满足第三范式,在分成两张表的时候第二张表还是有问题? + +学号->系名,系名->系主任 传递依赖。 + +需要将系名和系主任另外新建一张表。 + +![img](https://img-blog.csdnimg.cn/img_convert/dfe5d8f3dcb41d4e1c7eebd4ee8f3335.png) + +https://img-blog.csdnimg.cn/img_convert/dfe5d8f3dcb41d4e1c7eebd4ee8f3335.png + +总结: +第一范式:简单说 列不能再分 + +第二范式:简单说 建立在第一范式基础上,消除部分依赖 + +第三范式:简单说 建立在第二范式基础上,消除传递依赖。 +———————————————— +版权声明:本文为CSDN博主「萝卜头LJW」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 +原文链接:https://blog.csdn.net/u013164931/article/details/79692402 + + + +### 概念模型 + +常见单词意思: + +seral 自增 + +Charachers 字符串 + +cocle字段名英文 + +integer 数字 + +variable char 可变长度 + +decimal 小数 + +float 整数 + +number 数字 + +#### 概念模型 + +##### 1.创建概念模型(类似ER图) + +以用户为角度(CDM) + +记得弄关系,关系表可以不弄,会自动生成 + +##### 2.转换成逻辑模型 + +以计算机的角度(LDM) + +在CDM中点tools,下一步点GLDM + +##### 3.转换物理模型 + +以数据库角度(PDM) + +tools,下一步点GPDM + +##### 4.生成DDL + +database+generation(记得改成对应mysql的版本,否则报错) + +```mysql +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023-09-11 20:34:27 */ +/*==============================================================*/ + + +drop table if exists Relationship_1; + +drop table if exists book; + +drop table if exists librarian; + +drop table if exists library; + +drop table if exists reader; + +/*==============================================================*/ +/* Table: Relationship_1 */ +/*==============================================================*/ +create table Relationship_1 +( + re_id int not null, + book_id int not null, + primary key (re_id, book_id) +); + +/*==============================================================*/ +/* Table: book */ +/*==============================================================*/ +create table book +( + book_id int not null, + li_id int not null, + book_name char(15) not null, + press char(10) not null, + time national varchar(15) not null, + primary key (book_id) +); + +/*==============================================================*/ +/* Table: librarian */ +/*==============================================================*/ +create table librarian +( + id int not null, + li_id int not null, + name char(5) not null, + primary key (id) +); + +/*==============================================================*/ +/* Table: library */ +/*==============================================================*/ +create table library +( + li_id int not null, + id int not null, + li_name char(10) not null, + li_address char(10) not null, + primary key (li_id) +); + +/*==============================================================*/ +/* Table: reader */ +/*==============================================================*/ +create table reader +( + re_id int not null, + re_name char(4) not null, + primary key (re_id) +); + +alter table Relationship_1 add constraint FK_Relationship_1 foreign key (re_id) + references reader (re_id) on delete restrict on update restrict; + +alter table Relationship_1 add constraint FK_Relationship_2 foreign key (book_id) + references book (book_id) on delete restrict on update restrict; + +alter table book add constraint FK_Relationship_5 foreign key (li_id) + references library (li_id) on delete restrict on update restrict; + +alter table librarian add constraint FK_Relationship_3 foreign key (li_id) + references library (li_id) on delete restrict on update restrict; + +alter table library add constraint FK_Relationship_4 foreign key (id) + references librarian (id) on delete restrict on update restrict; + + +``` + -- Gitee