Python--mysql的使用

Python–mysql的使用

一、Mysql的基本使用
1.1 数据库应用的基本操作

查看当前有哪些数据库: show databases;
这里写图片描述

进入数据库: use RUNOOB;
ps:RUNOOB为可变,按实际数据库名替换即可。
这里写图片描述

查看进入到当前库中有哪些表:show tables;
这里写图片描述

查看表数据(例如:runoob_tbl) : select * from runoob_tbl;
这里写图片描述

1.2 Mysql 的基本查、增、改、删
select 查询语句:

select 列名 from 表名 where 条件判断
select * from runoob_tbl where runoob_id = 1;

这里写图片描述

GROUP BY和HAVING使用:

group by 用于筛选出一样的数据列;
having 在group by筛选出的数据列中过滤组条件的结果。

select * from runoob_tbl group by runoob_author;
select * from runoob_tbl group by runoob_author HAVING submission_date>'2018-04-11';

这里写图片描述

insert 增加语句:

insert into table_name (id, name, age) values(1, 'ling', 18), (), (), ();
insert into runoob_tbl(runoob_title,runoob_author,submission_date) values('python2','HC','2018-05-09');

这里写图片描述

update 修改语句:

update table_name set 列名=xxx, where 条件
update runoob_tbl set submission_date='2018-05-09' where runoob_id=2;

这里写图片描述

delete 删除语句:

delete from table where 条件判断
delete from runoob_tbl where runoob_id=2;

这里写图片描述

truncate 也可以用于清楚数据,但不删除表结构;
drop 可以用于将表所有数据包括表结构都删掉;

1.3 索引的基本使用
创建一个索引:

create index 库名_表名_列名1_列名2 on 表名 (列名1, 列名2
create index author on runoob_tbl (runoob_author);

查看创建后的索引:

show index from runoob_tbl;

这里写图片描述

使用explain在select语句前,可以查看查询语句中是否有使用索引:

explain select * from runoob_tbl where runoob_id = 1;

这里写图片描述

猜你喜欢

转载自blog.csdn.net/oqqalex123456789d/article/details/80246958