Skip to content

Commit c6666f4

Browse files
committed
更新了数据库阶段的文档
1 parent 3ad03f0 commit c6666f4

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

Day36-40/NoSQL入门.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ mongo --host 172.18.61.250
345345

346346
MongoDB shell version v3.6.5
347347
connecting to: mongodb://172.18.61.250:27017/
348-
...
349-
>
350348
```
351349

352350
1. 查看、创建和删除数据库。
@@ -544,4 +542,4 @@ python3
544542
>>>
545543
```
546544

547-
关于PyMongo更多的知识可以通过它的[官方文档](https://api.mongodb.com/python/current/tutorial.html)进行了解。
545+
关于PyMongo更多的知识可以通过它的[官方文档](https://api.mongodb.com/python/current/tutorial.html)进行了解,也可以使用[MongoEngine](<https://pypi.org/project/mongoengine/>)这样的库来简化Python程序对MongoDB的操作,除此之外,还有以异步I/O方式访问MongoDB的三方库[motor](<https://pypi.org/project/motor/>)都是不错的选择

Day36-40/关系型数据库MySQL.md

+61-41
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
netstat -nap | grep mysql
8888
```
8989

90+
也可以使用下面的命令查找是否有名为mysqld的进程。
91+
92+
```Shell
93+
pgrep mysqld
94+
```
95+
9096
- 使用MySQL客户端工具连接服务器。
9197

9298
命令行工具:
@@ -177,76 +183,70 @@
177183
-- 创建学院表
178184
create table tb_college
179185
(
180-
collid int not null auto_increment comment '编号',
181-
collname varchar(50) not null comment '名称',
182-
collmaster varchar(20) not null comment '院长',
183-
collweb varchar(511) default '' comment '网站',
186+
collid int auto_increment comment '编号',
187+
collname varchar(50) not null comment '名称',
188+
collmaster varchar(20) not null comment '院长',
184189
primary key (collid)
185190
);
186191
187192
-- 创建学生表
188193
create table tb_student
189194
(
190-
stuid int not null comment '学号',
191-
stuname varchar(20) not null comment '姓名',
192-
stusex bit default 1 comment '性别',
193-
stubirth date not null comment '出生日期',
194-
stuaddr varchar(255) default '' comment '籍贯',
195-
collid int not null comment '所属学院',
195+
stuid int not null comment '学号',
196+
stuname varchar(20) not null comment '姓名',
197+
stusex boolean default 1 comment '性别',
198+
stubirth date not null comment '出生日期',
199+
stuaddr varchar(255) default '' comment '籍贯',
200+
collid int not null comment '所属学院',
196201
primary key (stuid),
197202
foreign key (collid) references tb_college (collid)
198203
);
199204
200-
-- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid);
201-
202205
-- 创建教师表
203206
create table tb_teacher
204207
(
205-
teaid int not null comment '工号',
206-
teaname varchar(20) not null comment '姓名',
207-
teatitle varchar(10) default '助教' comment '职称',
208-
collid int not null comment '所属学院',
208+
teaid int not null comment '工号',
209+
teaname varchar(20) not null comment '姓名',
210+
teatitle varchar(10) default '助教' comment '职称',
211+
collid int not null comment '所属学院',
209212
primary key (teaid),
210213
foreign key (collid) references tb_college (collid)
211214
);
212215
213216
-- 创建课程表
214217
create table tb_course
215218
(
216-
couid int not null comment '编号',
217-
couname varchar(50) not null comment '名称',
218-
coucredit int not null comment '学分',
219-
teaid int not null comment '授课老师',
219+
couid int not null comment '编号',
220+
couname varchar(50) not null comment '名称',
221+
coucredit int not null comment '学分',
222+
teaid int not null comment '授课老师',
220223
primary key (couid),
221224
foreign key (teaid) references tb_teacher (teaid)
222225
);
223226
224227
-- 创建选课记录表
225-
create table tb_score
228+
create table tb_record
226229
(
227-
scid int auto_increment comment '选课记录编号',
228-
stuid int not null comment '选课学生',
229-
couid int not null comment '所选课程',
230-
scdate datetime comment '选课时间日期',
231-
scmark decimal(4,1) comment '考试成绩',
232-
primary key (scid),
233-
foreign key (stuid) references tb_student (stuid),
234-
foreign key (couid) references tb_course (couid)
230+
recid int auto_increment comment '选课记录编号',
231+
sid int not null comment '选课学生',
232+
cid int not null comment '所选课程',
233+
seldate datetime default now() comment '选课时间日期',
234+
score decimal(4,1) comment '考试成绩',
235+
primary key (recid),
236+
foreign key (sid) references tb_student (stuid),
237+
foreign key (cid) references tb_course (couid),
238+
unique (sid, cid)
235239
);
236-
237-
-- 添加唯一性约束(一个学生选某个课程只能选一次)
238-
alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid);
239240
```
240241
241242
2. DML
242243
243244
```SQL
244-
245245
-- 插入学院数据
246-
insert into tb_college (collname, collmaster, collweb) values
247-
('计算机学院', '左冷禅', 'http://www.abc.com'),
248-
('外国语学院', '岳不群', 'http://www.xyz.com'),
249-
('经济管理学院', '风清扬', 'http://www.foo.com');
246+
insert into tb_college (collname, collmaster) values
247+
('计算机学院', '左冷禅'),
248+
('外国语学院', '岳不群'),
249+
('经济管理学院', '风清扬');
250250
251251
-- 插入学生数据
252252
insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values
@@ -289,7 +289,7 @@
289289
(9999, '审计学', 3, 3366);
290290
291291
-- 插入选课数据
292-
insert into tb_score (stuid, couid, scdate, scmark) values
292+
insert into tb_record (sid, cid, seldate, score) values
293293
(1001, 1111, '2017-09-01', 95),
294294
(1001, 2222, '2017-09-01', 87.5),
295295
(1001, 3333, '2017-09-01', 100),
@@ -304,9 +304,9 @@
304304
(1378, 1111, '2017-09-05', 82),
305305
(1378, 7777, '2017-09-02', 65.5),
306306
(2035, 7777, '2018-09-03', 88),
307-
(2035, 9999, curdate(), null),
308-
(3755, 1111, date(now()), null),
309-
(3755, 8888, date(now()), null),
307+
(2035, 9999, default, null),
308+
(3755, 1111, default, null),
309+
(3755, 8888, default, null),
310310
(3755, 9999, '2017-09-01', 92);
311311
```
312312
@@ -460,6 +460,26 @@
460460
- 隔离性:多个事务并发执行时,一个事务的执行不应影响其他事务的执行
461461
- 持久性:已被提交的事务对数据库的修改应该永久保存在数据库中
462462
463+
3. MySQL中的事务操作
464+
465+
- 开启事务环境
466+
467+
```SQL
468+
start transaction
469+
```
470+
471+
- 提交事务
472+
473+
```Shell
474+
commit
475+
```
476+
477+
- 回滚事务
478+
479+
```SQL
480+
rollback
481+
```
482+
463483
### Python数据库编程
464484
465485
我们用如下所示的数据库来演示在Python中如何访问MySQL数据库。

0 commit comments

Comments
 (0)