天天看点

Python mysql的常用操作

1.创建库,表

建库:create database 库名;

建表:create table 表名( 列名 数据类型(位数) not null );

如:create table student(id int(10) not null,name varchar(10));

此处为创建一个名字为student的表,其中有id,name,id的数据类型为整形并且位数最大为10,not null 表示不能为空,

name字段数据类型为字符类型,位数最大为10;注意char与varchar的区别,前者在写入字段后,如果没有等于位数的最大值,

那么会在后面补上空格,直至最后。

2.查询的相关操作

查询有多少个库 show databases;

查询某库有多少个表 show tables; (需要先进入库中)

查看表结构  desc 表名;

查询表的内容 select * from 表名;(*表示查询所有字段内容,也可以将*替换为你需要的字段,如下面的例子。)

mysql> select * from sixgod;

+------+--------+

| id   | name   |

+------+--------+

|    1 | sixgod |

|    2 | yjj    |

|    3 | a      |

|    3 | b      |

|    3 | y      |

|   12 | a      |

|   12 | a      |

|  122 | b      |

|  122 | y      |

|  122 | yy     |

|    1 | yyy    |

+------+--------+

11 rows in set (0.00 sec)

mysql> select name from sixgod;

+--------+

| name   |

+--------+

| sixgod |

| yjj    |

| a      |

| b      |

| y      |

| a      |

| a      |

| b      |

| y      |

| yy     |

| yyy    |

+--------+

11 rows in set (0.00 sec)

将查询到的结果按照某字段进行升降序排列  select * from 表名 order by 字段名 asc/desc;

mysql> select * from sixgod order by id asc;  #(以id进行升序排列,默认为升序,asc也可以不写。)

+------+--------+

| id   | name   |

+------+--------+

|    1 | sixgod |

|    1 | yyy    |

|    2 | yjj    |

|    3 | a      |

|    3 | b      |

|    3 | y      |

|   12 | a      |

|   12 | a      |

|  122 | b      |

|  122 | y      |

|  122 | yy     |

+------+--------+

11 rows in set (0.00 sec)

mysql> select * from sixgod order by id desc; #(以id进行降序排列)

+------+--------+

| id   | name   |

+------+--------+

|  122 | yy     |

|  122 | b      |

|  122 | y      |

|   12 | a      |

|   12 | a      |

|    3 | a      |

|    3 | b      |

|    3 | y      |

|    2 | yjj    |

|    1 | sixgod |

|    1 | yyy    |

+------+--------+

11 rows in set (0.00 sec)

where条件的查询 select 列名  from 表名 where 条件判断;

如:查询name字段等于a的。

mysql> select * from sixgod where name='a';

+------+------+

| id   | name |

+------+------+

|    3 | a    |

|   12 | a    |

|   12 | a    |

+------+------+

3 rows in set (0.00 sec)

group by 分组:将所有相同的名字分组

mysql> select name,count(name) from sixgod  group by name;(count表示重复的有多少个)

+--------+-----------+

| name   | count(name) |

+--------+-----------+

| a      |         3 |

| b      |         2 |

| sixgod |         1 |

| y      |         2 |

| yjj    |         1 |

| yy     |         1 |

| yyy    |         1 |

+--------+-----------+

limit 分页显示 

mysql> select * from sixgod  limit 5;(只显示5条记录)

+------+--------+

| id   | name   |

+------+--------+

|    1 | sixgod |

|    2 | yjj    |

|    3 | a      |

|    3 | b      |

|    3 | y      |

+------+--------+

5 rows in set (0.00 sec)

3.增加的相关操作

insert into 表名(字段名) values(字段对应的值1),(2),(3);如果是一次添加多条记录则在后面继续写

mysql> insert into sixgod(id,name) values(100,'a'); (添加一条id为100,name为a的记录)

Query OK, 1 row affected (0.00 sec)

4.修改的相关操作

update 表名 set 列名='修改后的值' where 列名='值';

mysql> update sixgod set name='aa' where name='a'; 

将name字段等于a的修改为aa。

5.删除的相关操作

truncate  表名;只清除数据,不删除表结构 

drop   表名; 删除表结构

6.给用户赋权

grant all privileges on *.* to 'user1'@'%' identified by '123456' with grant option;

用户user1可以对所有的库,所有的表进行增删改查等,并且可以对其他用户进行授权。

grant all privileges on *.* to 'user1'@'%' identified by '123456' ;

用户user2只可以对所有的库,所有的表进行增删改查等。

7.索引

MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。

为表添加索引:create index 库名_表名_列名1_列名2  on表名(列名1, 列名2);

查看某表的索引:show index from 表名;

查看索引是否被使用到:mysql> explain select * from xuesheng where name='a';(在语句前加上explain)

以上文章转自链接:https://blog.csdn.net/weixin_39318540/article/details/80255470

如有侵权,敬请告知!!!!!

Python mysql的常用操作