最新版mysql基本命令操作
命令
数据库密码
| 创建密码 | mysqladmin -u root password '123456' |
|---|---|
| 修改密码 | 命令行 1、mysqladmin -u root -p'123456' password 'oldboy' |
| sql语句 | 2、set password=password('123123'); flush privileges; |
| sql语句 | 3、update mysql.user set authentication_string=PASSWORD('123456') where user ="root" and host ="localhost"; |
ml
| 基本命令 | 命令格式 |
|---|---|
| 切换进入库 | use oldboy; |
| 刷新 | flush privileges; |
| 授权新建用户 | show grants for 'wordpress'@'172.16.1.%'; |
| 授权远程连接 | create user jyt@'172.16.1.%' identified by '123456'; |
| 修改用户密码 | alter user jjj@'172.16.1.%' identified by '123123'; |
| 查看当前数据库的字符集 | show charset; |
| 收回oldboy用户的drop权限 | revoke drop on oldboy.* from oldboy@'lolcation'; |
| 向表中插入数据 | INSERT INTO stu(name,age) VALUE('oldguo','18'); |
删除
| 删除 | 命令格式 |
|---|---|
| 删除无用的库 | drop database oldboy; |
| 删除用户 | drop user root@'oldboy'; |
| 删除一张表 | drop table jyt; |
| 删除表中的列 | alter table oldguo drop state; |
| \(删除所有用户\) | delete from mysql.user; |
添加
| 添加 | 命令格式 |
|---|---|
| 创建库 | create detabase jyt; |
| 建库 | create database oldguo charset utf8mb4; |
| 增加用户并将用户设为超级管理员 | grant all privileges on . to jyt@'localhost' identified by '123456' with grant option; |
| 创建表 | create table oldguo ()charset=utf8mb4 engine=innodb; |
| 表 | - |
| 添加一列到最后 | alter table jyt add num char(11) not null unique comment '手机号'; |
| 添加一列到指定列后 | alter table jyt add qq varchar(255) not null unique comment 'qq' after name; |
| 添加一列到第一列 | alter table oldguo add sid varchar(255) not null unique comment '学生号' first; |
修改
| 修改 | 命令格式 |
|---|---|
| 修改库的格式 | alter database oldguo charset utf8mb4; |
| 修改列的属性 | alter table oldguo modify name varchar(128) not null ; |
show语句
| show语句 | 命令格式 |
|---|---|
| show databases; | 查看所有库 |
| show tables; | 查看当前库下的表名 |
| show tables from world; | 查看world库下的所有表 |
| show create table; | 查看建表语句 |
| show grants for root@% | 查看用户权限 |
| show charset | 查看所有字符集 |
| show collation | 查看校对规则 |
| show full processlist | 查看数据库的连接情况 |
| show status | 查看数据库整体状态 |
| show variables | 查看数据库所有变化情况 |
| show variables | 查看数据库所有变化情况 |
| show engines | 查看所有存储引擎 |
| show engine innodb status | 查看存储引擎状态情况 |
| show binary logs | 查看二进制日志情况 |
| show binlog events in | 查看二进制日志事件 |
| show relalog events in | 查看relay日志事件 |
| show slave status | 查看从库状态 |
| show master status | 查看数据库binlog位置信息 |
| show index from | 查看表的所有情况 |
| 查看所有库 | show databases; |
| 查看当前库中的表 | show tables; |
| 查看特定库中的表 | show tables from jyt; |
| 查看stu表中数据 | show create table stu; |
| 查看用户权限 | show grants for 'wordpress'@'172.16.1.%'; |
| 查看链接线程 | show processlist; |
| 匹配查询库 | show databases like 'oldboy'; |
| 匹配查询库以xx开头的所有 | show databases like 'oldboy'; |
| 查看创建的用户oldboy拥有哪些权限 | show grants for oldboy@'localhost'; |
查询select语句
| 基本命令(select) | 命令格式 |
|---|---|
| 查看当前所在库 | select database(); |
| 查看当前登录用户 | select user(); |
| 查看表名对应主机 | select user,host from mysql.user; |
| 查看表名对应主机和密码 | select user,host ,authentication_string from mysql.user; |
| 查看表 | select user,host from mysql.user where user="jyt"; |
| 在db表里查看权限 | select * from mysql.db where user='wordpress' and host='172.16.1.%'\G |
| 表相关 | - |
| order by | 排序 |
| 查询统计总数 | select district,sum(population) from city where countrycode='chn' group by district; |
| 查询统计总数并排序降序 | SELECT district,sum(population) FROM city WHERE countrycode='chn' GROUP BY district ORDER BY SUM(Population) DESC; |
| 查询中国所有的城市,并以人口数降序输出 | select*from city where countrycode='chn' order by population desc; |
| limit m,n 跳过m行显示n行 | limit x offset y 跳过y行显示x行 |
| 前5行 | SELECT*FROM city WHERE countrycode='chn' ORDER BY population DESC LIMIT 5; |
| 显示6-10行 | SELECT*FROM city WHERE countrycode='chn' ORDER BY population DESC LIMIT 5,5; |
| 显示6-10行 | select*from city where countrycode='chn' order by population desc limit 5 offset 5; |
| 函数 | 例 |
| avg()平均数 | select district,avg(population) from city where countrycode='chn' group by district; |
| count()计数 | select countrycode,count(name) from city group by countrycode; |
| sum()求和 | select countrycode,sum(population) from city group by countrycode ; |
| max()最大值 | - |
| min()最小值 | - |
| group_concat()聚合 | select countrycode,group_concat(district) from city group by countrycode; |
| where | 相当于grep | 说明 |
|---|---|---|
| where配合等值查询 | select * from world.city where countrycode='chn'; | 查询表中的中国城市信息 |
| where配合不等值查询 | select * from world.city where Population<100; | 人口小于100人的城市 (>,<,<=,>=,<>) |
| where配合模糊查询 | select * from world.city where CountryCode like 'c%'; | 国家以c开头 禁止%开头 |
| where配合逻辑连接符(AND or) | select * from world.city where Population > 10000 AND Population < 20000; | select * from world.city where population between 10000 and 20000; |
| select * from world.city where CountryCode='chn' OR CountryCode='usa'; | select * from world.city where countrycode in ('chn','usa'); | |
| SELECT * FROM world.city WHERE CountryCode='chn' UNION ALL SELECT*FROM world.city WHERE CountryCode='usa'; | 推荐 union 去重 加all不去重 默认去重 |
菜鸟9528号,请求开炮。

浙公网安备 33010602011771号