mysql基础语句

  1. sql语句可以单行或多行书写,以分号结尾。
  2. sql语句不区分大小写
  3. 注释
    - 单行注释: – 注释内容
    - 多行注释: /* 注释内容 */

sql分类

  • DDL(Data definition Language):数据定义语言,用来定义数据库对象:数据库、表、列等
  • DML(Data Manipulation Language):数据操作语言,用来对数据库中表的数据进行增删改
  • DQL(Data Query Language):数据查询语言,用来查询数据库中表的记录(数据)
  • DCL(Data Control Language):数据控制语言,用来定义数据可以的访问权限和安全级别,以及创建用户

DDL

-- DDL
-- 展示所有的数据库
show databases;

-- 创建数据库
create database if not exists db1;

-- 删除数据库
drop database if exists db1;

-- 使用数据库
use db1;
-- 查看当前使用的数据库
select database();

-- 创建表
create table tb_user
(
    id       int,
    username varchar(20),
    password varchar(30)
);
-- 查询表
show tables;
-- 查询表结构
desc tb_user;
/*
    张三
    char(10):定长字符串,占用10个字符空间,性能高,浪费空间
    varchar(10):边长字符串,占用2个字符空间,性能低,节约空间
*/

create table if not exists student
(
    id       int,
    name     varchar(10),
    gender   char(1),
    birthday date,
    score    double(5, 2),
    email    varchar(64),
    tel      varchar(15),
    state    tinyint
);

desc student;

-- 删除表
drop table if exists tb_user;

-- 修改表名
alter table student rename stu;
desc stu;
-- 添加一列
alter table stu add addr varchar(50) after email;

-- 修改数据的类型
alter table stu modify addr char(50);

-- 修改列名和数据类型
alter table stu change addr address varchar(55);

-- 删除列
alter table stu drop address;

DML

-- DML 数据的增删改
-- 给指定列添加数据
insert into stu(id, name)
values (1, '张三');

-- 给所有列添加数据
insert into stu(id, name, gender, birthday, score, email, tel, state)
values (1, '李四', '男', '1994-06-06', 89.89, '6666@qq.com', '18888888888', 1);

insert into stu
values (2, '李四', '男', '1994-06-06', 89.89, '6666@qq.com', '18888888888', 1);

-- 批量添加
insert into stu
values (3, '李四', '男', '1994-06-06', 89.89, '6666@qq.com', '18888888888', 1),
       (4, '李四', '男', '1994-06-06', 89.89, '6666@qq.com', '18888888888', 2),
       (5, '李四', '男', '1994-06-06', 89.89, '6666@qq.com', '18888888888', 3);

-- 修改数据
update stu
set gender = '女'
where id = 1;

update stu
set gender='女',
    birthday='1900-10-10',
    score=99.99
where id = 1;

-- 删除
delete
from stu
where id = 1;    

DQL

-- DQL 查询
-- 查询所有字段的数据
select *
from stu;

create table stu
(
    id        int,
    name      varchar(5),
    age       int,
    sex       varchar(5),
    address   varchar(100),
    math      double(5, 2),
    english   double(5, 2),
    hire_date date
);

select *
from stu;

insert into stu(id, name, age, sex, address, math, english, hire_date)
values (1, '马云', 55, '男', '杭州', 66, 78, '1995-09-01'),
       (2, '张三', 45, '女', '杭州', 98, 78, '1995-09-01'),
       (3, '张无忌', 55, '男', '杭州', 56, 77, '1995-09-01'),
       (4, '杨逍', 20, '男', '杭州', 76, 65, '1995-09-01'),
       (5, '杨过', 20, '男', '杭州', 86, null, '1995-09-01'),
       (6, '小龙女', 57, '女', '杭州', 88, 99, '1995-09-01'),
       (7, '唐三', 22, '男', '杭州', 78, 78, '1995-09-01'),
       (8, '萧炎', 18, '男', '杭州', 23, 100, '1995-09-01');

-- ============== 基础查询 =============

select name, age, address
from stu;

-- 去除重复记录
select distinct address
from stu;

-- as 起别名
select name, math as 数学, english 英语
from stu;

-- ============== 条件查询 =============

select *
from stu
where age > 20;
select *
from stu
where age > 20
  and age <= 45;
select *
from stu
where age between 20 and 55;

