MySQL常用命令合集及语法

– 1 DDL(数据定义语言) 用于创建和删除数据库对象等操作
– 命令举例 create database(创建数据库) alter database(修改数据库) drop database(删除数据库) use(选择数据库) show database(查看数据库)

– 1.1 显示所有数据库
show database;

– 1.2 创建数据库
create database t121;

– 1.3 使用(进入)数据库
use t121;

– 1.4 查看数据库中的所有表
show tables;

– 1.5 删除数据库
drop database t121test;

– 2 DML(数据操作语言) 用来操作数据库中包含的数据
– 命令举例 desc(查看表结构) create table(创建新表) insert(新增表数据) update(更新表) delete(删除表) show(查看数据库中的表) alter table(修改表)

– 2.1 查看表结构
desc flower;

– 2.2 创建数据库并定义字段
– create table [if not exists] 表名(
– 字段1 数据类型 [字段属性|约束] [索引] [注释],
– 字段2 数据类型 [字段属性|约束] [索引] [注释],
– …
– 字段n 数据类型 [字段属性|约束] [索引] [注释]
– )[表类型][表字符集][注释];

– 2.3.1 字段的属性及约束
– 名称 关键字 说明
– 非空约束 not null 字段不允许为空
– 默认约束 default 赋予某字段默认值
– 唯一约束 unique key(uk) 设置字段的值是唯一的允许为空,但只能有一个空值
– 主键约束 primary key(pk) 设置字段为表的主键可唯一标识该表记录
– 外键约束 foreign key(fk) 用于在两表之间建立关系,需要指定引用主表的哪一个字段
– 自动增长 aout_increment 设置该列为自增字段,默认每条自增1,通常用于设置主键

create table fx_student(
stuNo char(8) PRIMARY KEY comment'学员编号',
stuName varchar(50) not null comment'学员编号',
stuAge tinyint not null comment'年龄',
stuSex tinyint not null comment'性别',
birthDay datetime comment'生日'
)

– 2.4 修改表
– 2.4.1 修改表名
alter table 旧表名 rename[to] 新表名;

– 2.4.2 添加字段
alter table 表名 add 字段名 数据类型[属性];

– 2.4.3 修改字段
alter table 表名 change 原字段名 新字段名 数据类型[属性];

– 2.4.4 删除字段
alter table 表名 drop 字段名;

– 2.5 插入数据
– insert into 表名[(字段名列表)] values(值列表);

– 2.5.1 插入一条数据
insert into student_fx values ('10011011','张三',19,1,'1990-10-16 1:1:1');

– 2.5.2 插入多条数据
insert into student_info(sname,gender,birthday,telephone,email,classId) values
('胡丹丹','女','1990-10-16','12345678901','hdd@163.com',1),
('方辉','男','1993-11-12','12345678901','fh@163.com',2),
('林家栋','男','1992-11-16','12345678901','ljd@163.com',3),
('杨仁','男','1992-10-13','12345678901','yr@163.com',2);

– 2.6 更新数据
– update 表名 set 字段1=值1,字段2=值2,…,字段n=值n[where条件];

– 2.6.1 不含条件
update student_info set sname='胡丹丹',gender='女',birthday='1990-10-16',telephone='12345678901',email='hdd@163.com',classId=1;

– 2.6.1 含条件
update student_info set telephone='12345678902' where id=1;

– 2.7 删除数据
– delete from 表名 [where条件];

delete from student where id=1;

– truncate table 表名
– truncate 语句删除后将充值自增列,表结构及其字段、约束、索引保持不变,执行速度比delete语句快
– truncate 一般用来删除所有字段数据
truncate table student_info;

– 3 DQL(数据查询语句) 用于对数据库中的数据进行查询
– 命令举例 select(查询语句关键字)

– 3.1 单表查询
– select 查询内容 from 表名

– 3.1.1 where 限制条件
– = != < > <= >=

– 3.1.2 between (在庞大的数据面前 between效率更高)在…之间
id>=3 and id<=6;
– 等价于
id between 3 and 6;

– 3.1.3 or 或者
id=3 or id=6;

