MySQL命令
2025-08-20 10:31:48 0 举报
AI智能生成
MySQL常用命令
作者其他创作
大纲/内容
启动关闭
启动MySQL
net start mysql
关闭MySQL
net stop mysql
连接退出
连接
mysql -u<用户名> -p<密码>
mysql -u <用户名> -p
mysql -u<用户名> -p
退出连接
exit
库的使用
创建库
create database <库名>;
create database if not exists <库名>;
如果数据库已存在,则不创建,不存在,则创建
create database if not exists <库名> default charset <utf8mb4>;
默认库的编码格式
显示已有的库
show databases;
查看数据库的定义
show create database <库名>;
选择库
use <库名>;
删除库
drop database <库名>;
查询当前使用的库
select database ();
表的使用
创建表
create table <表名>()
显示已有的表
show tables;
展示表结构
show create table <表名>
show create table <表名> \G
desc <表名>;
字段约束
主键
primary key
外键
foreign key
references
自增
auto_increment
非空
not null
唯一
unique
枚举
enum
默认
default
注释
comment
编码
charset
数据类型
int
integer
float
浮点型
double
双精度
unsigned
无符号
date
yyyy-mm-dd /deɪt/
time
hh:mm:ss /taɪm/
datetime
yyyy-mm-dd hh:mm:ss
timestamp
yyyymmdd hhmmss /ˈtaɪmstæmp/
char
varchar
text
longtext
修改表信息
修改表名
alter table <旧表名> rename to <新表名>;
修改字段
alter table <表名> change <旧字段名> <新字段名> <数据类型>;
修改数据类型
alter table <表名> modify <字段名> <修改后的数据类型>;
添加字段信息
alter table <表名> add <字段名> <字段类型> <约束条件>[[first,after];
first
在首行添加
after <字段名>
在那个字段名下添加
默认最后一行添加
删除字段名
alter table <表名> drop <字段名>;
删除表
drop table <表名>;
增删改查
添加
insert into <表名> values(<添加的值1>,<添加的值2>,<......>);
删除
delete from <表名> where <条件> 【and or】 <条件>;
清空表数据
truncate table <表名>;
更新
update <表名> set <更新内容>,<更新内容> where <更新条件> 【and or】 <更新条件>;
条件
算术运算符
+
加
-
减
*
乘
/
除
% or mod
余
逻辑和比较运算符
and = &&
与
or = ||
或
not = !
非
is null 或者 isnull
空
is not null
不为空
between <开始值> and <结束值>
两个值之间
<字段名> in (1,2,3,'aa')
判断字段值是否在()里面
<字段名> not in (1,2,3,'aa')
判断字段值是否不在()里面
>
大于
>=
大于等于
<
小于
<=
小于等于
=
等于
!=
不等于
聚合函数
count
统计数量
max
最大值
min
最小值
avg
平均值
sum
求和
查询
常见查询
select * from <表名> where <条件> 【and or】 <条件>;
字段别名
select 【<字段名> as <别名> or <字段名> <别名>】 from <表名>;
“as”可以省略
总数据
select count(*) from <表名> where <条件>;
和
select sum(<数值类型的字段名>) from <表名>;
平均
select avg(<数值类型的字段名>) from <表名>;
分页
select * from <表名> limit <开始行>,<每页展示的数量>;
查询指定行
select * from <表名> where <条件> 【and or】 <条件> limit <开始行>,<每页展示的数量>;
select * from <表名> where <条件> 【and or】 <条件> limit <每页展示的数量>;
查询前几行数据
分组
select * from <表名> group by <字段名>;
select * from <表名> where <条件> 【and or】 <条件> group by <字段名>;
select group_concat(<字段名>),<字段名>,<...> from <表名> where <条件> 【and or】 <条件> group by <字段名>;
group_concat()展示分组的全部数据
select count(<字段名>),group_concat(<字段名>),<字段名>,<...> from <表名> where <条件> 【and or】 <条件> group by <字段名>;
count()计算分组后总数据
select group_concat(<字段名>) from <表名> group by <字段名> with rollup;
最后一列显示出所有数据
排序
select * from <表名> order by <字段名> 【asc or desc】;
select * from <表名> where <条件> order by <字段名> 【asc or desc】;
select * from <表名> order by <字段名1> 【asc or desc】,<字段名2> 【asc or desc】;
先对"字段名1"排序,再对"字段名2"排序
去重
select distinct <字段名> from <表名>;
模糊
select * from <表名> where <条件> like '%<匹配数据>_';
(%)模糊匹配 (_)匹配一个字符
范围
select * from <表名> where <字段名> between <开始范围> and <结束范围>;
过滤
select <过滤条件的字段名1> from <表名> having <字段名1><过滤条件>;
查询出的字段必须有过滤条件的字段名
分组过滤
select <过滤条件的字段名1>,<字段名> from <表名> group by <字段名> having <字段名1><过滤条件>;
条件分组过滤
select <过滤条件的字段名1>,<字段名> from <表名> where <条件> and <条件> group by <字段名> having <字段名1><过滤条件>;
分组过滤排序
select <字段名1>,<字段名> from <表名> where <条件> and <条件> group by <字段名> having <字段名1><过滤条件> order by <字段名>;
where>group by>having>order by>limit
内连接
select * from <表名1> inner join <表名2> where <表名1>.<外键>=<表名2>.<主键>;
select * from <表名1> join <表名2> where <表名1>.<外键>=<表名2>.<主键>;
select <表名1>.<表名1中的字段>,<表名2>.<表名2中的字段> from <表名1> join <表名2> where <表名1>.<外键>=<表名2>.<主键>;
select <别名1>.<别名1中字段>,<别名2>.<别名2中字段> from <表名1> <别名1> join <表名2> <别名2> where <别名1>.<外键>=<别名2>.<主键>;
外连接
左连接
把inner join 换成 left join
右连接
把inner join 换成 right join
子查询
select * from <表名1> where <字段名1> 【<in> or <not in>】(select <字段名1> from <表名2> where <条件> and <条件>) and <条件>;
0 条评论
下一页