常见Mysql数据库操作语句

-- DDL创建数据库结构
-- 查询所有数据库
show databases ;
-- 修改数据库字符集
alter database db02 charset utf8mb4;
-- 创建字符编码为utf——8的数据库
create database db05 DEFAULT CHARACTER SET utf8;


-- 创建表格
create table tb_user(
  id int auto_increment primary key comment 'ID,唯一标识',
  username varchar(20) not null unique comment '用户名',
  name varchar(20) not null  comment '姓名',
  age int comment '年龄',
  gender varchar(20)  default '男' comment '性别'
)comment '用户表';

-- 创建表格2
create table tb_emp
(
    id          int auto_increment comment 'ID主键'
        primary key,
    create_time datetime                     not null comment '创建时间',
    update_time datetime                     not null comment '更新时间',
    username    varchar(20)                  not null comment '用户名',
    password    varchar(32) default '123456' null comment '密码',
    name        varchar(10)                  not null comment '员工姓名',
    gender      tinyint unsigned             not null comment '1男 2女',
    image       varchar(300)                 null comment '图像',
    job         tinyint unsigned             null comment '1班主任 2讲师 3学工主管 4教研主管',
    entrydate   date                         null comment '入职日期',
    constraint tb_emp_username_uindex
        unique (username)
)
    comment '员工表';

-- 查询表结构
desc tb_emp;

-- 删除表
drop table tb_emp;

-- DML数据操作语言
insert into tb_emp(id, create_time, update_time, username, password, name, gender, image, job, entrydate) values
(null,now(),now(),'zhouzhiruo3','123','周芷若',2,'1.jpg',2,'2023-12-28');

insert into tb_emp values (null,now(),now(),'zhangwuji2','123','张无忌',1,'2.jpg',1,'2023-12-28');

insert into tb_emp (id, create_time, update_time, username, name, gender) values (null,now(),now(),'weiyixiao1','韦一笑',2);

insert into tb_emp (create_time, update_time,username, name, gender) values (now(),now(),'weiyixiao3','韦一笑',2);

update tb_emp set name='张三',update_time=now() where id=1;

update tb_emp set entrydate='2024-1-1',update_time=now() ;

delete from tb_emp where id=10;

-- DQL数据查询语言
select id, create_time, update_time, username, password, name, gender, image, job, entrydate from tb_emp ;
-- 别名
select username as '用户名' ,job  '职位'  ,update_time 更新时间 from tb_emp;
-- 去重
select distinct job  from tb_emp;
-- 条件查询
select * from tb_emp where entrydate between '2023-12-28' and '2023-12-29';

select * from tb_emp where name like '张_';

select * from tb_emp where job in (1,3);

select * from tb_emp where image is not null ;
-- 聚合函数
select count('7') from tb_emp;

-- 分组查询
select gender,count(*) from tb_emp group by gender having count(*)>3;

select job,count(*) from tb_emp where entrydate<'2024-01-01'group by job having job=2;

select * from tb_emp where username='zhouzhiruo3';

-- 排序查询
select * from tb_emp order by entrydate desc ,job;

-- 分页查询
select * from tb_emp limit 5;
select * from tb_emp limit 0,5;

select * from tb_emp limit 5,5;

-- MYSQL控制函数 if 和case

select if(gender=1,'男性','女性') 性别,count(*) 数量 from tb_emp group by gender;

select (case job when 1 then '班主任' when 2 then '学生' when 3 then '助教' else '其他' end) 职位 ,count(*)from tb_emp group by job;
select (case job when 1 then '班主任' when 2 then '学生' when 3 then '助教' else '其他' end) 职位 from tb_emp;


-- 案例创建category分类表
-- 多表查询

select * from tb_emp,tb_dept;
select * from tb_emp;

-- 隐式内连接
select * from tb_emp,tb_dept where tb_emp.dept_id=tb_dept.id;

select tb_emp.name,tb_dept.deptname from tb_emp,tb_dept where tb_emp.dept_id=tb_dept.id;

-- 显式内连接
select tb_emp.name,tb_dept.deptname from tb_emp inner join tb_dept on tb_emp.dept_id = tb_dept.id;
select e.name,d.deptname from tb_emp e join tb_dept d on e.dept_id = d.id;
-- 左外链接

select e.name,d.deptname from tb_emp e left join tb_dept d on e.dept_id = d.id

-- 右外链接
select e.name,d.deptname from tb_emp e right join tb_dept d on e.dept_id = d.id;

-- 标量子查询
-- 查询教研部门下的员工
select id from tb_dept where tb_dept.deptname='教研部门';

