1、查看類
- 查看mysql當前的各種指標
status;
show status;
- 查看字符集
show variables like "%character%";
show character set;
select user();
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; #開啓
show full processlist;
show create view viewname; #查看
drop view viewname; #刪除
2、操做類
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'@'%'; #刪除用戶