MySQL命令汇总

用户相关命令

进入数据库:mysql -u 账号 -p'密码';
创建新账户:create user '账号'@'%' identified by '密码';
给予当前用户操作权力:grant all on *.* to '账号'@'%';
刷新:flush privileges;
查看当前帐号:select user();
删除用户:drop user'用户名'@'%';

库相关命令

创建数据库:create database 库名;
查看当前所在库:select database 库名;
查看所有数据库:show databases;
进入数据库:use 库名;
删除库:drop database 库名;

表相关命令

创建表:create table 表名(字段 字段类型, 字段 字段类型);
查看表结构:desc 表名;
查看当前库的所有表:show tables;
修改表字段:alter table 表名 change 修改字段 修改后的字段;
删除表:drop table 表名;

表数据相关命令

插入表数据:insert into 表名 value(插入值1,插入值2);
查询表数据:select 字段 from 表名;
修改表数据:update 表名 set 修改字段和值 where 条件;
删除表数据:delete from 表名 where 条件;

查询命令

查找为空的数据:select * from 表名 where 字段 is null;
查找不为空的数据:select * from 表名 where 字段 is not null;
查找两个条件同时满足的数据:select * from 表名 where 条件 and 条件;
查找两个条件只要有一个条件满足的数据:select * from 表名 where 条件 or 条件;
查找条件相反的数据:select * from 表名 where not 条件;
通过升序来查找对应字段数据:select * from 表名 order by 字段;
通过降序来查找对应字段数据:select * from 表名 order by 字段 desc;
查找对应的数据你要查几条那就写几从1开始:select * from 表名 limit 条数;
将对应的字段去重:select distinct 字段 from 表名;
模糊查询:select * from 表名 where 字段 like '%数据%';
区间查找:select * from 表名 where 字段 between 什么数据 and 到什么数据;
查询表中数量:select count (*) from 表名;
查到对应字段的数量:select count(字段) from 表名;
字段的内容求和:select sum(字段) from 表名;
求字段平均值:select avg(字段) from 表名;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容