– 3.1.4 in 后面可以跟随多个值 等价于or
id in (3,6);

– 3.1.5 like 模糊查询
– 3.1.5.1 % 零个或多个字符的字符串
– 以a开头的字符串
like 'a%';
– 以a结尾的字符串
like '%a';
– 包含a的字符串
like '%a%';
– 3.1.5.2 _ 任意一个字符
– 第二个字符为a的字符串
like '_a%';
– 以a开头长度是3的字符串
like 'a__';
– 3.1.5.3 [] 指定范围 (MySQL中无法使用)
– a-z之间的任意一个字符
like '[a-z]';
– abc之间的任意一个字符
like '[abc]';
– 3.1.5.4 [^] 不属于指定范围 (MySQL中无法使用)
– 除了abc以外的任意一个字符
like '[^abc]';
– 3.1.5.5 * 代表多个字符 (MySQL中无法使用)
– 以a开头以c结尾的字符串
like 'a*c';
– 3.1.5.6 ? 代表一个字符 (MySQL中无法使用)
– 以a开头以c结尾长度为3的字符串
like 'a?c';
– 3.1.5.7 # 代表一个数字字符 (MySQL中无法使用)
– 以a开头以c结尾长度为3中间的字符为数字的字符串
like 'a#c';

– 3.1.6 char_length 字符长度
– 姓名长度为2的用户信息
– ps:判断空的时候不能用 = 要用 is
char_length(name)=2;

– 3.1.7 group by 分组依据
– 分组后取出的是每个组的第一条数据

– 3.1.8 having 过滤条件
– 语法和where一样,where后面的条件不能使用聚合函数,但是having可以用
select * from student where 条件 group by 分组 having avg(字段) > 60;

– 3.1.9 order by 排序
– 根据年龄默认升序排列
order by age [asc];
– 根据年龄降序排列
order by age desc;
– 多条件排序
– 先按年龄升序排列,年龄相同再按照身高降序排列
select * from student order by age asc, height desc;

– 3.1.10 limit 限制展示条数
– 3.1.10.1 limit number 从第一条记录开始展示number条
– 所有学员信息中的前3条
select * from student limit 3;
– 查询年龄最大的学员信息
select * from student order by age desc limit 1;

– 3.1.10.2 limit number1,number2 number1代表起始记录的下标(从0开始),number2代表从起始位置开始展示的记录数
– 所有学员信息中的前5条记录
select * from student limit 0,5;
– 所有学员信息中第6条到第10条信息(每页5条,第二页的信息)
select * from student limit 5,5;

– 3.1.11 distinct 去重
– 对重复的数据进行去重操作
– 查询student中所有出现的学员姓名
select distinct name from student;

– 3.2 执行顺序
– from 查询
– where 限制条件
– group by 分组
– having 过滤条件
– order by 排序
– limit 展示条数
– distinct 去重
– select 查询的结果

– 3.3 正则查询
select * from student where name regexp 正则表达式;

– 3.4 聚合查询
– max 最大值
select max(字段) from 表名;

– min 最小值
select min(字段) from 表名;

– avg 平均值
select avg(字段) from 表名;

– sum 求总和
select sum(字段) from 表名;

– count 计数
select count(字段) from 表名;
– 查询成绩大于等于60分的人数
select count(id) from student where score >= 60;

– 3.5 多表查询
– 3.5.1 inner join 连接查询
– 表1 [inner] join 表2 on 表1与表2的关系
select * from student
join score
on student.id=score.studentid
where student.name = '张三';

– 3.5.2 as 起别名 可以省略
select * from student as stu
join score as sc
on stu.id=sc.studentid
where stu.name = '张三';

– 3.5.3 子查询
– 讲一个查询语句的结果作为另一个查询语句的条件
– 查询英语成绩最高的学员信息步骤
– a.通过学员编号查询学员信息
select * from student where id =
– b.查询英语最高分的学员编号
(select studentid from score where englishscore=
– c.查询英语的最高分
(select max(englishscore) from score));

– 3.5 拼接
– concat
select concat(stuno,stuname) from fx_student;
select concat('hello',' mysql');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[微信紅包]恭喜发財,大吉大利!

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值