二阶段day2

本文介绍了在MySQL中常见的错误类型,如主键冲突、列数不匹配等,并通过Workbench展示了数据库连接和表间关系,包括一对一、一对多和多对多关系。详细解释了ER实体关系图的概念,并提供了员工与部门的实例。此外,还探讨了多种MySQL查询语句,涉及投影、筛选、模糊匹配和正则表达式等操作。
摘要由CSDN通过智能技术生成

看看报错类型

1、相同主键出现时

在这里插入图片描述

2、列数不匹配

在这里插入图片描述

3、未给字段名赋值且该字段名又不能为空

在这里插入图片描述

使用workbench

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

连接成功

在这里插入图片描述

进入到workbench

在这里插入图片描述

表与表之间的关系

一对一:人和身份证、用户和购物车

  • 任意在其中一方添加对方的主键作为外键

一对多:一部电影 和 电影演员

  • 在多的一方添加对方的主键作为外键

多对多:多个职位 和 多个求职人员

  • 添加二者的关系三方,将二者的主键作为外键添加到第三方将二者连接起来

ER实体关系图

ER实体关系图长啥样?

以员工与部门为例:椭圆为属性,矩形代表实体,如员工、部门,菱形代表关系

在这里插入图片描述

直接看案例:人和共享单车

-- 创建使用者表
create table tb_user
(
uid int unsigned not null auto_increment,
nickname varchar(20) not null,
reg_date datetime not null,
vip_level int not null default 1,
primary key (uid)
);
-- 创建单车表
create table tb_bike
(
bid int not null auto_increment,
in_use boolean not null default 0,
is_broken boolean not null default 0,
primary key (bid)
);
-- 创建第三方,记录表
create table tb_record
(
rid bigint unsigned not null auto_increment comment '流水号',
uid int unsigned not null comment '用户ID',
bid int not null comment '自行车ID',
start_time datetime not null comment '开始时间',
end_time datetime comment '结束时间',
pay_way varchar(10) comment '支付方式',
payment decimal(5,2) comment '骑行费用',
primary key (rid),
foreign key (uid) references tb_user (uid),
foreign key (bid) references tb_bike (bid)
);

MySQL查询语句

1查询所有学生的所有信息

select * from tb_student;

-- 实际写法
select stu_id,stu_name,stu_sex,stu_birth,stu_addr,col_id
from tb_student;

2查询学生的学号、姓名和籍贯(投影)

select stu_id as 学号
,stu_name as 姓名
,stu_addr as 籍贯
from tb_student;

3查询所有课程的名称及学分(投影和别名)

select cou_name as 名称
	,cou_credit as 学分
from tb_course;

4查询所有女学生的姓名和出生日期(筛选)

select stu_name as 姓名
	,stu_birth as 出生日期
from tb_student 
where stu_sex = 0;

– 5查询籍贯为“四川成都”的女学生的姓名和出生日期(筛选)

select stu_name as 姓名
	,stu_birth as 出生日期
from tb_student 
where stu_addr = '四川成都' and stu_sex = 0;

6查询籍贯为“四川成都”或者性别是女的学生

select stu_id
	   ,stu_name
       ,stu_sex
       ,stu_birth
       ,stu_addr
       ,col_id
from tb_student
where stu_addr = '四川成都' or stu_sex = 0;
;

7查询所有80后学生的姓名、性别和出生日期(筛选)

select stu_name as 性别
       ,case stu_sex when 1 then '男'  when 0 then '女' else '未知' end as 性别
       ,stu_birth as 出生日期
from tb_student
where stu_birth>='1980-1-1' and stu_birth<='1989-12-31';

-- 或者between '1980-1-1' and '1989-12-31'

– 8查询学分大于2的课程的名称和学分(筛选)

select cou_name
	  ,cou_credit
from tb_course
where cou_credit>2;

9查询学分是奇数的课程的名称和学分(筛选)

select cou_name
	  ,cou_credit
from tb_course
where cou_credit%2<>0;
-- mod也可以,mod取余

– 10查询选择选了1111的课程考试成绩在90分以上的学生学号(筛选)

select stu_id
from tb_record
where cou_id=1111 and score>90;

11查询名字叫“杨过”的学生的姓名和性别

 select  stu_name as 姓名
		,case stu_sex when 0 then '女' else '男' end as 性别
from tb_student
where stu_name = '杨过';

12查询姓“杨”的学生姓名和性别(模糊),%表示匹配多个字符,_匹配一个字符

 select  stu_name as 姓名
		,case stu_sex when 0 then '女' else '男' end as 性别
from tb_student
where stu_name like '杨%';
-- 注意,使用等号不得行了,要使用like

13查询姓“杨”名字两个字的学生姓名和性别(模糊)

 select  stu_name as 姓名
		,case stu_sex when 0 then '女' else '男' end as 性别
from tb_student
where stu_name like '杨_';
-- 注意,使用等号不得行了,要使用like

14查询姓“杨”名字三个字的学生姓名和性别(模糊)

 select  stu_name as 姓名
		,case stu_sex when 0 then '女' else '男' end as 性别
from tb_student
where stu_name like '杨__';
-- 注意,使用等号不得行了,要使用like

15查询名字中有“不”字或“嫣”字的学生的姓名(模糊)

select  stu_name as 姓名
from tb_student
where stu_name like '%不%' or stu_name like '%嫣%';

-- 或者,方法2
select  stu_name as 姓名
from tb_student
where stu_name like '%不%'
union
-- union并集的意思,mysql只支持并集

select  stu_name as 姓名
from tb_student
where stu_name like '%嫣%';
-- union all,不去掉重复元素

16查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)

-- regexp:regular expression
select  stu_name as 姓名
from tb_student
where stu_name regexp '\[杨林][\\u4e00-\\u9fa5]{2}';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值