码迷,mamicode.com
首页 > 数据库 > 详细

MySQL常用命令

时间:2019-01-31 14:47:48      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:not   lob   tin   te pro   plain   jinjia   当前系统时间   字段   print   

mysql> SHOW ENGINES; #查看存储引擎命令 mysql> SHOW GLOBAL VARIABLES LIKE ‘%default%engine%‘; #查看表引擎 (1) 为所有root用户设定密码; mysql> SET PASSWORD mysql> update mysql.user SET password=PASSWORD(‘your_pass‘) WHERE cluase; FLUSH # mysqlamin mysqladmin -u root -p123456 password abcdef

(2) 删除所有匿名用户
mysql> DROP USER ‘‘@‘localhost‘;
上述两步骤可运行命令:[shell]# mysql_secure_installation

                [shell]# mysqld --print-defaults    #如何获取程序默认使用的配置
                ]# mysql -e "SHOW DATABASES;"   #命令行界面输出命令结果;

SQL脚本运行:# mysql -uUSERNAME -hHOST -pPASSWORD < /path/from/somefile.sql
                    mysql> source /path/from/somefile.sql

获取运行中的mysql进程使用各服务器参数及其值:
mysql> SHOW GLOBAL VARIABLES;
mysql> SHOW [SESSION] VARIABLES;
mysql> SHOW CHARACTER SET; #查看字符集
mysql> SHOW COLLATION; #查看排查规则

CREATE相关的常用命令:
                 CREATE DATABASE
                 CREATE EVENT
                 CREATE FUNCTION
                 CREATE FUNCTION UDF
                 CREATE INDEX
                 CREATE PROCEDURE
                 CREATE SERVER
                 CREATE TABLE
                 CREATE TABLESPACE
                 CREATE TRIGGER
                 CREATE USER
                 CREATE VIEW

相关查询:

mysql> SHOW TABLE STATUS LIKE ‘t1‘\G            #查看表状态信息:    
增加字段    mysql> ALTER TABLE student  ADD  age varchar(20) NOT NULL; 
mysql> CREATE INDEX name ON student (name); #索引的创建可以使用ALTER、CREATE命令创建
mysql> EXPLAIN SELECT * FROM students WHERE id=3; #确认是否有使用到索引进行查询。
mysql> EXPLAIN SELECT * FROM student WHERE name LIKE ‘%X%‘;
mysql> EXPLAIN SELECT * FROM student  WHERE id=4\G

数据插入:

mysql> INSERT INTO student (name,sex,register_date) VALUES (‘Jinjiao‘,‘M‘,‘2019-01-29‘);
mysql> INSERT INTO student SET name=‘Yinjiao‘,sex=‘F‘,register_date=‘2019-01-28‘;

更新数据:
` mysql> UPDATE student SET age=100 WHERE id=19;``

查询:

mysql> SELECT NOW();    #显示当前系统时间。
mysql> SELECT  DISTINCT sex FROM  student;   #去重查询
mysql> SHOW GLOBAL VARIABLES LIKE ‘query%‘;   #命中个数
mysql> SHOW GLOBAL STATUS LIKE ‘Qcache%‘;     #缓存命中率查询
mysql> SHOW GLOBAL STATUS LIKE ‘Com_se%‘;

WHERE 子句:

mysql> SELECT name,age,id FROM student WHERE age >10;
mysql> SELECT name,age,id FROM student WHERE age+10 >10;

分组聚合:

mysql> SELECT * FROM student GROUP BY sex;
mysql> SELECT avg(age) FROM student GROUP BY sex;
HAVING: 对分组聚合运算后的结果指定过滤条件;
mysql> SELECT avg(age) as aage,sex FROM student GROUP BY sex HAVING aage>20;
                    #aage 定义的字段别名
mysql> SELECT count(id) AS NOS FROM student GROUP BY id;
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id;
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id HAVING NOS>2;

升序与降序语句:
升序:ASC
降序:DESC
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id HAVING NOS>2 ORDER BY NOS;
mysql> SELECT count(id) AS NOS,id FROM student GROUP BY id HAVING NOS>2 ORDER BY age DESC;

查看表结构:
DESCRIBE tbl_name;
DESC命令用于获取数据表结构

MySQL常用命令

标签:not   lob   tin   te pro   plain   jinjia   当前系统时间   字段   print   

原文地址:http://blog.51cto.com/zhanx/2348153

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!