select *
from stu
where hire_date between '1996-09-01' and '1999-09-09';

-- 等于
select *
from stu
where age = 18;

-- 不等于
select name, age
from stu
where age != 20;
select name, age
from stu
where age <> 20;

-- 或者
select name, age
from stu
where age = 18
   or age = 20
   or name = '小龙女';

select name, age
from stu
where age in (18, 20);

-- 查询数据为空的数据
select *
from stu
where english is null;

-- 查询姓张的两个字的名字
select *
from stu
where name like '张_';

-- 查询姓张的名字
select *
from stu
where name like '张%';

-- 查询包含 龙 的姓名
select *
from stu
where name like '%龙%';

-- ============== 排序查询 =============
-- asc 升序(默认)
-- desc 降序

-- 根据年龄升序排列
select name, age
from stu
order by age;

-- 降序
select math as 数学, name
from stu
order by math desc;

-- 根据过个条件排序
select name, math, english
from stu
order by math desc, english desc;

-- ============== 分组查询 =============
-- select 字段列表 from 表名 [where 分组前条件限定] group by 分组字段名 [having 分组后条件过滤]
/*
    聚合函数
    count:统计数量
    max:
    min
    sum
    avg 平均
*/

-- 统计,统计的列名的数据不能为空
select count(*)
from stu;

select count(id)
from stu;

select max(math)
from stu;

select min(english)
from stu;

select avg(math)
from stu;

select avg(english)
from stu;

-- 查询男同学和女同学各自的数学平均分
-- 注意:分组之后,查询的字段为聚合函数和分组字段,查询其他字段无任何意义
select sex, avg(math)
from stu
group by sex;

-- 查询男同学和女同学各自的数学平均分,以及各自的人数
select sex, avg(math), count(*)
from stu
group by sex;

-- 查询男同学和女同学各自的数学平均分,以及各自的人数,分数低于70的不参与分组
select sex, avg(math), count(*)
from stu
where math > 70
group by sex;

-- 查询男同学和女同学各自的数学平均分,
-- 以及各自的人数,分数低于70的不参与分组
-- 分组之后人数大于2的
select sex, avg(math), count(*)
from stu
where math > 70
group by sex
having count(*) > 2;

-- ============== 分页查询 =============
-- select 字段列表 from 表名 limit 起始索引,查询条目数
-- 起始索引:从0开始
-- 计算公式:起始索引 = (当前页面-1)*每页显示的条数

-- 从0开始查询,查询3条数据
select id, name
from stu
limit 0,3;

-- 每页显示3条数据,查询第1页数据
select id, name
from stu
limit 0,3;

-- 每页显示3条数据,查询第2页数据
select id, name
from stu
limit 3,3;

-- 每页显示3条数据,查询第3页数据
select id, name
from stu
limit 6,3;

DCL

-- DCL
-- 查询用户
use mysql;
select *
from user;

-- 创建用户
-- 如果希望任意主机都能访问mysql 只需要把localhost 替换成 %
create user 'admin'@'localhost' identified by 'admin123';

-- 修改密码
alter user 'admin'@'localhost' identified with mysql_native_password by 'adminadmin';

/*
    权限控制
    all,all privileges 所有权限
    insert 插入
    delete 删除数据
    update 修改数据
    select 查询
    alert  修改表
    drop   删除数据库、表、视图
    create 创建数据库、表
*/
-- 查询 权限
show grants for 'admin'@'localhost';

-- 授予权限
grant all on mysql.user to 'admin'@'localhost';
grant select, update on db1.stu to 'admin'@'localhost';
grant select, update on mysql.user to 'admin'@'localhost';

-- 撤销权限
revoke all on mysql.user from 'admin'@'localhost';

-- 删除用户
drop user 'admin'@'localhost';

约束

  • 非空约束 not null
  • 唯一约束 unique
  • 主键约束 primary key
  • 检查约束 check (mysql 不支持)
  • 默认约束 default
  • 外健约束 foreign key

基本约束

drop table if exists emp;

create table emp
(
    id       int primary key auto_increment,-- 主键且自增长
    ename    varchar(50)  not null unique, -- 员工姓名,非空且唯一
    joindata date         not null, -- 入职日期 非空
    salary   double(7, 2) not null, -- 工资非空
    bonus    double(7, 2) default 0 -- 奖金 默认为0
);

