查看所以庫大小
select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
group by TABLE_SCHEMA
order by data_length desc;
查詢 表結構
show create tables tablename
查詢視圖結構
show create view viem.name
查詢數據庫中的存儲過程和函數
select `name` from mysql.proc where db = 'xx' and `type` = 'PROCEDURE' //存儲過程
select `name` from mysql.proc where db = 'xx' and `type` = 'FUNCTION' //函數
show procedure status; //存儲過程
show function status; //函數
查看存儲過程或函數的創建代碼
show create procedure proc_name;
show create function func_name;
查看視圖
SELECT from information_schema.VIEWS //視圖
SELECT from information_schema.TABLES //表
查看觸發器
SHOW TRIGGERS [FROM db_name] [LIKE expr]
SELECT * FROM triggers T WHERE trigger_name=”mytrigger” \G
查看錶主鍵
ELECT column_name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE table_name='表名' AND constraint_name='PRIMARY'
mysql查看鎖表情況
mysql> show status like 'Table%';
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| Table_locks_immediate | 105 |
| Table_locks_waited | 3 |
+----------------------------+----------+
Table_locks_immediate 指的是能夠立即獲得表級鎖的次數
Table_locks_waited 指的是不能立即獲取表級鎖而需要等待的次數
查看正在被鎖定的的表
show OPEN TABLES where In_use > 0;
字符匹配查詢
select * from test2 where name regexp 'g$';
查詢空值
在mysql中,查詢某字段爲空時,切記不可用 = null,
而是 is null,不爲空則是 is not null
www.2cto.com
select * from table where column is null;
select * from table where column is not null;
修改表名
alter table h rename hen;把h表名改成hen
使用like查詢狀態state爲Waiting的所有線程
select * from information_schema.processlist where state like '%Waiting%';
建表後添加主鍵
alter table zhang add primary key(id);