爬虫学习笔记(十三)数据存储——mysql 2020.5.17

前言

本节学习mysql

1、安装

教程很多了
比如
https://blog.csdn.net/aa541505/article/details/103147115?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158926711519724811830210%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.57652%2522%257D&request_id=158926711519724811830210&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-19-103147115.nonecase&utm_term=mysql
https://blog.csdn.net/bobo553443/article/details/81383194?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158926711519724811830210%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.57652%2522%257D&request_id=158926711519724811830210&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-4-81383194.nonecase&utm_term=mysql

2、简单介绍

三范式

  • 列不可拆分
  • 唯一标志
  • 引用主键

数据完整性

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

数据库启动

在这里插入图片描述

3、操作

数据库操作

create database animal charset=utf8;# 创建数据库
show databases; #查看所有库
drop database animal; #删除数据库
select database(); #查看当前库
use animal; #切换数据库

表操作

show tables; #查看所有表
show create table dog; #查看创建帮助
create table dog(
	id int auto_increment primary key,
	name varchar(10) not null
	); #创建表
alter table dog add gender bit; #修改表
desc dog; #查看表结构
rename table dog to cat; #改表名

数据操作

# 增
insert into dog values(1,"拉布拉多",0); #插入数据
insert into dog(name,gender) values("小猎犬",1); #缺省插入
insert into dog values(3,"腊肠",0),(4,"约克夏"1); #批量插入
# 改
update dog set name="泰迪" where id=4;
# 查
select * from dog;
# 删
delete from dog where name="腊肠"; #物理删除,删完id不变
alter table dog add isdelete bit default 0; #搞个标志位
update dog set isdelete=1 where id=2; #逻辑删除

4、查询

先搞个库

create table stu(
     id int auto_increment primary key,
     name varchar(10) not null,
     birthday datetime,
     gender bit default 0,
     isdelete bit default 0,
     address varchar(100),
     score int
     );
insert into stu values(1,"小明","2008-01-01",0,0,"北京",90),
(2,"小红","2007-01-01",1,0,"上海",80),
(3,"小兰","2006-01-01",1,0,"广州",100),
(4,"小王","2005-01-01",0,0,"深圳",20),
(5,"老王","2009-01-01",0,0,null,30),
(6,"老刘","2004-01-01",0,0,null,40),
(7,"小丽","2003-01-01",1,0,"东莞",50),
(8,"小芳","2002-01-01",1,0,"福建",60),
(9,"小粒","2001-01-01",0,0,"福州",70);

条件运算

select * from stu where id>=6;
select name from stu where id!=6;

逻辑运算

select name from stu where id>6 and gender=1;
select name from stu where id<4 or gender=0; 

模糊查询

select * from stu where name like "小%"; #%表示任意多个字符

范围查询

select * from stu where id in(1,3,5,7);
select * from stu where id between 1 and 5;
select * from stu where address is null;

聚合查询

select count(*) from stu  where gender=0; #个数
select Max(score) from stu; #最大值
select sum(score) from stu where gender=1; #求和
select avg(score) from stu where gender=0; #求平均

分组查询

select gender as "性别", count(*) from stu group by gender; #男生女生的总数

排序

select * from stu where isdelete=0 order by score desc; #降序,升序用asc

分页

select * from stu limit 0,3; #从0开始查3个
select * from stu limit 3,2; #从3开始查2个

去重

select distinct adress from stu;

5、表间关系

先建几个表

# 科目表
create table subjects(
	id int auto_increment primary key not null,
	title varchar(10) not null
	);
insert into subjects values(0,"语文"),
	(0,"数学"),
	(0,"英语"),
	(0,"科学");
# 花名册
create table stu(
     id int auto_increment primary key not null,
     name varchar(10) not null,
     birthday datetime,
     gender bit default 0,
     isdelete bit default 0,
     address varchar(100),
     score int(10)
     );
insert into stu values(1,"小明","2008-01-01",0,0,"北京",90),
(2,"小红","2007-01-01",1,0,"上海",80),
(3,"小兰","2006-01-01",1,0,"广州",100),
(4,"小王","2005-01-01",0,0,"深圳",20),
(5,"老王","2009-01-01",0,0,null,30),
(6,"老刘","2004-01-01",0,0,null,40),
(7,"小丽","2003-01-01",1,0,"东莞",50),
(8,"小芳","2002-01-01",1,0,"福建",60),
(9,"小粒","2001-01-01",0,0,"福州",70);
# 分数表
create table scores(
	id int primary key auto_increment,
	stuid int, #对应花名册id
	subid int, #对应科目表id
	score decimal(5,2), #小数,5位数,保留1位
	foreign key(stuid) references stu(id), #外键关联
	foreign key(subid) references subjects(id)
	); 
insert into scores values(0,1,1,80),(0,2,2,60),(0,2,3,70),(0,3,1,90),(0,4,4,60),(0,5,2,75);

链接查询

# 每个学生每个学科的成绩
select stu.name,subjucts.title,scores.score from scores inner join stu on scores.stuid=stu.id inner join subjects on scores.subid=subjects.id; 
# 左链接
select subjucts.title,scores.score from scores right join subjects on scores.subid=subjects.id; 

例子

select stu.name,subjects.title,avg(scores.score),max(scores.score)
     from scores
     inner join stu on scores.stuid=stu.id
     inner join subjects on scores.subid=subjects.id
     group by stu.name;

6、视图

用于对复杂查询的封装

# 创建视图
create view studentscore as select stu.name,scores.score from scores inner join stu on scores.stuid=sut.id;
# 使用视图
select * from studentscore;
# 删除
drop view studentscore;

7、查询效率

  • 数据类型越小越好
  • 能用整型最好
  • 用0来代替null

索引

# 创建索引
create index name on tablename(key(length));
# 查看索引
show index from tablename;
# 删除索引
drop index name on tablename;

8、备份

# 备份
mysqldump -u root -p db_name table_name > 备份文件的绝对路径
# 恢复
mysql -u root -p db_name < 备份文件的绝对路径

9、与python交互

# 安装
# pip install pymysql
import pymysql
try:
    # 1.链接 数据库  链接对象 connection()
    conn = pymysql.Connect(
        host="localhost",
        port=3306,
        db='animal',
        user='root',
        passwd="mysql",
        charset='utf8'
    )
    # 2. 创建 游标对象 cursor()
    cur = conn.cursor()
    # 增加一条数据 科目表--GO语言
    insert_sub = 'insert into subjects values(0,"GO语言")'
    result = cur.execute(insert_sub)
    print(result)
    # 修改
    update_sub = 'update subjects set title="区块链" where id=7'
    result = cur.execute(update_sub)
    print(result)
    # 删除
    delete_sub = 'delete from  stu  where id=8'
    result = cur.execute(delete_sub)
    print(result)
    # 查找
    find_sub = 'select * from subjects where id=1'
    cur.execute(find_sub)
    result = cur.fetchone() #一行数据一个元组
    print(result)
    find_sub = 'select * from subjects'
    cur.execute(find_sub)
    result = cur.fetchall() #一个大元组包含多个小元组
    print(result)
    # 提交事务
    conn.commit()
    # 关闭游标
    cur.close()
    # 关闭链接
    conn.close()
except Exception as e:
    print(e)

结语

对mysql做了个简单学习

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章