1.表查询
查看表结构
desc t_student
# 查看表中的字段类型,名字,长度,约束
字段的增加
# 默认在表末尾增加一个字段
alter table 表名 add 字段名 数据类型;
# 添加到某个字段之后
alter table 表名 add 字段名 数据类型 after 字段名(被指定);
# 第一个添加字段,
alter table 表名 add 字段名 数据类型 first;
字段长度/数据类型的修改
# 格式
alter table studenttest modify column 字段名 数据类型(长度);
# 修改长度注意一定要大于原来长度,原有数据会被破坏,不可修改
字段名的修改
alter table 表名 change 旧的字段名 新的字段名 数据类型;
删除字符段
alter table 表名 drop column 字段名;
清空表数据
delete from 表名;
修改表名
alter table 表名 rename 新的表名;
删除表
drop table 表名;
2.表数据的查询(常用)
# 格式:* 就是全部的意思
select * from 表名;
# 查看指定的字段所有数据
where 字段(条件)
where 条件附加
比较运算符:
=
!=
>
<
>=
<=
逻辑运算符
and
or
not
还有其他mysql特有的知识
between 在两个值之间
not betwenn 不在两个值之间
in 在指定集合之中
not in 不在指定集合之中
# 格式
select * from 表名 where 条件语句;
select 字段名1,.... from 表名 where 条件语句;
聚合函数
聚合函数就是: sum, max,min,avg
聚合函数可以引用到select查询中,或者having子句中,但是不能用在where子句中,因为where是对行记录逐条筛选的
# 求平均值
avg(字段名)
# 统计一个字段个数
count(字段名)
3.MySQL表数据查询(必须记住)
约束条件
约束:对表中的数据进行一个限定
创建约束条件是在创建表的时候,写数据类型之后进行创建
不能后面在加入
1.非空约束
表示字段不能为空,插入数据的时候必须传入数据进来
用not null(如果不加系统默认为空)
如:
name char(5) not null
下面的使用方法和上面的一样
默认值约束 default
没有对插入的数据,指定默认值填充
唯一约束 unique
字段数值不能重复(空值除外)
主键约束 primary key
确保数值唯一而且不能为空
注意每一个表只能有一个主键约束
.自增 auto_increment
设置自增字段必须是主键,默认设置为int类型, 从1开始递增
这个其实只在学习或者测试开发阶段运用
一般要加个主键约束 如:(primary key auto_increment)
4.数据更新
表数据的修改
update 表名 set 字段名= 值 , .... where 条件;
# 如果没有where 子句, 字段的所有数据都全部修改
delete from 表名 where 条件;
# 删除某一行数据
表数据删除
delete from 表名 where 条件;
数据查询
select * from 表名
select *from 表名 where 条件
模糊查询
like, 在where子句中可以使用该语句以及关键字进行模糊查询
% 表示匹配0个或者多个字符 (贪婪模式) null除外
_ 表示匹配任意一个字符(有多少个下划线,就匹配多少个字段)相当于非贪婪模式
消除重复项
select distinct * from 表名;
排序
select * from 表名 order by 字段名 desc;
排序默认是升序
asc 升序
desc 降序
在排序中,如果数据重复,那么在该字段对应进行默认排序
分页(限制查询返回条数)
limit 条数 分页 (开发这个是比较麻烦的模块)
一般我们查询的数据, 都是显示全部,如果还是1000w条数据,我们需要进行分页查询
分组
group by 根据字段分组
select * from 表名 where 条件 group by 字段名(特指分组的字段名)
as
as 就是别名
聚合筛选
having 对分组之后的数据再一次进行筛选
注意having通常和group by 一起使用
select * from 表名 group by 字段名 having 条件
子查询(实际上就是嵌套)
在 select 子句中,是可以嵌套select语句
select * from (select * from 表名 where 条件) as 名字 where 条件;
子查询:
select * from 表名 where 条件(select 字段名 from 表名 where 条件)
多表查询(重要)
表与表的关系
1. 一对一
身份证对应你这个人
2. 一对多
老师直播就是一对多
3.多对多
城市和人
外键约束
foreign key (外键) : 建立表与表之间的某种约束,这个关系的存在可以加强表与表之间的关系
主表: 被外键链接进来(这里必须有一个唯一约束(主键,unique))
从表 设计外键约束进行链接表 不需要唯一约束
foreign key (自己表名中的一个键)references 需要衔接的表名(键)
# 创建一个主表
create table t9 (
id int PRIMARY key auto_increment,
name char(5)
);
# 创建一个从表
CREATE table t10(
id int,
age int,
t10id int unique,
foreign key (t10id) references t9(id)
);
联合查询
三大链接
inner join -- 内连接
left join -- 左连接
right join -- 右连接
select * from 表1 inner join 表2 on 连接条件(关联的字段)
其他衔接也一样
触发器
delimiter 结束语
create trigger 触发器名称 触发时间 触发条件 on 表名 for each row
begin
触发事件
end 结束语
delimiter;
# on 表名: 因为这个表名触发
# 触发时间: before 在触发条件之前; after 在触发之后
# 触发发条件: insert , update,delete
# new 新数据 insert update
# old 旧数据 delete,update
create table stu1(
id int primary key auto_increment,
name char(5),
class int,
sex enum("男", "女")
);
create table stu2(
class int,
stu_num int
);
insert into stu1(name, class, sex) VALUES
("张三", 2301, "男"),
("李四", 2301, "女"),
("王五", 2301, "男");
insert into stu2(class, stu_num) VALUES
(2301, 2),
(2302, 0),
(2303, 1);
use demo1;
delimiter !
create trigger addStudent after insert on stu1 for each ROW
begin
# 自定义一个变量
declare cls INT;
# 这里的new相当于stu2 查询出来的数据
# class就是类名 new.class 就是插入后的动作查询出来的内容
# new.class 为啥是2301,insert插入后新数据获取的class数值 2303
# 报错不会显示错误
set cls = (select stu_num from stu2 where class = new.class);
update st2 set stu_num = cls + 1 where class = new.class;
end !
delimiter;
insert into stu1 (name, class, sex) VALUES
('x1', 2301, '男'),
('x2', 2301, '女'),
('x3', 2303, '男');