开启 - MySQL服务
sudo mysql.server start
停止 - MySQL服务
sudo mysql.server stop
重启 - MySQL服务
sudo mysql.server restart
查看 - MySQL服务状态
sudo mysql.server status
登录 - MySQL内部
mysql -u root -p
MySQL内部 - 退出MySQL
exit; 或 quit;
MySQL内部 - cmd(命令行)登录连接,设置字符格式,非命令行登录与配置了 utf8 或 gbk 格式不需要设置,有乱码才设置。
set names gbk;
MySQL内部 - MySQL状态信息,例如 UNIX socket ...
status;
MySQL内部 - 重置密码
set password = password('123456');
MySQL内部 - 重置指定账户密码
set password for '用户名'[@'允许登录的地址'] = password('密码');
MySQL内部 - 查看数据库编码格式 (推荐用这个,我用的这个!)
show variables like 'character%';
MySQL内部 - 查看数据库编码格式 (这两个都可以查看数据库编码格式)
show variables like ‘collation%’;
MySQL内部 - 显示查看所有数据库
show databases;
数据库内部 - 指定数据库是否存在
show databases like '数据库名';
MySQL内部 - 显示查看可用字符集,也就是可以支持的编码格式
show charset;
MySQL内部 - 显示查看校对规则,通常使用默认的不需要修改,其实就是字符集中每个字符的“排序规则”
show collation;
MySQL内部 - 创建数据库
create database 数据库名 charset 编码名(推荐utf8);
create database 数据库名 charset 编码名(推荐utf8) collate 校对规则;
MySQL内部 - 创建数据库,如果配置好了,字符集默认就是 utf8
create database 数据库名;
MySQL内部 - 查看数据库创建信息,可以查看指定数据库的完整创建语句
show create database 数据库名;
MySQL内部 - 删除现有数据库
drop database 数据库名;
MySQL内部 - 修改数据库字符集(编码格式)
alter database 数据库名 charset 新的字符集名称;
MySQL内部 - 修改数据库校对规则
alter database 数据库名 collate 新的校对规则名;
MySQL内部 - 使用(进入/选择)数据库
use 数据库名;
MySQL内部 - 外部导入.sql文件
source /Users/xxx/Desktop/test.sql;
数据库内部 - 显示查看所有表
show tables;
数据库内部 - 指定表是否存在
show tables like '表名';
数据库内部 - 创建表,编码格式:utf8、 gbk ...... 表类型:InnoDB、MyIsam、BDB、Memory ......
create table 表名 (字段名 字段类型, 字段名 字段类型 default 默认值);
create table 表名 (字段名 字段类型 comment '描述文字', 字段名 字段类型 comment '该字段的描述文字,类似注释');
create table 表名 (id int auto_increment primary key, 字段名 字段类型);
create table 表名 (字段名 字段类型, 字段名 字段类型) charset = 编码格式 engine = 表类型名称;
数据库内部 - 创建表 - 如果不存在就创建表,如果存在则不需要走创建
create table if not exists 表名 (字段名 字段类型, 字段名 字段类型);
数据库内部 - 创建表,关键字做字段使用,在关键字左右加上 `desc` 两点即可,其他字段也可以加
create table `表名` (`id` int, `desc` text);
数据库内部 - 创建表,尾部设置主键
create table 表名 (id int auto_increment, 字段名 字段类型,primary key(id));
数据库内部 - 创建表,设置多字段主键
create table 表名 (id int, name varchar(10), 字段名 字段类型,primary key(id, name));
MySQL内部 - 修改表字符集(编码格式),配置好了默认就是 utf8
alter table 表名 charset=新的字符集名称;
数据库内部 - 查看表创建信息,可以查看指定表的完整创建语句
show create table 表名;
数据库内部 - 查看表结构
desc 表名;
数据库内部 - 删除表
drop table 表名;
数据库内部 - 删除表 - 如果存在这个表就删除
drop table is exists 表名;
数据库内部 - 修改表名
alter table 旧表名 rename 新表名;
数据库内部 - 添加字段,默认添加在最后
alter table 表名 add 字段名 字段类型, add 字段名 字段类型;
数据库内部 - 添加字段,加上 ` ` 是以防关键字冲突,可加可不加,视情况而定
alter `table` 表名 add `字段名` 字段类型;
数据库内部 - 添加字段,添加到指定字段名后面
alter table 表名 add 字段名 字段类型 after 指定字段名;
数据库内部 - 添加字段,添加到表的最前面,无法指定字段
alter table 表名 add 字段名 字段类型 first;
数据库内部 - 修改字段,完全修改为新的字段
alter table 表名 change 旧字段名 新字段名 新字段类型;
alter table 表名 change 旧字段名 新字段名 新字段类型 default 默认数据;
数据库内部 - 修改字段,不修改字段名,只修改字段其他信息
alter table 表名 modify 字段名 新字段类型;
alter table 表名 modify 字段名 新字段类型 after 指定字段名;
数据库内部 - 删除字段
alter table 表名 drop 字段名;
表数据操作 - 插入数据
insert into 表名 (字段列表) values (值列表);
表数据操作 - 插入数据,值列表与字段列表位置一一对应,不能漏下任何一个字段,可省略写字段列表。
insert into 表名 values (值列表);
表数据操作 - 高级插入数据 set 缺点:每次只能写入一行
insert into 表名 set 字段名 = 字段值, 字段名 = 字段值, ...;
表数据操作 - 高级插入数据 同事插入多行数据
insert into 表名 (字段列表) values (值列表), (值列表), (值列表), ...;
表数据操作 - 高级插入数据 查询的结果数据插入某个表中,可用于数据迁移
insert into 表名 (字段名,字段名...) select 字段名,字段名... from 表名;
表数据操作 - 删除数据,删除全部数据
delete from 表名;
表数据操作 - 删除数据,根据判断条件删除数据
delete from 表名 where 判断条件(字段名 > n);
delete from 表名 where 判断条件(字段名 > n) and 判断条件(字段名 > n) ;
表数据操作 - 高级删除数据,根据判断条件删除部分数据
下面sql意思:删除表中字段值为空的且按指定字段名排好序在删除指定数量的行数
delete from 表名 where 字段名 is null order by 字段名 (asc || desc) limit 数量;
表数据操作 - 高级删除数据,清空表数据,并将表恢复到"初始状态",就跟刚创建的表一样
truncate 表名;
表数据操作 - 更新数据
update 表名 set 字段名 = 字段值, 字段名 = 字段值 where 判断条件(字段名 = n) ;
表数据操作 - 高级更新数据,下面sql意思:值更新按指定字段排序之后得到的数据结果中指定的几行,其他行数据不动
update 表名 set 字段名 = 字段值, 字段名 = 字段值 order by 字段名 (asc || desc) limit 数量;
表数据操作 - 查询数据,查看表内所有数据 * 表示所有
select * from 表名;
表数据操作 - 查询数据,查看表内指定字段
select 字段名, 字段名, ... from 表名;
表数据操作 - 查询数据,根据判断条件查看表内指定字段
select 字段名, 字段名, ... from 表名 where 判断条件(字段名 > n);
select 字段名, 字段名, ... from 表名 where 判断条件(字段名 > n) and 判断条件(字段名 > n) ;
表数据操作 - 高级查询数据 - 长度数量 count(*)
select count(*) as 数量 from 表名;
select 字段名, count(*) as 数量 from 表名;
表数据操作 - 高级查询数据 - 常用计算 count(*) avg() max() min() sum()
select count(*) as 数量, avg(字段名) as 平均值, max(字段名) as 最大值, min(字段名) as 最小值, sum(字段名) as 总和 from 表名;
表数据操作 - 高级查询数据 - 别名 as 或者 字段名后面直接空格写别名也可以,推荐写 as
select 字段名 别名, 字段名 别名, ... from 表名;
select 字段名 as 别名, 字段名 as 别名, ... from 表名;
select 字段名 as 别名, 字段名 as 别名, ... from 表名 where 判断条件(别名 > n);
表数据操作 - 高级查询数据 - 剔除查询结果中的重复数据
select distinct 字段名,字段名 from 表名;
表数据操作 - 高级查询数据 - 条件运算符
select 字段名,字段名 from 表名 where 字段名 > 条件 and 字段名 != 条件;
select 字段名,字段名 from 表名 where 字段名 <= 条件 or 字段名 = 条件;
......
表数据操作 - 高级查询数据 - 模糊查询 like
select 字段名,字段名 from 表名 where 字段名 like '%匹配字符%';
select 字段名,字段名 from 表名 where 字段名 like '%匹配字符';
select 字段名,字段名 from 表名 where 字段名 like '匹配字符%';
select 字段名,字段名 from 表名 where 字段名 like '_匹配字符_';
select 字段名,字段名 from 表名 where 字段名 like '_匹配字符';
select 字段名,字段名 from 表名 where 字段名 like '__匹配字符';
select 字段名,字段名 from 表名 where 字段名 like '匹配字符_';
select 字段名,字段名 from 表名 where 字段名 like '匹配字符__';
select * from 表名 where 字段名 like '_匹配字符%';
表数据操作 - 高级查询数据 - 范围查询 between
select * from 表名 where 字段名 between 范围值 and 范围值;
其实就相当于:
select * from 表名 where 字段名 >= 范围值 and 字段名 <= 范围值;
表数据操作 - 高级查询数据 - 范围查询 in
select * from 表名 where 字段名 in (指定值, 指定值, 指定值);
其实就相当于:
select * from 表名 where 字段名='指定值' or 字段名='指定值' or ...;
表数据操作 - 高级查询数据 - 范围查询 is
select * from 表名 where 字段名 is null;
select * from 表名 where 字段名 is not null;
null 与 not null 只能用 is 来修饰,不能写成:
select * from 表名 where 字段名 = null;
select * from 表名 where 字段名 = not null;
表数据操作 - 高级查询数据 - 分组 group by
select * from 表名 group by 字段名;
select * from 表名 group by 字段名, 字段名;
select count(*) as 数量 from 表名 group by 字段名;
表数据操作 - 高级查询数据 - 分组 group by 之后带条件语句 having
select * from 表名 group by 字段名 having 字段名 > 判断条件;
表数据操作 - 高级查询数据 - 排序 order by
select * from 表名 order by 字段名 (asc || desc, 不写默认 asc);
select * from 表名 order by 字段名 (asc || desc), 字段名 (asc || desc) ...;
select * from 表名 group by 字段名 group by 字段名 (asc || desc);
表数据操作 - 高级查询数据 - 分页 limit
select * from 表名 limit 页码,每页条数;
select * from 表名 order by 字段名 (asc || desc) limit 页码,每页条数;
表数据操作 - 高级查询数据 - 联合查询 union
select * from test
union (all 或 distinct(默认))
select * from user
union (all 或 distinct(默认))
select ......
order by 字段 (asc || desc)
limit 页码,每页条数;
表数据操作 - 高级查询数据 - 连接查询 join
select * from 表名 [连接方式] join 表名 [on 连接条件] where ...;
表数据操作 - 高级查询数据 - 交叉连接 cross join
select * from 表名, 表名;
select * from 表名 join 表名;
select * from 表名 cross join 表名;
表数据操作 - 高级查询数据 - 内连接 inner join
select * from 表名 join 表名 on 连接条件 where ...;
select * from 表名 inner join 表名 on 连接条件 where ...;
表数据操作 - 高级查询数据 - 左外连接 left join
select * from 表名 left [outer] join on 连接条件 where ...;
表数据操作 - 高级查询数据 - 右外连接 right join
select * from 表名 right [outer] join on 连接条件 where ...;
表数据操作 - 高级查询数据 - 自连接
select * from 表名1 as a [连接形式] join 表名1 as b on a.字段名 = b.字段名;
表数据操作 - 高级查询数据 - 子查询
select * from 表名 where 字段名 > (一个子查询语句);
select * from 表名 where 字段名 > (select avg(字段名) from 表名);
表数据操作 - 高级查询数据 - 子查询 in关键字
select * from 表名 where 字段名 = in(select 字段名 from 表名);
select * from 表名 where 字段名 = in(一个子查询语句);
结果相当于:
select * from 表名 where 字段名 in (值1, 值2, 值3, ...);
表数据操作 - 高级查询数据 - 子查询 any关键字 (some 关键字与 any关键字使用功能都一样)
select * from 表名 where 字段名 = any(select 字段名 from 表名);
select * from 表名 where 字段名 = any(必须为子查询语句);
结果相当于:
select * from 表名 where 字段名=值1 or 字段名=值2 or 字段名=值3 or ...;
表数据操作 - 高级查询数据 - 子查询 all关键字
select * from 表名 where 字段名 = all(select 字段名 from 表名);
select * from 表名 where 字段名 = all(必须为子查询语句);
结果相当于:
select * from 表名 where 字段名=值1 and 字段名=值2 and 字段名=值3 and ...;
表数据操作 - 高级查询数据 - 子查询 exists关键字
select * from 表名 where exists(任何子查询);
select * from 商品信息表 where exists (select * from 商品类型表 like '%电%' and 商品信息表.商品类型ID = 商品类型表.商品类型ID)