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