MYSQL数据库基本知识整理

SQL数据库基本知识整理

1.数据类型

数值类型

image-20240222100100861

#有效位数是5位,小数点保留2位image-20240222101110724

字符类型

image-20240222100642406

日期类型

image-20240222100736325

2.DDL(数据定义语言)

DDL(Data Definition Language),数据定义语言,该语言部分包括以下内容:

对数据库的常用操作

对表结构的常用操作

修改表结构

-- 1、DDL操作之数据库操作
-- 查看所有数据库
SHOW DATABASES;
-- 创建数据库
create database if not exists mydb1;
-- 选择使用哪一款数据库
use mydb1;
-- 删除数据库
drop database if exists mydb1;
-- 修改数据库编码
alter database mydb1 character set utf8;
-- 修改表添加列
ALTER TABLE student add dept varchar(20);
-- 修改列名和类型
ALTER TABLE student change dept department varchar(30);
-- 修改表删除列
ALTER TABLE student drop department;
-- 修改表的名字
rename table student to stu

3.DML(数据库操作)

DML数据库操作-DML

DML是指数据操作语言,用英文全称是Data Manipulation Language,用来对数据库中表的数据记录进行更新。

关键字:

数据插入:insert
数据删除:delete
数据更新:update

数据插入

-- 一一对应的写法
insert into stu(sid,name,gender,age,addr) varlues(1,'小二','男',16,'佛山');
-- 直接写,多条需要加逗号
insert into stu values(1,'小二','男',40,'1999-09-09','广州'),(2,'小三','男',41,'1989-09-09','佛山');
-- 按需修改
insert into stu(sid) values(1004);

数据修改

-- 一列全改
update stu set address = '重庆';
-- 改指定数据
update stu set address = '重庆' where sid = 1;
-- 一次修改多列
update stu set address = '印度' where sid < 3;
update stu set gender = '女',age = 44 where sid < 3;

数据删除

-- 删除行
detele from stu where sid = 1;
-- 删除列
ALTER TABLE student drop department;
-- 删除表所有数据
delete from stu;
-- 清空表数据,整个表删除,再创建一个新表
truncate table stu;

4.练习

image-20240222114435719

create table employee(id int unsigned primary key auto_increment,name varchar(20),gender ENUM('男','女') default NULL,salary int);
insert into employee values('张三','男',2000),('李四','男',1000),('王五','女',4000);
update employee set salary = 5000;
update employee set salary = 3000 where name = '张三';
update employee set salary = 4000,gender = '女'where name = '李四';
update employee set salary = salary + 1000 where name = '王五';

5.约束

5.1.主键约束

-- 主键约束(primary key)PK
概念
MySQL主键约束是一个列或者多个列的组合其值能唯一地标识表中的每一行,方便在RDBMS中尽快的找到某一行。
主键约束相当于 唯一约束+非空约束 的组合,主键约束列不允许重复,也不允许出现空值。
每个表最多只允许一个主键。
主键约束的关键字是:primary key
当创建主键的约束时,系统默认会在所在的列和列组合上建立对应的唯一索引。
# 单个主键
create table emp2(eid INT,name varchar(20),deptId INT,salary double,constraint pk1 primary key(eid));
# 联合主键
CREATE TABLE emp3(name varchar(20),deptId int,salary double,primary key(name,deptId));
# 修改主键
create TABLE emp4(eid int,name varchar(20),deptId int,salary double);
alter table emp4 add constraint primary key(eid);
# 删除单列主键
alter table emp1 drop primary key;

5.2.自增长约束

-- 自增长约束(auto_increment)
概念
在 MySQL 中,当主键定义为自增长后,这个主键的值就不再需要用户输入数据了,而由数据库系统根据定义自动赋值。
每增加一条记录,主键会自动以相同的步长进行增长。
通过给字段添加 auto_increment 属性来实现主键自增长
特点
默认情况下,auto_increment的初始值是1,每新增一条记录,字段值自动加 1。
一个表中只能有一个字段使用 auto_incremen约束,且该字段必须有唯一索引,以避免序号重复(即为主键或主键的一部分)。
auto_increment约束的字段必须具备 NOT NULL属性。
auto_increment约束的字段只能是整数类型TINYINT、SMALLINT、INT、BIGINT等。
auto_increment约束字段的最大值受该字段的数据类型约束,如果达到上限,auto_increment就会失效。
-- 指定自增长初始值
create table t_user2(id int primary key auto_increment,name varchar(20))auto_increment = 100;

