查看所以库大小
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);