Mysql

1.定义(分类)

在了解 SQL 之前我们需要知道下面这几个概念

  • 数据定义语言: 简称DDL (Data Definition Language),用来定义数据库对象:数据库、表、列等;
  • 数据操作语言: 简称DML (Data Manipulation Language),用来对数据库中表的记录进行更新。关键字: insert、update、delete等
  • 数据控制语言: 简称DCL(Data Control Language),用来定义数据库访问权限和安全级别,创建用户等。关键字: grant等
  • 数据查询语言: 简称DQL(Data Query Language),用来查询数据库中表的记录,关键字: select from where等

2.CRUD入门

表的创建(DDL)

数据类型:

image.png
image.png
image.png

创建表:

image.png
auto_increment代表自增

约束:

image.png

删除表:

image.png

表的插入,更新,删除(DML)

插入表

image.png

更新表

image.png

删除表

image.png

表的查询(DQL)

基本的查询

image.png
image.png
image.png
distinct代表不重复

条件查询

运算符:

image.png

实现
-- 1.查询 username为 wzz 的人员信息:
select * from user where username='wzz';

-- 2.查询 age大于等于10的人员信息:
select * from user where age>=10;

-- 3.查询 signature是null的人员信息:
select * from user where signature is null;

-- 4.查询 signature不是null的人员信息:
select * from user where signature is not null;

-- 5.查询 commentTime是在对应时间段间的人员信息:
select * from comment where commentTime >= '2022-5-18 23:58:41' and commentTime <= '2022-5-21 23:58:41';

-- 6.查询 id是1,commentTime是在对应时间段间的人员信息:
select * from comment where commentTime <= '2022-5-18 23:58:41' and authorID = 1;

-- 7.查询 or的数据:
select * from user where age = 8 or age =20;
select * from user where age in (8,20);

-- 8.查询 模糊查询:
select * from user where username like '%杨%';

-- 9.查询 模糊查询2个字符的username:
select * from user where username like '__'

聚合函数

image.png

分组查询

image.png
image.png

排序查询

1685629406588.png

分页查询

image.png
image.png

if,when表达式

image.png
image.png
image.png

mysql进阶

连表

一对多

表的创建

image.png

-- auto-generated definition
create table tb_staff
(
    id         int auto_increment comment '组件id'
        primary key,
    username   varchar(20)      not null comment '用户名',
    password   varchar(30)      not null comment '密码',
    name       varchar(10)      not null comment 'name',
    gendar     tinyint unsigned not null comment '性别,1男,2女人

',
    image      varchar(300)     null comment '图片
',
    job        tinyint unsigned null comment '工作
',
    entrydate  date             null comment '入职日期
',
    createtime datetime         null comment '创建时间',
    endtime    datetime         null comment '结束时间',
    deptid     int unsigned     null comment '部门id
',
    constraint tb_staff_username_uindex
        unique (username)
);
-- auto-generated definition
create table tb_dept
(
    id         int auto_increment
        primary key,
    name       varchar(10) not null,
    createtime datetime    not null,
    endtime    datetime    not null,
    constraint table_dept_tname_uindex
        unique (name)
)
    comment '部门表
';


外键

1685668799180.png
image.png

alter table tb_staff drop foreign key tb_staff_tb_dept_id_fk;

alter table tb_staff
add constraint tb_staff_tb_dept1_id_fk
foreign key (deptid) references tb_dept (id);

一对一

1685673008133.png

多对多

image.png

多表查询

select * from tb_staff,tb_dept where tb_staff.deptid = tb_dept.id;

内连接

image.png
image.png

外连接image.png

子查询

image.png
查询name=未来的信息表

select * from tb_staff where deptid = (select id from tb_dept where name='未来');

image.png

行子查询:

image.png

表子查询image.png

事务

概念

![6KB0}LR{4Y]R7(~~LJWH.png

使用

image.png
image.png

四大特性image.png

索引

概念

image.png

优缺点

image.png

语法

image.png
image.png
图片来源:B站黑马程序员,javaweb视频

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿泽不会飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值