# 创建表之后指定
alter table t_user3 auto_increment = 200;

5.3.删除约束

-- truncate删除得比较彻底,会重新开始,delete会保留自增
delete from t_user1; 
truncate t_user1

5.4.非空约束

-- 非空约束(not null)
alter table t_user6 modify name varchar(20) not null;

5.5.唯一约束

-- 唯一约束
# 添加
alter table employee add constraint uni unique(salary);
# 删除
alter table employee drop index uni;

5.6.默认约束

-- 默认约束(default)
MySQL 默认值约束来指定某列得默认值
create table t_user(id int,name varchar(20),address varchar(20) default '北京');
insert into t_user(id,name) values(1,'小黄');
# 添加
alter table t_user modify address varchar(20) default '上海';
# 删除
alter table t_user modify address varchar(20) default null;

5.7.零填充约束

-- 零填充约束(zerofill)
概念
1、插入数据时,当该字段的值的长度小于定义的长度时,会在该值的前面补上相应的0
2、zerofill默认为int(10)
3、当使用zerofill 时,默认会自动加unsigned(无符号)属性使用unsigned属性后,数值范围是原值的2倍,例如,有符号为-128~+127,无符号为0~256。
# 增加
use mydb1;
create table t_user12(id int zerofill,name varchar(20));
# 删除
alter table t_user12 modify id int;

5.8.外键约束

-- 外键约束(foreign key)FK
概念
MySQL外键约束(FOREIGN KEY)是表的一个特殊字段,经常与主键约束一起使用。对于两个具有关联关系的
表而言,相关联字段中主键所在的表就是主表(父表),外键所在的表就是从表(子表)。

外键用来建立主表与从表的关联关系,为两个表的数据建立连接,约束两个表中数据的一致性和完整性。比如一个水果摊,只有苹果、桃子、李子西瓜等4种水果,那么,你来到水果摊要买水果就只能选择苹果、桃子李子和西瓜,其它的水果都是不能购买的。

特点
定义一个外键时,需要遵守下列规则:
1.主表必须已经存在于数据库中,或者是当前正在创建的表。
2.必须为主表定义主键。
3.主键不能包含空值,但允许在外键中出现空值。也就是说,只要外键的每个非空值出现在指定的主键中,这个外键的内容就是正确的。
4.在主表的表名后面指定列名或列名的组合。这个列或列的组合必须是主表的主键或候选键。
5.外键中列的数目必须和主表的主键中列的数目相同。
6.外键中列的数据类型必须和主表主键中对应列的数据类型相同。
image-20240223160955521

-- 例子1
-- 创建数据库
create database mydb3 charset utf8;
use mydb3;
-- 创建从表
create table if not exists dept(deptno varchar(20) primary key,name varchar(20)); 
-- 创建主表
create table if not exists emp(eid varchar(20) primary key,ename varchar(20),age int, dept_id varchar(20),constraint emp_fk foreign key(dept_id) references dept(deptno)); 
-- 后期添加
alter table emp add constraint emp_fk foreign key(dept_id) references dept(deptno);
-- 删除
alter table emp drop FOREIGN KEY emp_fk;
-- 例子2
-- 部门表
create table if not exists dept2(deptno varchar(20) primary key,name varchar(20));
-- 员工表
create table if not exists emp2(eid varchar(20) primary key,ename varchar(20),age int,dept_id varchar(20));
-- 添加外键
alter table emp2 add constraint emp2_fk foreign key(dept_id) references dept2(deptno);
-- 例子3
# 多表关系
-- 1、添加主表数据
-- 注意必须先给主表添加数据
insert into dept values('1001','研发部');
insert into dept values('1002','销售部');
insert into dept values('1003','财务部');
insert into dept values('1004','人事部');

