show variables like "%character%";
show character set;
查看当前用户
select user();
查看排序规则
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
show variables like "%collation%";
show collation;
查看sql模式
show global variables like "sql_mode";
查看存储引擎
show engines;
show global variables like '%innodb%'; #查看innodb引擎
开启独立表空间(重启MySQL的失效,需修改配置文件)
show golbal variables like "innodb_file_per_table"; #查看
set global innodb_file_per_table=ON; #开启
查看MySQL的进程数
show full processlist;
查看视图
show create view viewname; #查看
drop view viewname; #删除
二,操作类
库操作
create database dbname; #创建数据库
show databases; #查看数据库
show create database mydb; #查看数据库创建语句
drop database dbname; #删除数据库
表操作
create table mytab (name varchar(20)); #创建表
create table tab1 select user,host,... from mysql.user; #根据查询出来的数据创建表
create table tab1 like oldtab; #基于oldtab 创建空表tab1;
show create table tab1; #查看表结构
show db.tables; # 查看库db1下的所有表
show table status where name="tab1"\G; #查看表tab1的状态
select count(1) from tab1; #查看表有多少行
insert into tab1 values("xuel","kaliarch"); 为表tab1插入数据
drop table mytable; #删除表
alter table mytable rename renametable; #修改mytable的表名
alter table renametable add age int(20); #更新表添加一列
alter table renametable modify age char(1); #修改列的数据类型
alter table renametable change column age age2 char(1); #重命名列
alter table s1 engine=myisam; #修改表的存储引擎
select 列名,列名2 from 表名 where 列名=‘gnome’ ; #按列查询
delete from course where id=2; #删除表格中查询记录一条记录
update 表名称 set 列名称=新值 where 列=值; #更新数据
select * from 表名 where 条件1 and 条件2; #where逻辑组合
select * from 表名 where 条件1 or 条件2;
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; #内连接
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name; #左连接
(LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL)
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name; #右连接
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name; #全连接(结合了 LEFT JOIN 和 RIGHT JOIN 的结果。)
select * from mysql.user limit 2; #取两行数据
SELECT DISTINCT column_name,column_name FROM table_name; #DISTINCT 关键词用于返回唯一不同的值。
SELECT column_name,column_name FROM table_name
ORDER BY column_name,column_name ASC|DESC; #ORDER BY 关键字默认按照升序对记录进行排序,降序为desc
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern; #WHERE column_name LIKE pattern; 类似where字句终端模式匹配
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2; #BETWEEN 操作符选取介于两个值之间的数据范围内的值。
SELECT column_name(s) FROM table_name AS alias_name; #表的别名
SELECT column_name AS alias_name FROM table_name; #列的别名
show indexes from tab1; #查看tab1的索引
alter table tab1 add index (user); #添加索引
alter table tab1 drop index user; #删除索引
用户权限
create user xuel identified by "xuel@anchnet.com"; #创建用户
rename user xuel to xuel2; #重命名用户
select user,password,host from mysql.user; #查询用户
grant all privileges on mydb.* to xuel2@'%'; #授权(注意:如果用户未存在授权时候identified by 后新增用户)
revoke all privileges xuel2; #撤回授权
select * from mysql.user where user='xuel1'\G; #查看用户详细权限
show grants for xuel1; #查看xuel1的授权
set password xuel=password("xxzx567@@") #修改用户密码
set password for username@host = password( 'your_password')
update mysql.user set password=password("xxzx@789") where user="xuel" and host="%"; #5.6之前更新用户密码
update mysql.user set authentication_string=password('!8gecco4') where user='root' and Host = 'localhost';` #5.7更新用户密码
drop user 'xuel1'@'%'; #删除用户