MySQL学习-常用命令整理
Eyes are more eloquent than lips.
“眉目传情胜于甜言蜜语”
整理了一下自己遇到并且经常用到的MySQL命令,虽然官方文档上有很详细的解释,不过自己还是在这里记录一下,方便查看,顺便加深一下印象,当前版本:MySQL 5.6
注:
- 未加 ‘shell > ’的均表示在mysql命令行下的命令;
- MySQL关键字全部使用小写,标识符全部使用中文(感觉相对直观);
一、系统服务
启动/停止/重启/查看状态:shell > service mysql start | stop | restart | status
二、连接服务器
连接: shell > mysql [-h主机名] -u用户名 -p[密码]
断开: mysql > quit | exit;
三、数据库操作
创建: create database 数据库名;
删除: drop database 数据库名;
查看定义: show create database 数据库名;
查看所有数据库: show databases;
查看当前使用的数据库: select database();
切换: use 数据库名;
四、数据库对象
1、表
创建:
create table 表名
(
列名1 数据类型 [列级约束条件] [默认值],
... ...
[表级约束条件]
);
修改: alter table 表名 [修改动作]
修改动作如下:
- 修改表名: rename 新表名;
- 修改字段名: change 旧字段名 新字段名 数据类型;
- 修改字段类型: modify 字段名 数据类型;
- 添加字段: add 字段名 数据类型 [列级约束条件] [first | after 列名];
- 删除字段: drop 字段名;
- 修改字段的排列位置: modify 字段名 数据类型 [first | after] 列名;
- 修改存储引擎: engine = 新引擎名;
- 删除外键约束: drop foreign key 外键名;
删除: drop table [if exists] 表名列表;
查看表结构:
- 基本结构: desc 表名;
- 详细结构: show create table 表名;
2、索引
创建: 索引定义块的格式为 [unique | fulltext | spatial] index 索引名(字段[列表]) [asc | desc]
- 创建表时将定义块附加在‘表级约束条件’位置;
- 在已经存在的表上创建: alter table 表名 add 定义块;
- 直接创建: create [unique | fulltext | spatial] index 索引名 on 表名(字段[列表]);
删除:
- 修改表: alter table 表名 drop index 索引名;
- 直接删除: drop index 索引名 on 表名;
查看: show index from 表名;
3、视图
创建:
create [or replace] [algorithm = {unidefined | merge | temptable}]
view 视图名
as select 字段[列表]
from 表或视图[列表]
[with [cascaded | local] check option];
修改:
- 使用创建的命令格式修改;
- 直接修改:将创建命令中的 create [or replace] 替换为 alter ;
通过视图修改表数据:把视图当做表操作,使用表的命令格式
删除: drop view [if exists] 视图列表 [restrict | cascade];
查看信息:
- desc 视图名;
- show table status [like '匹配模式'];
- show create view 视图名;
- select * from information_schema.views [where 子句];
4、存储过程和函数
创建存储过程:
create procedure 过程名([参数列表])
特性
begin
程序体
end;
创建函数:
create function 函数名([参数列表])
returns 返回类型
特性
程序体
return 返回值;
修改特性: alter {procedure | function} name 特性;
删除: drop {procedure | function} [if exists] 名称;
调用存储过程: call 过程名(参数列表);
调用函数: select 函数名(参数列表);
查看状态:
- show {procedure | function} status [like '匹配模式'];
- show create {procedure | function} 名称;
- select * from information_schema.routines [where 子句];
5、触发器
创建:
create trigger 名称
before | after
insert | update | delete
on 表名
for each row
begin
程序体
end;
删除: drop trigger 名称;
查看:
- show triggers;
- select * from information_schema.triggers [where 子句];
五、数据处理
1、插入: insert into 表名[(字段列表)] 数据部分;
数据部分如下:
- 直接插入数据: values(数据1)[, (数据2), ... ];
- 插入查询结果: select 子句;
2、更新: update 表名 set 字段1=值1[, 字段2=值2, ... ] [where 子句];
3、删除: delete from 表名 [where 子句];
六、查询(表)
基本格式:
select [distinct] {* | 字段列表}
from 表列表
[
[where 过滤条件]
[group by 字段列表]
[having 过滤条件]
[order by 字段1[asc | desc] [,字段2[asc | desc], ... ]]
[limit [行偏移量,] 显示行数]
];
过滤条件 格式: 条件1 [and | or 条件2 ... ]
条件 格式:
- 字段 比较运算符 [all | any | exists] 结果[集]
- 字段 in 结果集
- 字段 between 下限 and 上限
- 字段 like '匹配模式'
- 字段 regexp '正则表达式'
子查询:将select 子句的查询结果作为外层查询过滤条件中的结果[集];
连接查询:
- 内连接:
select 字段列表
from 表1 inner join 表2
on 过滤条件;
- 左 | 右 外连接:
select 字段列表
from 表1 left | right outer join 表2
on 过滤条件;
七、数据备份与还原
1、数据库级别的备份与还原
备份数据库对象
基本格式: shell > mysqldump [-h主机名] -u用户名 -p[密码] 备份对象 > 目标文件.sql
备份对象:
- 单个数据库的所有表: 数据库名
- 单个数据库中的部分表: 数据库名 表列表
- 多个数据库: --databases 数据库列表
- 所有数据库: --all-databases
还原数据库对象
- shell > mysql -u用户名 -p [数据库名] < 目标文件.sql
- source 目标文件.sql
2、直接复制数据库目录(仅用于MyISAM表)
- 备份:复制整个数据库目录
- 还原:将备份文件拷贝的数据库目录,并将其用户和组改为mysql
3、表数据的备份与还原
数据导出:
- select 语句: select 子句 into outfile '目标文件' [选项列表];
- mysqldump: shell > mysqldump -T 备份路径 -uroot -p 数据库名 [表列表] [选项列表]
- mysql: mysql -uroot -p [--virtical | --html | --xml] --execute="select 语句" 数据库名 > 目标文件
数据导入:
- load 语句:
load data infile '目标文件' into table 表名 [选项列表] [ignore 行数 lines];
- mysqlimport: shell > mysqlimport -uroot -p 数据库名 目标文件 [选项列表];
八、性能优化
1、查看性能参数: show status like '参数值';
2、分析查询: explain [extended] select语句;
3、优化数据库结构:
- 禁用 | 开启索引: alter table 表名 disable | enable keys;
- 禁用 | 开启唯一性检查: set unique_checks={0 | 1};
- 禁用 | 开启外键检查: set foreign_key_checks={0 | 1};
- 禁用 | 开启自动提交: set autocommit={0 | 1};
- 分析表: analyze [local | no_write_to_binlog] table 表列表;
- 检查表: check table 表列表,option;
- 优化表: optimize [local | no_write_to_binlog] table 表列表;
九、用户管理
1、账户管理
新建普通用户:
- create 语句:
create user '用户名'@'主机名'
[
identified by [password] '密码'
| identified with auth_plugin [as '插件名']
];
- grant 语句:
grant 权限列表 on 数据库.表
to '用户名'@'主机名'
[identified by '密码']
[with grant option];
- 向user表插入数据:
insert into mysql.user(host, user, password [, pri_list])
values('主机名', '用户名', '密码' [, 权限列表]);
删除普通用户:
- 直接删除: drop user 用户列表;
- 删除user表中的行:
delete from mysql.user where host='主机名' and user='用户名';
修改密码
root用户:
- 修改自己的密码:
①shell > mysqladmin [-h主机名] -uroot -p password "新密码"
②update mysql.user set password=password("新密码");
③set password=password("新密码");
- 修改普通用户的密码:
①set password for '用户名'@'主机名'=password("新密码");
②update mysql.user set password=password("新密码") where user='用户名' and host='主机名';
③grant usage on 数据库.表 to '用户名'@'主机名' identified by '新密码';
- 找回自己的密码:
①shell > mysqld_safe --skip-grant-tables user=mysql
②shell > net stop mysql
③shell > mysqld --skip-grant-tables
④打开新终端:shell > mysql -uroot
⑤使用上面的方法设置密码
普通用户:
- 修改自己的密码: set password=password("新密码");
2、权限管理
- 授权:
grant 权限类型1[(字段列表1)] [, 权限类型2[字段列表2]] ...
on 对象类型
to '用户名1'@'主机名1'
[identified by [password] '密码']
['用户2'@'主机名2' ...]
[with grant option];
对象类型:表,函数,存储过程
- 收回权限:
revoke [all | 权限类型1[(字段列表1)] ... ]
on 表列表
from '用户名'@'主机名'列表;
- 查看权限:
①show grants for '用户名'@'主机名';
②select 权限列表 from user where user='用户名' and host='主机名';
(全文完)
MySQL学习-常用命令整理的更多相关文章
- MySql语句常用命令整理---多表查询
首先第一张表还是我们单表查询之前用到t_employee,我们在另外新建一个表t_dept(部门表)建表命令如下: drop table if exists t_dept; CREATE TABLE ...
- MySql语句常用命令整理---单表查询
初始化t_employee表 创建t_employee表 -- DROP TABLE IF EXISTS test; CREATE TABLE t_employee ( _id INTEGER PRI ...
- MySQL 数据库常用命令小结
MySQL 数据库常用命令 1.MySQL常用命令 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删 ...
- 【linux】---常用命令整理
linux常用命令整理 一.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等 常用参数搭配: l ...
- CentOS系统操作mysql的常用命令
MySQL名字的来历MySQL是一个小型关系型数据库管理系统,MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了 ...
- Data Base MySQL的常用命令
MySQL的常用命令 一.下载地址: http://www.mysql.com 二.安装注意: root默认密码:123456 三.常用命令: 1.创建用户并授权: 创建用户,只能本地访问:cr ...
- MySQL备份常用命令总结
MySQL备份常用命令总结 1.数据库和数据全部备份 mysqldump -uroot -pPassword -hlocalhost databasename > test.sqlmysqldu ...
- Tomcat性能优化及常用命令整理
1汤姆猫性能优化 1.1连接参数 1.1.1默认连接配置 默认连接器采用阻塞式 IO,默认最大线程数为200,配置如下: <Connector port="8080" pro ...
- linux 服务器常用命令整理
linux 服务器常用命令整理 目录 网络分析 - tcpdump \ telnet \ (netstat \ ss \ lsof) \ nload 网络传输 - scp \ rsync \ (rz ...
随机推荐
- ♫【JS模式】偏函数
<深入浅出Node.js> var toString = Object.prototype.toString var isType = function(type) { return fu ...
- 【转】Java运算符优先级
原文网址:http://www.cnblogs.com/gw811/archive/2012/10/13/2722752.html Java运算符优先级 序列号 符号 名称 结合性(与操作数) 目数 ...
- 数据结构(线段树):NOI 2016 区间
[问题描述] [输入格式] [输出格式] [样例输入] 6 3 3 5 1 2 3 4 2 2 1 5 1 4 [样例输出] 2 [样例说明] [更多样例] 下载 [样例 2 输入输出] 见目录下的 ...
- 字符串(多串后缀自动机):HDU 4436 str2int
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- 数据结构(KD树):HDU 4347 The Closest M Points
The Closest M Points Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Ot ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- Delphi NativeXml用法攻略
NativeXml用法攻略 NativeXml可以在官网上下载,下载后将文件夹放在指定地方,打开DELPHI在其环境变量中引用NativeXml路径,然后在程序中引用NativeXml单元,我们就可以 ...
- Missing Number ——LeetCode
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- WEB性能测试:你应该带上VisualStudio2010
原文地址:http://www.16aspx.com/Article/62 在Web性能测试方面,增加了循环(Loops)和条件(Conditions),让开发人员可以为他们的应用程序写出更复杂,更智 ...
- hdu 4322 最大费用流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 #include <cstdio> #include <cstring> ...