-- 2、添加从表数据
-- 加数据时,外键列的值不能随便写,必须依赖主表的主键列注意给从表添
insert into emp2 values('1','乔峰',20,'1001');
insert into emp2 values('2','段誉',21,'1001');
insert into emp2 values('3','虚竹',23,'1001');
insert into emp2 values('4','阿紫',18,'1002');
insert into emp2 values('5','扫地僧',35,'1002');
insert into emp2 values('6','李秋水',33,'1003');
insert into emp2 values('7','鸠摩智',50,'1003');
insert into emp2 values('8','天山童姥',60,'1005');-不可以
-- 3、删除数据
注意:
1:主表的数据被从表依赖时,不能删除,否则可以删除2:从表的数据可以随便删除
delete from dept where deptno='1001'; --不可以删除
delete from dept where deptno='1004'; --可以删除
delete from emp2 where eid='7'; --可以删除
-- 4、删除约束
alter table emp2 drop foreign key emp_fk1;
-- 5、多对多关系
-- 学生表和课程表(多对多)
 -- 1 创建学生表student(左侧主表)
 create table if not exists student(sid int primary key auto_increment,name   varchar(20),age int,gender varchar(20));

 -- 2 创建课程表course(右侧主表)
 create table course(cid int primary key auto_increment,name varchar(20));

 -- 3 创建中间表student_course/score(从表)
 create table score(sid int,cid int,score double);
 
 -- 4 建立外键约束
 alter table score add foreign key(sid) references student(sid);
 alter table score add foreign key(cid) references course(cid);
 
 -- 5 给学生表添加数据
 insert into student values(1,'小龙女',18,'女'),(2,'阿紫',19,'女'),(3,'周芷若',20,'男');
 
 -- 6 给课程表添加数据
insert into course values (1,'语文'),(2,'数学'),(3,'英语');

 -- 7 给中间表添加数据
 insert into score values(1,1,78),(1,2,75),(2,1,88),(2,3,80),(3,2,90),(3,3,65);
 
 -- 8 修改和删除时,中间从表可以随便删除和修改,但是两边的主表受从表依赖的数据不能删除和修改。
image-20240226145421087

6.DQL(基本查询)

6.1.简单查询

语法格式
select

[all,distinct]

<目标列的表达式1>[别名] <目标列的表达式2>[别名]… from <表名或视图名>[别名],<表名或视图名>[别名]…

[where<条件表达式>]

[group by <列名> [having <条件表达式>]] [order by<列名>[asc | desc]] [limit<数字或者列表>];

-- 例子1
-- 创建数据库和表
create database if not exists mydb2 charset utf8;
use mydb2;
create table product(pid int primary key auto_increment,pname varchar(20) not null,price double,category_id varchar(20));

-- 添加数据
insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',5000,'c001');

insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');

insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');

insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,'c004');

-- 简单查询
select * from product;
select pname,price from product;
select * from product as p;
select pname as '商品名',price as '价格' from product;
select distinct price from product;
select pname,price + 10 as new_price from product;

6.2.运算符操作

-- 算术运算符

select 6+2;
select 6-2;
select 6*2;
select 6/2;
select 6%2;

select pname,price + 10 as new_price from product;
select pname,price * 1.1 as new_price from product;

-- 比较运算符 -- 逻辑运算符

select * from product where pname = '海尔洗衣机';
select * from product where price = 800;

-- 价格不是800的商品
select * from product where price != 800;
select * from product where price <> 800;
select * from product where not(price =800);

select * from product where price >= 60;

-- 价格在200与1000之间
select * from product where price between 200 and 1000;
select * from product where price > 200 and price < 1000;

-- 查询价格200或800的所有商品
select * from product where price in(200,800);
select * from product where price = 200 or price = 800;
select * from product where price = 200 || price = 800;

-- 查询含有'裤'字的所有商品
select * from product where pname like '%裤%'; -- %用来匹配任意字符
-- 查询含有'海'字的开头的所有商品
select * from product where pname like '海%';
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%'; -- '_'下划线匹配单个字符

