標籤:lis show 右串連 add 終端 密碼 revoke 字元 結合
一、查看類
- 查看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; #刪除
二、操作類
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_nameORDER BY column_name,column_name ASC|DESC; #ORDER BY 關鍵字預設按照升序對記錄進行排序,降序為descSELECT 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 "[email protected]"; #建立使用者rename user xuel to xuel2; #重新命名使用者select user,password,host from mysql.user; #查詢使用者grant all privileges on mydb.* to [email protected]‘%‘; #授權(注意:如果使用者未存在授權時候identified by 後新增使用者)revoke all privileges xuel2; #撤回授權select * from mysql.user where user=‘xuel1‘\G; #查看使用者詳細許可權show grants for xuel1; #查看xuel1的授權set password xuel=password("[email protected]@") #修改使用者密碼set password for [email protected] = password( ‘your_password‘) update mysql.user set password=password("[email protected]") 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‘@‘%‘; #刪除使用者
MySQL常用操作