select * from tb_emp where tb_emp.dept_id = 2;

select * from tb_emp e where e.dept_id = (select id from tb_dept d where d.deptname='教研部门');

-- 查看房东白后入职的员工信息
select entrydate from tb_emp e where e.name='方东白';
select entrydate from tb_emp e where entrydate >'2021-01-04';

select entrydate from tb_emp e where entrydate >(select entrydate from tb_emp e where e.name='方东白');
-- 列子查询
-- 查询教研部门和后勤部门的员工信息

select id from tb_dept where deptname in('教研部门','后勤部门');

select * from tb_emp where tb_emp.dept_id in(2,3);

select * from tb_emp where tb_emp.dept_id in(select id from tb_dept where deptname in('教研部门','后勤部门'));

-- 行子查询
-- 查询和风不吹相同入职时间和职位的员工信息。
select entrydate,job from tb_emp where name ='风不吹';

select * from tb_emp where entrydate = '2025-01-04' and job  = 7 ;

select * from tb_emp where (entrydate,job) = (select entrydate,job from tb_emp where name ='风不吹');

-- 表子查询
-- 查询2024-01-04号后的员工信息,及部门名称
select * from tb_emp where entrydate >'2024-01-04' ;

select e.*,d.deptname from (select * from tb_emp where entrydate >'2024-01-04') e,tb_dept d where e.dept_id= d.id;

-- 分类表
create table category
(
    id          int unsigned auto_increment comment '主键ID'
        primary key,
    name        varchar(20)                not null comment '分类名称',
    type        tinyint unsigned           not null comment '分类类型:1菜品分类 2套餐分类',
    sort        tinyint unsigned           not null comment '排序字段',
    status      tinyint unsigned default 0 not null comment '状态:0停售 1启售',
    create_time datetime                   not null comment '创建时间',
    update_time datetime                   not null comment '更新时间',
    constraint category_name_uindex
        unique (name)
)
    comment '分类表';
-- 菜品表
create table dish
(
    id           int unsigned auto_increment comment '主键id'
        primary key,
    name         varchar(20)                not null comment '菜品名称',
    category_id  int unsigned               not null comment '菜品分类',
    price        decimal(8, 2)              not null comment '价格',
    image        varchar(300)               not null comment '图片',
    describetion varchar(200)               null comment '描述',
    status       tinyint unsigned default 0 not null comment '状态 0停售 1起售',
    create_time  datetime                   not null comment '创建时间',
    update_time  datetime                   not null comment '修改时间',
    constraint dish_name_uindex
        unique (name)
)
    comment '菜品表';

-- 套餐表
create table setmeal
(
    id          int unsigned auto_increment comment '主键id'
        primary key,
    name        varchar(20)                not null comment '套餐名称',
    category_id int unsigned               not null comment '套餐分类id',
    price       decimal(8, 2)              not null comment '价格',
    image       varchar(300)               not null comment '图片',
    description varchar(200)               null comment '描述信息',
    status      tinyint unsigned default 0 not null comment '状态:0停售 1启售',
    create_time datetime                   not null comment '创建时间',
    update_time datetime                   not null comment '修改时间',
    constraint setmeal_name_uindex
        unique (name)
)
    comment '套餐表';
-- 套餐菜品表
create table setmeal_dish
(
    id         int unsigned auto_increment comment '主键ID'
        primary key,
    setmeal_id int unsigned     not null comment '套餐ID',
    dish_id    int unsigned     not null comment '菜品ID',
    copies     tinyint unsigned not null comment '菜品的份数'
)
    comment '套餐菜品关系表';

-- 查询价格低于10元的,菜品名称价格和及菜品的分类名称

select d.name,d.price,c.name from category c,dish d where c.id=d.category_id and d.price<10  ;
-- 查询所有价格在10-50(包含)之间的菜品且状态为‘起售’的名称,价格,及分类名称(即使菜品没有分类,也要查询出来)

select d.name,d.price,c.name , d.status from dish d left join category c on d.category_id = c.id  where  d.status=1  and  d.price between 10 and 50  ;
select d.name,d.price,c.name from dish d left join category c on d.category_id = c.id  where  d.price between 10 and 50 and d.status=1 ;

-- 查询每个分类下,最贵的菜品,展示出分类的名称,最贵的菜品价格;
select c.name,d.name,max(d.price) from dish d ,category c where d.category_id=c.id group by c.id ;

-- 查询每个分类下,菜品状态为‘起售’,并且该分类下菜品的数量大于等于2的分类名称

select c.name,count(*) from category c,dish d where c.id = d.category_id and d.status=1 group by c.name having count(*)>=2;