-- 查询category_id为null的商品
select * from product where category_id is null;
-- 查询category_id不为null的商品
select * from product where category_id is not null;

-- 使用least求最小值
select least(10,5,20) as small_num;
select least(10,null,20) as small_num;  -- 如果有一个是null就不能进行正常比较

-- 使用greatest求最大值
select greatest(10,5,20) as big_num;    
select greatest(10,null,20) as big_num;  -- 如果有一个是null就不能进行正常比较


-- 位运算符(后期了解)

6.3.排序查询

-- asc升序(默认升序)
select * from product order by price asc;

-- desc降序
select * from product order by price desc;
select distinct price from product order by price desc;
select * from product order by price desc,category_id desc;

6.4.聚合函数

之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值;另外聚合函数会忽略空值。

image-20240227204250722

-- 1 查询商品的总条数
select count(pid) from product;
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(pid) from product where price >=200;
-- 3 查询分类为'c001'的所有商品的总和
select sum(price) from product where category_id='c001';
-- 4 查询商品的最大价格
select pname,max(price),category_id from product;
-- 5 查询商品的最小价格
select pname,min(price),category_id from product;
select max(price) max_price,min(price) min_price from product;
-- 6 查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id='c002';
-- 

6.5.聚合查询-null值处理

1、count函数对null值的处理
如果count函数的参数为星号(*),则统计所有记录的个数。而如果参数为某字段,不统计含null值的记录个数。
2、sum和avg函数对null值的处理
这两个函数忽略null值的存在,就好象该条记录不存在一样。
3、max和min函数对null值的处理
max和min两个函数同样忽略null值的存在。
-- 测试
create table test_null(c1 varchar(20),c2 int);

insert into test_null values('aaa',3);
insert into test_null values('bbb',3);
insert into test_null values('ccc',null);
insert into test_null values('ddd',6);

select count(*),count(1),count(c2) from test_null;
select sum(c2),max(c2),min(c2),avg(c2) from test_null;

6.6.分组查询

-- group by
-- 计算每类商品有多少件商品
select category_id,count(pid) from product group by category_id;
-- having 分组条件筛选
-- 分组之后对统计结果进行筛选的话必须使用having,不能使用where(where后面不能跟聚合函数)
-- where子句用来筛选 FROM 子句中指定的操作所>产生的行 
-- group  by  子句用来分组 WHERE 子句的输出。 
-- having 子句用来从分组的结果中筛选行

-- 统计各个分类商品的个数,且只显示个数大于4的信息
select category_id,count(*) from product group by category_id having count(*) > 4;

-- SQL执行顺序:from->group by->count(pid)->select->having
select 
    category_id,count(pid) cnt 
from 
    product 
group by 
    category_id 
having 
    cnt > 4 
order by 
    cnt;

6.7.分页查询

-- 分页查询在项目开发中常见,由于数据量很大,显示屏长度有限,因此对数据需要采取分页显示方式。例如数据共有30条,每页显示5条,第一页显示1-5条,第二页显示6-10条。  
-- 显示前5条
select * from product limit 5;
-- 从第4条开始显示,显示5条
select * from product limit 3,5;
select * from product limit 0,60;   --第一页
select * from product limit 60,60;  --第二页
select * from product limit 120,60; --第三页
select * from product limit (n-1)*60,60; --第n页

6.8.insert into,select语句

-- 将一张表的数据导入到另一张表中,可以使用INSERT INTO SELECT语句。
create table product2(pname varchar(20),price DOUBLE);
insert into product2(pname,price) select pname,price from product;
create table product3(category_id varchar(20),product_count int);
insert into product3 select category_id,count(pid) from product GROUP BY category_id;

6.9.书写顺序

-- 书写顺序
select category_id,count(pid) cnt from product where price > 1000 group by category_id having cnt > 4 order by cnt limit 1;
-- 执行顺序
from-->where-->group by-->count(pid)-->having-->select-->order by-->limit

6.10.练习

-- 数据1
use mydb2;
create table student(id INT,name varchar(20),gender varchar(20),chinese INT,english INT,math INT);

