Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Lawrencezf
Python-100-Days
提交
c6666f45
P
Python-100-Days
项目概览
Lawrencezf
/
Python-100-Days
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python-100-Days
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c6666f45
编写于
6月 04, 2019
作者:
J
jackfrued
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新了数据库阶段的文档
上级
3ad03f02
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
63 addition
and
45 deletion
+63
-45
Day36-40/NoSQL入门.md
Day36-40/NoSQL入门.md
+2
-4
Day36-40/关系型数据库MySQL.md
Day36-40/关系型数据库MySQL.md
+61
-41
未找到文件。
Day36-40/NoSQL入门.md
浏览文件 @
c6666f45
...
...
@@ -345,8 +345,6 @@ mongo --host 172.18.61.250
MongoDB shell version v3.6.5
connecting to: mongodb://172.18.61.250:27017/
...
>
```
1.
查看、创建和删除数据库。
...
...
@@ -544,4 +542,4 @@ python3
>>>
```
关于PyMongo更多的知识可以通过它的
[
官方文档
](
https://api.mongodb.com/python/current/tutorial.html
)
进行了解。
\ No newline at end of file
关于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/
>
)
都是不错的选择。
\ No newline at end of file
Day36-40/关系型数据库MySQL.md
浏览文件 @
c6666f45
...
...
@@ -87,6 +87,12 @@
netstat -nap | grep mysql
```
也可以使用下面的命令查找是否有名为mysqld的进程。
```Shell
pgrep mysqld
```
-
使用MySQL客户端工具连接服务器。
命令行工具:
...
...
@@ -177,35 +183,32 @@
-- 创建学院表
create table tb_college
(
collid int not null auto_increment comment '编号',
collname varchar(50) not null comment '名称',
collmaster varchar(20) not null comment '院长',
collweb varchar(511) default '' comment '网站',
collid int auto_increment comment '编号',
collname varchar(50) not null comment '名称',
collmaster varchar(20) not null comment '院长',
primary key (collid)
);
-- 创建学生表
create table tb_student
(
stuid
int not null comment '学号',
stuname
varchar(20) not null comment '姓名',
stusex
bit
default 1 comment '性别',
stubirth
date not null comment '出生日期',
stuaddr
varchar(255) default '' comment '籍贯',
collid
int not null comment '所属学院',
stuid
int not null comment '学号',
stuname
varchar(20) not null comment '姓名',
stusex
boolean
default 1 comment '性别',
stubirth
date not null comment '出生日期',
stuaddr
varchar(255) default '' comment '籍贯',
collid
int not null comment '所属学院',
primary key (stuid),
foreign key (collid) references tb_college (collid)
);
-- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid);
-- 创建教师表
create table tb_teacher
(
teaid
int not null comment '工号',
teaname
varchar(20) not null comment '姓名',
teatitle
varchar(10) default '助教' comment '职称',
collid
int not null comment '所属学院',
teaid
int not null comment '工号',
teaname
varchar(20) not null comment '姓名',
teatitle
varchar(10) default '助教' comment '职称',
collid
int not null comment '所属学院',
primary key (teaid),
foreign key (collid) references tb_college (collid)
);
...
...
@@ -213,40 +216,37 @@
-- 创建课程表
create table tb_course
(
couid
int not null comment '编号',
couname
varchar(50) not null comment '名称',
coucredit
int not null comment '学分',
teaid
int not null comment '授课老师',
couid
int not null comment '编号',
couname
varchar(50) not null comment '名称',
coucredit
int not null comment '学分',
teaid
int not null comment '授课老师',
primary key (couid),
foreign key (teaid) references tb_teacher (teaid)
);
-- 创建选课记录表
create table tb_
score
create table tb_
record
(
scid int auto_increment comment '选课记录编号',
stuid int not null comment '选课学生',
couid int not null comment '所选课程',
scdate datetime comment '选课时间日期',
scmark decimal(4,1) comment '考试成绩',
primary key (scid),
foreign key (stuid) references tb_student (stuid),
foreign key (couid) references tb_course (couid)
recid int auto_increment comment '选课记录编号',
sid int not null comment '选课学生',
cid int not null comment '所选课程',
seldate datetime default now() comment '选课时间日期',
score decimal(4,1) comment '考试成绩',
primary key (recid),
foreign key (sid) references tb_student (stuid),
foreign key (cid) references tb_course (couid),
unique (sid, cid)
);
-- 添加唯一性约束(一个学生选某个课程只能选一次)
alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid);
```
2.
DML
```
SQL
-- 插入学院数据
insert into tb_college (collname, collmaster
, collweb
) values
('计算机学院', '左冷禅'
, 'http://www.abc.com'
),
('外国语学院', '岳不群'
, 'http://www.xyz.com'
),
('经济管理学院', '风清扬'
, 'http://www.foo.com'
);
insert into tb_college (collname, collmaster) values
('计算机学院', '左冷禅'),
('外国语学院', '岳不群'),
('经济管理学院', '风清扬');
-- 插入学生数据
insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values
...
...
@@ -289,7 +289,7 @@
(9999, '审计学', 3, 3366);
-- 插入选课数据
insert into tb_
score (stuid, couid, scdate, scmark
) values
insert into tb_
record (sid, cid, seldate, score
) values
(1001, 1111, '2017-09-01', 95),
(1001, 2222, '2017-09-01', 87.5),
(1001, 3333, '2017-09-01', 100),
...
...
@@ -304,9 +304,9 @@
(1378, 1111, '2017-09-05', 82),
(1378, 7777, '2017-09-02', 65.5),
(2035, 7777, '2018-09-03', 88),
(2035, 9999,
curdate()
, null),
(3755, 1111, d
ate(now())
, null),
(3755, 8888, d
ate(now())
, null),
(2035, 9999,
default
, null),
(3755, 1111, d
efault
, null),
(3755, 8888, d
efault
, null),
(3755, 9999, '2017-09-01', 92);
```
...
...
@@ -460,6 +460,26 @@
-
隔离性:多个事务并发执行时,一个事务的执行不应影响其他事务的执行
-
持久性:已被提交的事务对数据库的修改应该永久保存在数据库中
3.
MySQL中的事务操作
-
开启事务环境
```SQL
start transaction
```
-
提交事务
```Shell
commit
```
-
回滚事务
```SQL
rollback
```
### Python数据库编程
我们用如下所示的数据库来演示在Python中如何访问MySQL数据库。
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录