回顾:
-
怎么打开MySQL服务
-
服务中找到自定义的服务名称 MySQL8
-
开启服务
-
-
如何创建数据库?
create database if not exists schoolDB;
-
删除数据库?
drop database if exists schoolDB;
-
使用数据库?
use schoolDB;
-
创建表?
create table if not exists tb_cls( # 列明 数据类型 约束 c_id int primary key, c_name varchar(20) not null );
create table if not exist tb_stu( s_id int primary key, s_name varchar(20) not null, s_birth date, s_sex varchar(20) default '男', c_id int, constrain fk_stu_cls foreign key(c_id) references tb_stu(c_id) );
-
向表中添加输入 插入操作
insert into tb_cls(c_id, c_name) values(1,'DA2002'); insert into tb_stu(s_id, s_name, s_birth, s_sex, c_id) values(1,'张三','2022-07-04','男',1);
-
修改表中数据 更新
update tb_cls set c_name = 'DD2002' where c_id = 1;
-
删除表中数据
delete from tb_cls where c_id = 1; // 能删除吗? 不能
-
查询表中数据
select * from tb_cls; select c_name from tb_cls; select c_name '姓名' from tb_cls;
10.课前准备
创建数据库
创建班级表
创建学生表
分别向两个表中插入数据
11 .查询全项数据(用的非常多)
select * from tb_stu;
根据主键查询数据 (定位某一条)
select * from tb_stu where s_id=1;
根据某一项查询数据
select * from tb_stu where s_name='张三';
多条件查询 (登录)
select * from tb_stu where c_id=1 and s_name='张三'; select * from tb_stu where c_id=1 or s_name='张三';
范围查询 (多选批量操作)
select * from tb_stu where s_id in(1,3,5);
结果集查询(多选批量操作)
select * from tb_stu where s_id=1 or s_id=3 or s_id=5;
模糊查询 (搜索)
查询关键字?
like
匹配不限数量字符用什么?
%
匹配一个字符用什么?
#知道姓张 两个字 select * from tb_stu where s_name like '张_';
#姓张同学的查询 select * from tb_stu where s_name like '张%';
交叉连接
什么意思?
多个表之间相互配合 一对多
左连接
什么意思?
已左边为主 进行多表联查 left join
右连接
什么意思?
已右边为主 进行多表联查 right join