INSERT INTO student(id,NAME,gender,chinese,english,math) values(1,'张明','男',89,78,90);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(2,'李进','男',67,53,95);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(3,'王五','女',87,78,77);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(4,'李一','女',88,98,92);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(5,'李财','男',82,84,67);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(6,'张宝','男',55,85,45);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(7,'黄蓉','女',75,65,30);
INSERT INTO student(id,NAME,gender,chinese,english,math) values(7,'黄蓉','女',75,65,30);
-- 查询表中所有学生的信息
select * from student;

-- 查询表中所有学生的姓名和对应的英语成绩
select name as '姓名',english as '英语成绩' from student;

-- 过滤表中重复数据
select DISTINCT * from student;

-- 统计每个学生的总分
select NAME as '姓名',sum(chinese + english + math) as '总分' from student group by NAME;

-- 统计所有学生总分数加10分特长分
select NAME as '姓名',sum(chinese + english + math + 10) as '总分'from student group by NAME;

-- 使用别名表示学生分数
select NAME as '姓名',chinese '语文成绩',english '英语成绩',math '数学成绩' from student;

-- 查询英语成绩大于90分得同学
select * from student where english > 90;

-- 查询总分大于200分得所有同学
select * from student where (chinese + english + math) > 200;
select *,chinese + english + math as total_score from student where (chinese + english + math)> 200;

select *,chinese + english + math as total_score from student where total_score > 200; --错误写法

-- 查询英语分数在80-90之间得同学
select NAME as '姓名',english as '英语分数' from student where english between 80 and 90;

-- 查询英语分数不在80-90之间得同学
select NAME as '姓名',english as '英语分数' from student where english not between 80 and 90;

select NAME as '姓名',english as '英语分数' from student where english < 80 || english > 90;

-- 查询数学分数为89,90,91的同学
select * from student where math in(89,90,91);

-- 查询数学分数不为为89,90,91的同学
select * from student where math not in(89,90,91);

-- 查询所有姓李的学生英语成绩
select *,english from student where name like '李%';

-- 查询数学80并且语文分80的同学
select * from student where math = 80 and chinese = 80;

-- 查询英语80或者总分200的同学
select * from student where english = 80 or (math + chinese + english) > 200;

-- 对数学成绩降序排序后输出
select * from student order by math desc;

-- 对总分排序后输出,然后再按从高到低的顺序输出
select * from student order by (chinese+math+english) desc;

-- 对姓李的学生成绩排序输出
select * from student where name like '李%' order by (chinese+math+english) desc;

-- 查询男生和女生分别有多少人,并将人数降序排序输出
select gender,count(*) as total from student group by gender order by count(*) desc;

-- 查询男生和女生分别有多少人,并将人数大于4的显示出来
select gender,count(*) as total from student group by gender having count(*) >4;
-- 数据2
create table emp(empno int,ename varchar(50),job varchar(50),mgr int,hiredate date,sal int,comm int,deptno int);

INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp VALUES(7908,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);
-- 1、按员工编号升序排列不在10号部门工作的员工信息
select * from emp where deptno != 10 order by empno asc;

-- 2、查询姓名第二个字幕不是"A"且薪水大于1000元的员工信息,按年薪降序排列
-- 年薪:12*月薪 + 奖金
-- ifnull(comm,0) 如果comm的值为null,则当0,不为null,则还是原来的值
select * from emp where ename not like '_A%' and sal > 1000; 
select * from emp where ename not like '_A%' and sal > 1000 order by (12*sal + ifnull(comm,0)) desc; 

-- 3、求每个部门的平均薪水
select deptno as 部门,avg(sal) as '平均薪水' from emp group by deptno order by avg(sal);

-- 4、求各个部门的最高薪水
select deptno as 部门,max(sal) as '最高薪水' from emp group by deptno order by max(sal);

-- 5、求每个部门每个岗位的最高薪水
select deptno,job,max(sal) from emp group by deptno,job order by deptno,max(sal) desc;

-- 6、求平均薪水大于2000的部门编号
select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;

