Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Python-100-Days
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huangkq
Python-100-Days
Commits
c6666f45
Commit
c6666f45
authored
Jun 03, 2019
by
jackfrued
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新了数据库阶段的文档
parent
3ad03f02
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
45 deletions
+63
-45
NoSQL入门.md
Day36-40/NoSQL入门.md
+2
-4
关系型数据库MySQL.md
Day36-40/关系型数据库MySQL.md
+61
-41
No files found.
Day36-40/NoSQL入门.md
View file @
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
View file @
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数据库。
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment