MySQL 实用语句集合
参考链接[用户]:http://blog.csdn.net/dmtnewtons_blog/article/details/9136339
参考链接[属性]:http://stackoverflow.com/questions/15821532/get-current-auto-increment-value-for-any-table
参考链接[索引]:http://blog.csdn.net/dmtnewtons_blog/article/details/18605947
1、查询数据库中表的属性:
SHOW TABLE STATUS FROM `DatabaseName` LIKE 'TableName' ;
结果举例:
+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
| TableName | InnoDB | 10 | Compact | 2 | 8192 | 16384 | 0 | 0 | 11534336 | 8 | 2014-03-06 02:13:49 | NULL | NULL | utf8_general_ci | NULL | checksum=1 delay_key_write=1 row_format=DYNAMIC | |
+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
2、获取数据库表中自增字段的值:
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
结果举例:
+----------------+
| AUTO_INCREMENT |
+----------------+
| 8 |
+----------------+
3、创建MySQL登录用户,可以任意地址远端访问,同root权限:
grant all on *.* to 'remote'@'%' identified by 'password';
结果举例:
#/usr/local/mysql/bin/mysql -h 1.1.1.1 -u remote -p
4、清空表:
TRUNCATE `TableName`;
5、查看表中的索引:
EXPLAIN SELECT `id`,`username` FROM `TableName`;
结果举例:
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------------+
| 1 | SIMPLE | TableName | index | NULL | idx_username | 98 | NULL | 1 | Using index |
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------------+
6、字符串截取:
[1] 在路径中截取文件名:
SELECT SUBSTRING_INDEX('C:\\dir\\file.txt', '\\', -1); //windows
SELECT SUBSTRING_INDEX('/usr/local/ade.c', '/', -1); //Linux
[2] 返回当前 MySQL用户名和机主名
mysql> SELECT USER();
-> 'davida@localhost'
这个值指示了你指定的连接服务器时的用户名,及你所连接的客户主机。这个值可以和CURRENT_USER()的值不同。
你可以这样提取用户名部分:
mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
-> 'davida'
由于 USER() 返回一个utf8 字符集中的值,你也应确保'@'字符串文字在该字符集中得到解释:
mysql> SELECT SUBSTRING_INDEX(USER(),_utf8'@',1);
-> 'davida'