-- 7、将部门平均薪水大于1500的部门编号列出来,求部门平均薪水降序排列
select deptno,avg(sal) from emp group by deptno having avg(sal) > 1500 order by avg(sal) desc;

-- 8、选择公司中有奖金的员工姓名、工资
select ename,sal from emp where comm is not null;

-- 9、查询员工最高工资和最低工资的差距
select max(sal)-min(sal) as '最高与最低薪资差距' from emp;

7.正则表达式

简单操作

image-20240303205900246

image-20240303210142800

-- ^ 在字符串开始处进行匹配
select 'abc' REGEXP '^a';
-- 以‘海’字开头的商品
select * from product where pname REGEXP '^海'; 
-- $ 在字符串末尾开始匹配
-- 是否以a结尾
select 'abc' REGEXP 'a$'; -- 0 
-- 是否以c结尾
select 'abc' REGEXP 'c$'; -- 1
-- 以‘水’字结尾的商品
select * from product where pname REGEXP '水$';
-- .匹配任意字符,可以匹配除了换行符以外的任意字符
select 'abc' REGEXP '.b'; -- 1
select 'abc' REGEXP '.c'; -- 1
select 'abc' REGEXP 'a.'; -- 1
-- [...] 匹配括号内的任意单个字符
select 'abc' REGEXP '[xyz]'; -- 0 正则表达式的任意字符是否在前边的字符串中出现
select 'abc' REGEXP '[xaz]'; -- 1
-- [^...] 注意^符合只有[]内才是取反的意思,在别的地方都是表示开始匹配
select 'a' REGEXP '[^abc]';   -- 0
select 'x' REGEXP '[^abc]';   -- 1
select 'abc' REGEXP '[^a]';   -- 1
-- a* 匹配0个或多个a,包括空字符串。可以作为占位符使用。有没有指定字符都可以匹配到数据
select 'stab' REGEXP '.ta*b'; -- 1
select 'stb' REGEXP '.ta*b';  -- 1
select '' REGEXP 'a*';        -- 1
-- a+ 匹配1个或者多个a,但是不包括空字符
select 'stab' REGEXP '.ta+b';  -- 1
select 'stb' REGEXP '.ta+b';   -- 0
-- a? 匹配0个或者1个a
select 'stb' REGEXP '.ta?b';   -- 1
select 'stab' REGEXP '.ta?b'   -- 1
select 'staab' REGEXP '.ta?b'  -- 0
-- a1|a2 匹配a1或者a2,
select 'a' REGEXP 'a|b';   -- 1
select 'b' REGEXP 'a|b';   -- 1
select 'b' REGEXP '^(a|b)'; -- 1   -- 以a或者b开头
select 'a' REGEXP '^(a|b)'; -- 1   -- 以a或者b开头
select 'c' REGEXP '^(a|b)'; -- 0
-- a{m} 匹配m个a
select 'auuuuc' REGEXP 'au{4}c';   -- 1
select 'auuuuc' REGEXP 'au{3}c';   -- 0
-- a{m,} 匹配m个或者更多个a
select 'auuuuc' REGEXP 'au{3,}c';  -- 1
select 'auuuuc' REGEXP 'au{4,}c';  -- 1
select 'auuuuc' REGEXP 'au{5,}c';  -- 0 -- 至少要出现5个u
-- a{m,n} 匹配m到n个a,包含m和n
select 'auuuuc' REGEXP 'au{3,5}c';  -- 1 至少3次,最多5次
select 'auuuuc' REGEXP 'au{4,5}c';  -- 1
select 'auuuuc' REGEXP 'au{5,10}c'; -- 0
-- (abc)
abc作为一个序列匹配,不用括号括起来都是用单个字符去匹配,如果要把多个字符作为一个整体去匹配需要用到括号,所以括号适合上面的所有情况。
select 'xababy' REGEXP 'x(abab)y';     -- 1
select 'xababy' REGEXP 'x(ab)*y';      -- 1
select 'xababy' REGEXP 'x(ab){1,2}y';  -- 1
select 'xababy' REGEXP 'x(ab){3}y';    -- 0

8.多表联合查询

外键约束对多表查询并无影响

image-20240307002635184

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注