-- 查询经典川菜中包含那些菜品,套餐的名称价格,菜品的名称价格和份数。

select d.name,s.name,s.price,d.name,sd.copies from dish d,setmeal s,setmeal_dish sd where s.id=sd.setmeal_id and d.id=sd.dish_id and s.name='经典川菜';

-- 查询低于菜品平均价格的菜品信息,菜品名称,菜品价格
select avg(price)
from dish;

select name,price from dish where price<(select avg(price) from dish);

-- 事务
-- 开启事务
start transaction ;
delete  from tb_dept where id=1;
delete  from tb_emp where dept_id=1;
-- 提交事务
commit;
-- 回滚事务
rollback;

select * from tb_emp;
select * from tb_dept;

-- 创建索引
create index idx_emp_name on tb_emp(name);

-- 查看索引
show index  from tb_emp;

-- 删除索引
drop index idx_emp_name on tb_emp;

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/297863.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【docker】数据卷和数据卷容器

目录 一、如何管理docker容器中的数据&#xff1f; 二、数据卷 1、数据卷原理 2、数据卷的作用 3、数据卷案例 三、数据卷容器 1、数据卷容器作用 2、数据卷容器案例 四、容器互连&#xff08;centos镜像&#xff09; 一、如何管理docker容器中的数据&#xff1f; 二、…

Apple M2 Pro芯片 + docker-compose up + mysql、elasticsearch pull失败问题的解法

背景 &#xff08;1&#xff09;从github上git clone了一个基于Spring Boot的Java项目&#xff0c;查看readme&#xff0c;发现要在项目的根目录下&#xff0c;执行“docker-compose up”。&#xff08;2&#xff09;执行“docker-compose up”的前提是&#xff0c;在macos上要…

编译原理笔记(三)

一、词法分析程序的设计 1、词法分析程序的输出 在识别出下一个单词同时验证其词法正确性之后&#xff0c;词法分析程序将结果以单词符号的形式发送至语法分析程序以回应其请求。 单词符号一般分下列5类&#xff1a; 关键字&#xff1a;如&#xff1a;begin、end、if、whil…

Spring Boot学习随笔- 集成MyBatis-Plus(二)条件查询QueryWrapper、聚合函数的使用、Lambda条件查询

学习视频&#xff1a;【编程不良人】Mybatis-Plus整合SpringBoot实战教程,提高的你开发效率,后端人员必备! 查询方法详解 普通查询 // 根据主键id去查询单个结果的。 Test public void selectById() {User user userMapper.selectById(1739970502337392641L);System.out.print…

opencv007 图像运算——加减乘除

今天学习图像处理的基础——加减乘除&#xff0c;总体来说比较好理解&#xff0c;不过生成的图片千奇百怪哈哈哈哈 opencv中图像的运算本质是矩阵的运算 加法 做加法之前要求两张图片形状&#xff0c;长宽&#xff0c;通道数完全一致 cv2.add(img1, img2) add的规则是两个图…

MySQL之视图内连接、外连接、子查询

一、视图 1.1 含义 虚拟表&#xff0c;和普通表一样使用 视图&#xff08;view&#xff09;是一个虚拟表&#xff0c;其内容由查询定义。同真实的表一样&#xff0c;视图包含一系列带有名称的列和行数据。但是&#xff0c;数据库中只存放了视图的定义&#xff0c;而并没有存放…

Visio导出eps格式图片

Visio导出eps格式图片 文章目录 Visio导出eps格式图片1. Visio中使用Adobe Acrobat虚拟打印2. Adobe Acrobat中裁剪并另存为eps格式 如何使用Visio绘图然后导出.eps格式的图片呢&#xff1f;这个过程需要用到Adobe Acrobat&#xff0c;使用Adobe Acrobat的虚拟打印功能&#xf…

JVM知识总结(简单且高效)

1. JVM内存与本地内存 JVM内存&#xff1a;受虚拟机内存大小的参数控制&#xff0c;当大小超过参数设置的大小时会报OOM。本地内存&#xff1a;本地内存不受虚拟机内存参数的限制&#xff0c;只受物理内存容量的限制&#xff1b;虽然不受参数的限制&#xff0c;如果所占内存超过…

【Java集合篇】负载因子和容量的关系

负载因子和容量有什么关系 ✔️典型解析✔️loadfactor为啥默认是0.75F&#xff0c;不是1呢?✔️为什么HashMap的默认负载因子设置成0.75✔️0.75的数学依据是什么✔️0.75的必然因素 ✔️HashMap的初始值设为多少合适? ✔️典型解析 HashMap 中有几个属性&#xff0c;如 cap…