外健约束

/**
  创建表时添加
  create table 表名(
    列名 数据类型,
    ...
    [constraint] 外健名称 foreign key(外健列名) reference 主表(主表列名)
  );

  创建表后添加
  alert table 表名 add constraint 外健名称 foreign key(外健字段名称) reference 主表名(主表列名称)
 */
 
 -- 部门表
create table dept
(
    id       int primary key auto_increment,
    dep_name varchar(20),
    addr     varchar(20)
);
 
 -- 员工表
create table emp
(
    id     int primary key auto_increment,
    name   varchar(20),
    age    int,
    dep_id int not null,
    -- 添加外键dep_id,关联dept表的id主键
    constraint fk_emp_dept foreign key (dep_id) references dept (id)
);

insert into dept(dep_name, addr)
values ('研发部', '广州'),
       ('销售部', '深圳');

insert into emp(name, age, dep_id)
values ('张三', 20, 1),
       ('李四', 20, 1),
       ('王五', 20, 1),
       ('jack', 20, 2),
       ('jerry', 20, 2),
       ('tom', 20, 2);
       

-- 删除外键
alter table emp
    drop foreign key fk_emp_dept;

-- 添加外键
alter table emp
    add constraint fk_emp_dept foreign key (dep_id) references dept (id);

多表之间关系的练习


-- 练习
/**
    专辑  曲目 是一对多
    专辑  短评 是一对多
    用户  短评 是一对多
    专辑  用户 是多对多
 */

drop table if exists song;
drop table if exists review;
drop table if exists user;
drop table if exists music;

create table if not exists music
(
    id           int primary key auto_increment,
    title        varchar(32) comment '专辑名',
    alias        varchar(32) comment '专辑别名',
    image        varchar(64) comment '封面图片',
    style        varchar(8) comment '流派',
    type         varchar(4) comment '类型',
    medium       varchar(4) comment '介质',
    publish_time date comment '发行时间',
    publisher    varchar(16) comment '出版者',
    number       tinyint comment '唱片数',
    barcode      bigint comment '条形码',
    summary      varchar(1024) comment '简介',
    artist       varchar(16) comment '艺术家'
);


create table song
(
    id            int primary key auto_increment,
    serial_number tinyint comment '歌曲序号',
    name          varchar(32) comment '歌曲名',
    music_id      int comment '专辑id'
);

create table review
(
    id          int primary key auto_increment,
    content     varchar(256) comment '评论内容',
    review_time datetime comment '评论时间',
    music_id    int comment '专辑id',
    user_id     int comment '用户id'
);

create table user
(
    id        int primary key auto_increment,
    username  varchar(16) unique not null comment '用户名',
    image     varchar(64) comment '用户头像地址',
    signature varchar(64) comment '个人签名',
    nickname  varchar(16) comment '用户昵称'
);

create table if not exists tb_music_user
(
    id       int primary key auto_increment,
    music_id int,
    user_id  int
);

-- alter table emp
-- add constraint fk_emp_dept foreign key (dep_id) references dept (id);

-- 专辑 曲目 一对多
alter table song
    add constraint fk_song_music foreign key (music_id) references music (id);

-- 专辑 短评 一对多
alter table review
    add constraint fk_review_music foreign key (music_id) references music (id);

-- 用户 短评 一对多
alter table review
    add constraint fk_review_user foreign key (user_id) references user (id);

-- 用户 专辑 多对多
-- 多对多是需要借助第三张表来实现
alter table tb_music_user
    add constraint fk_music_id foreign key (music_id) references music (id);

alter table tb_music_user
    add constraint fk_user_id foreign key (user_id) references user (id);


多表查询

  1. 连接查询
    • 内连接:相当于查询A B交集数据
    • 外连接
      • 左外连接:相当于查询A表中所有的数据和交集部分数据
      • 右外连接:相当于查询B表中所有的数据和交集部分数据
  2. 子查询

模拟数据

drop table if exists emp;
drop table if exists dept;

create table if not exists dept
(
    did   int primary key auto_increment,
    dname varchar(20)
);

