20.MySQL 常用命令

1.show 查询信息,状态
use 数据库名字  //选择数据库
show databases // 显示数据库
show tables // 显示表
show columns from 表名  // 显示列信息 ,  快捷方式 desc 表名  , show full columns from 表名
show status // 显示服务器信息
show create database 数据库名 //显示创建数据库的sql
show create table 表名  // 显示创建表的sql
show grants // 显示授予用户的权限
show errors  // 显示服务器错误
show warnings  //显示服务器警告
终端下 
help show //查看帮助信息
select * from 表名 limit n,m   // 下标从0开始
等价于 
select * from 表名 limit m offset n
show create table 表名 //展示建表语句
show table status like 表名 // 表状态
show index from 表名 //显示索引信息
show variables 
 like'character_set_server';  // 查看服务器当前的字符集和校对规则

show variables like 'collation_server';


字符集,校对规则:
show character set //显示所有可用的字符集,即校对
show collation // 更详细的
create table
(
  name varchar(32) character set utf8 collate utf8_latvian_ci // 对列进行设置
) ENGINE=InnoDB DEFAULT CHARSET=utf8 collate utf8_latvian_ci  COMMENT='推送消息'; // 建表时,指定字符集和校对规则
select * from 表名 order by 列名 collate dec8_swedish_ci  // 查询时,指定与建表不一样的校对规则

2.权限管理:
create user 用户名 identified by '密码' ; // 创建用户, 需要刷新授权 ,flush privileges;
rename user 老用户名 to 新有用户名  // 重命名用户
drop user 用户名 // 删除用户
show grants for 用户名 // 查看某个用户的权限
grant 权限 on 数据库.表 to 用户 ;
grant select on test.* to 'aaa'@'%' ; // 授予aaa用户对数据库 test 的所有表都有 select 权限
revoke select on test.* from aaa ; // 把aaa的权限撤回


更改密码:

set password for 用户名=password('123123');
set password=password('123123');// 设置自己的密码

3.表状态检查

analyze table 表名 //分析表键状态
check table 表名 
repair table 表名 //修复表名
optimize table 表名//回收空间

4.mysql 启动关闭

service mysqld start // CentOS6, 通过 rpm 包安装的
systemctl start mariadb  #启动MariaDB   // CentOS7
 ./mysql_safe  & //启动
mysqlamdin -u root -p shutdown //关闭mysql
service mysqld stop // 通过 rpm 包安装的

5.DDL 语言
create database 数据库名
drop database 数据库名
create table 表名
(
  id int(11),
  primary key (`int`)
)engine=InnoDB default charset=utf8 

drop table 表名
alter table 表名 modify 列名 属性 //修改属性
alter table 表名 add 列名 属性 //增加属性
alter table 表名 drop 列名 属性 //删除
alter table 表名 change 列名老 列名新 //字段改名
alter table 表名 add 列名1 属性1 after 列名2 //新增字段在 列名2后面 ,first
alter table 表名老 rename to 表名新 //修改表名
alter table 表名 auto_increment = n // 修改自动增长为 n 开始


6.帮助的使用



8.SQL 技巧

select * from 表名 order by rand() limit 2 //随机取行
select * from 表 group by 列1 with rollup // 显示更多信息
bit_or()
bit_and()

______________________________________________________________________


9. 性能分析

set profiling = 1 // 开启
show profiles;
show profile for query ID
先 flush status  ,  show status where variable_name like "Handler%" or variable_name like 'create%';


10.mysql

mysql -u root -p  -e "sql语句" 






















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值