商智C店H5性能优化实战

前言 商智C店&#xff0c;是依托移动低码能力搭建的一个应用&#xff0c;产品面向B端商家。随着应用体量持续增大&#xff0c;考虑产品定位及用户体验&#xff0c;我们针对性能较差页面做了一次优化&#xff0c;并取得了不错的效果&#xff0c;用户体验值&#xff08;UEI&…

每日一题——LeetCode1089.复写0

方法一 splice&#xff1a; 通过数组的slice方法&#xff0c;碰到 0就在后面加一个0&#xff0c;最后截取原数组的长度&#xff0c;舍弃后面部分。 但这样做是违反了题目的要求&#xff0c;不要在超过该数组长度的位置写入元素。 var duplicateZeros function(arr) {var le…

docker 完成MySQL的主从复制

文章目录 搭建步骤 搭建步骤 拉取镜像 docker pull mysql:5.7运行主从 docker run -p 3307:3306 --name mysql-master -v /mydata/mysql-master/log:/var/log/mysql -v /mydata/mysql-master/data:/var/lib/mysql -v /mydata/mysql-master/conf:/etc/mysql -e MYSQL_ROOT_P…

Springboot进行多环境配置的2种方式

本文来说下Springboot使用Spring Profile和Maven Profile进行多环境配置 文章目录 概述Spring Profile多环境主配置文件与不同环境的配置文件 Maven ProfileProfile配置资源过滤 Spring Profile与Maven Profile具体使用 概述 原因 在实际的项目上&#xff0c;一般会分三种环境d…

淘宝商品详情API接口(item_get-获得淘宝商品详情)主图,属性,sku,价格,搜索商品列表

淘宝开放平台提供了API接口&#xff0c;允许开发者获取淘宝商品的相关信息。为了获取商品详情&#xff0c;您可以使用 item_get API接口。以下是如何使用此API接口来获取商品的主图、属性、SKU、价格以及搜索商品列表的简要说明&#xff1a; 公共参数 名称类型必须描述keyStr…

如何利用MiniTab的命令行来提高数据建模效率

使用MiniTab进行数据建模时&#xff0c;如果涉及到需要多次更改数据、多次查看模型&#xff0c;感兴趣的同学可以尝试一下&#xff0c;把命令行显示出来&#xff0c;通过命令行的形式来执行&#xff0c;避免在繁多的菜单中到处查找。 操作方式如下图&#xff1a; 点击菜单“查…

Transformer架构和对照代码详解

1、英文架构图 下面图中展示了Transformer的英文架构&#xff0c;英文架构中的模块名称和具体代码一一对应&#xff0c;方便大家对照代码、理解和使用。 2、编码器 2.1 编码器介绍 从宏观⻆度来看&#xff0c;Transformer的编码器是由多个相同的层叠加⽽ 成的&#xff0c;每个…

Java重修第三天—“方法“的案例练习

案例一&#xff1a;买飞机票 题目 用户购买机票时&#xff0c;机票原价会按照淡季、旺季&#xff0c;头等舱还是经济舱的情况进行相应的优惠&#xff0c;优惠方案如下:5-10月为旺季&#xff0c;头等舱9折&#xff0c;经济舱8.5折。11月到来年4月为淡季&#xff0c;头等舱7折&…

内核线程创建-kthread_create

文章参考Linux内核线程kernel thread详解 - 知乎 大概意思就是早期创建内核线程&#xff0c;是交由内核处理&#xff0c;由内核自己完成&#xff08;感觉好像也不太对呢&#xff09;&#xff0c;创建一个内核线程比较麻烦&#xff0c;会导致内核阻塞。因此就诞生了工作队列以及…

美格智能5G RedCap模组SRM813Q通过广东联通5G创新实验室测试认证

近日&#xff0c;美格智能5G RedCap轻量化模组SRM813Q正式通过广东联通5G创新实验室端到端的测试验收&#xff0c;获颁测评证书。美格智能已连续通过业内两家权威实验室的测试认证&#xff0c;充分验证SRM813Q系列模组已经具备了成熟的商用能力&#xff0c;将为智慧工业、安防监…

docker - 常用容器部署命令大全(MySQL、Redis、RabbitMQ、ES、Kibana、Nacos、Sentinel)

目录 一、常用容器运行指令 MySQL Redis RabbitMQ ElasticSearch & kibana Nacos Sentinel 一、常用容器运行指令 MySQL docker run -d --name mysql -p 3306:3306 -e TZAsia/Shanghai -e MYSQL_ROOT_PASSWORD1111 mysql:5.7 -e TZAsia/Shanghai&#xff1a;指定…
最新文章