Mysql的命令大全

一、数据库

1、创建数据库
sql 复制代码
create database 数据库名 charset utf8;
2、查看数据库
sql 复制代码
show databases;
select databse();
3、删除数据库
sql 复制代码
drop databse 数据库名;
4、使用数据库
sql 复制代码
use 数据库名;

二、数据表

1、创建表
sql 复制代码
create table 表名(字段名 类型);
2、查看表
sql 复制代码
show tables;
3、修改表
sql 复制代码
alter table 表名 modify 字段名 char(6); # 修改字段数据类型
sql 复制代码
alter table 表名 rename 新表名;  # 修改表名
sql 复制代码
alter table 表名 add 字段名 数据类型;  # 新增字段
sql 复制代码
alter table 表名 drop 字段名;  # 删除字段
sql 复制代码
alter table 表名 chaneg 旧字段名 新字段名 数据类型; #修改字段名
4、查看表结构
sql 复制代码
desc 表名;
5、删除表
sql 复制代码
drop table 表名;

三、数据

1、插入数据
sql 复制代码
insert into 表名(字段名) values(数据)
2、查看数据
sql 复制代码
select 字段名 from 表名;
select * from 表名;
3、修改数据
sql 复制代码
update 表名 set 字段名="";
4、删除数据
sql 复制代码
delete from 表名;

四、数据类型

1、数值类型
类型 大小
int(整数) 4字节
float(浮点数) 4字节
double(浮点数) 8字节
smallint(整数) 2字节
bigint(整数) 8字节
2、日期类型
类型 格式
year YYYY(2014)
date YYYY-MM-DD(2014-12-31)
time HH:MM:SS(11:59:30)
datetime YYYY-MM-DD HH:MM:SS
timestamp YYYYMMDD HHMMSS
3、字符类型
类型 区别
char (定长)处理速度更快
vachar (不定长)存储空间少
4、枚举类型与集合类型
类型 区别
enum(枚举) 在给定范围选一个值
set(集合) 在给定范围选多个值

五、约束条件

1、完整性约束
sql 复制代码
create table students(name char(16),sex not null default '男'); # 如果输入性别为空,则默认为男
2、唯一性约束
sql 复制代码
create table students(id int unique,name char(16) unique); # unqie为唯一,可以有多个
3、主键
sql 复制代码
create table students(id int primary key,name char(16)); # 主键不能为空,且必须唯一
sql 复制代码
create table students(id int ,name char(16),primary key(id,name)); # 联合主键
4、自增
sql 复制代码
create table students(id int primary key auto_increment );
5、外键
sql 复制代码
create table students(id int, name char(6),foreign key(class_id) references class(id));

六、单表记录查询

1、定义显示格式
sql 复制代码
select concat('姓名:',name,'年龄',age)as decs from students;
2、去重
sql 复制代码
select distinct name,age from students;
3、条件约束
sql 复制代码
select * from students where age>=18;  # 单条件查询
select * from students where age>=18 and sex="男"; # 多条件查询
select * from students where age between 18 and 25;  # 范围查询
select * from students where name in ('hhq','zxy'); # 包含查询
select * from students where name like "黄%";  # 模糊查询
4、分组查询
聚合函数 作用
max 最大值
min 最小值
avg 平均值
sum 总值
count 计数
mysql 复制代码
select age,count(age) as '年龄人数' from students group by age;
5、过滤
mysql 复制代码
select age,count(age) as '年龄人数' from students group by age having 年龄人数>25;
6、排序
mysql 复制代码
select age from students order by age asc;  # 升序,默认升序
select age from students order by age desc;  # 降序
7、限数
mysql 复制代码
select * from students limit 10;

七、多表记录查询

1、基础查询(本质)
sql 复制代码
select * from studends,class where students.cls_id=class.id;
2、内连接
sql 复制代码
select * from students inner join class on students.cls_id=class.id;  # 查询2张表的共同部分
3、左连接
sql 复制代码
select * from students left join class on students.cls_id=class.id;  # 以左表为准,查询2张表的共同部分
4、右连接
sql 复制代码
select * from students right join class on students.cls_id=class.id;  # 以右表为准,查询2张表的共同部分
5、外连接
mysql 复制代码
select * from students left join class on students.cls_id=class.id
union
select * from students right join class on students.cls_id=class.id;  #同时以左表和右表为准,查询记录

八、视图

1、创建视图
sql 复制代码
create view 视图名 as select * from students where age>18;
2、修改视图
mysql 复制代码
alter view 视图名 as select * from students where age>20;
3、删除视图
sql 复制代码
drop view 视图名;

九、事务

1、开启事务
mysql 复制代码
start transaction;
2、提交事务
mysql 复制代码
commit;
3、回滚
mysql 复制代码
rollback;

十、索引

1、创建索引
复制代码
create index 索引名 on 表名(字段名);
相关推荐
一匹电信狗25 分钟前
【MySQL】数据库的相关操作
linux·运维·服务器·数据库·mysql·ubuntu·小程序
陈一Tender43 分钟前
JavaWeb后端实战(登录认证 & 令牌技术 & 拦截器 & 过滤器)
java·开发语言·spring boot·mysql
TDengine (老段)2 小时前
连接 TDengine 遇到报错 “failed to connect to server, reason: Connection refused” 怎么办?
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
李慕婉学姐3 小时前
Springboot黄河文化科普网站5q37v(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
Cabbage_acmer3 小时前
MySQL期中考试突击!
数据库·mysql
Lu Yao_3 小时前
Redis 缓存
数据库·redis·缓存
小桥流水人家哇4 小时前
性能测试单场景测试时,是设置并发读多个文件,还是设置不同的用户读不同的文件?
数据库·性能测试技巧
表示这么伤脑筋的题我不会4 小时前
Oracle 21C 部署ogg踩过的坑
数据库·oracle
你不是我我4 小时前
【Java 开发日记】MySQL 与 Redis 如何保证双写一致性?
数据库·redis·缓存
望获linux4 小时前
【实时Linux实战系列】实时 Linux 在边缘计算网关中的应用
java·linux·服务器·前端·数据库·操作系统