create table if not exists emp
(
    id       int primary key auto_increment,-- 主键且自增长
    name     varchar(50)  not null unique,  -- 员工姓名,非空且唯一
    gender   char(1),
    salary   double(7, 2) not null,         -- 工资非空
    joindata date         not null,         -- 入职日期 非空
    dep_id   int,
    foreign key (dep_id) references dept (did)
);

insert into dept(dname)
values ('研发部'),
       ('市场部'),
       ('财务部'),
       ('销售部');

insert into emp(name, gender, salary, joindata, dep_id)
values ('孙悟空', '男', 7200, '2023-10-10', 1),
       ('猪八戒', '男', 3600, '2001-09-09', 2),
       ('沙僧', '男', 2700, '1900-01-01', 2),
       ('唐僧', '男', 9000, '2001-10-01', 3),
       ('白龙马', '女', 5000, '1560-10-23', 1),
       ('黑熊精', '男', 4500, '1700-09-20', 1),
       ('蜘蛛精', '女', 2300, '1760-10-19', null);

1. 连接查询

内连接(隐式)

-- 内连接 隐式内连接
-- select 字段列表 from 表1,表2 where 条件
select *
from emp,
     dept
where emp.dep_id = dept.did;

select emp.name, emp.gender, dept.dname
from emp,
     dept
where emp.dep_id = dept.did;

-- 给表 起别名
select t1.name, t1.gender, t2.dname
from emp t1,
     dept t2
where t1.dep_id = t2.did;

内连接(显式)

-- 显式内连接
-- select 字段列表 from 表1 [inner] join 表2 条件
select *
from emp
         inner join dept on emp.dep_id = dept.did;
select t1.name, t2.dname
from emp t1
         join dept t2 on t1.dep_id = t2.did;

左外连接

-- 左外连接
-- select 字段列表 from 表1 left [outer] join 表2 on 条件
-- 查询emp表中所有的数据和对应的部门信息
select *
from emp
         left join dept on emp.dep_id = dept.did;

右外连接

-- 右外连接
-- select 字段列表 from 表1 right [outer] join 表2 on 条件
-- 查询dept表中所有的数据和对应的员工信息
select *
from emp
         right join dept on emp.dep_id = dept.did;

2. 子查询

子查询:查询中嵌套查询,称嵌套查询为子查询
子查询根据查询结果不同,作用不同
单行单列
多行单列
多行多列

单行单列

-- 单行单列:作为条件值,使用 = != > <等惊醒条件判断
-- 查询工资高于猪八戒的员工信息
select *
from emp
where salary > (select salary from emp where name = '猪八戒');

多行单列

-- 多行单列:作为条件值,使用 in 等惊醒条件判断
-- 查询 财务部 和市场部所有的员工信息
select *
from emp
where dep_id in (select did from dept where dname = '市场部' or dname = '财务部');

多行多列

-- 查询日志日期是 '2001-01-01'之后的员工信息和部门信息
select *
from (select * from emp where joindata > '2001-01-01') t1,
     dept
where t1.dep_id = dept.did;

事务

  • 原子性(Atomicity):事务是不可分割的最小操作单位,要么同时成功,要么同时失败
  • 一致性(Consistency):事务完成时,必须使所有的数据都保持一致状态
  • 隔离性(Isolation):多个事务之间,操作的可见性
  • 持久性(Durability):事务一旦提交或回滚,它对数据库中的数据的改变就是永久的
-- 事务

-- 查看事务的默认提交方式
-- 1 自动提交  0 手动提交
select @@autocommit;

-- 修改事务提交方式
-- 改为手动提交以后,每次执行完sql语句以后,都要手动再执行commit
set @@autocommit = 0;


-- 开启事务
begin;
-- 李四的金额 -500
update account
set money=money - 500
where name = '李四';

# update account
# set id = 'aa'
# where id = 1;
# sdfsgfd
-- 张三的金额 +500
update account
set money=money + 500
where name = '张三';

-- 提交事务
commit;

-- 回滚事务
rollback;

drop table if exists account;
create table if not exists account
(
    id    int primary key auto_increment,
    name  varchar(10),
    money double(10, 2)
);
insert into account(name, money)
values ('张三', 1000),
       ('李四', 1000);


select *
from account;

update account
set money = 1000
where id > 0;

参考文献

黑马程序员JavaWeb基础教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ljp345775

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

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

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

打赏作者

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

抵扣说明:

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

余额充值