MySQL语法及IDEA使用MySQL大全

在项目中我们时常需要写SQL语句,或简单的使用注解直接开发,或使用XML进行动态SQL之类的相对困难的SQL,并在IDEA中操控我们的SQL,但网上大都图方便或者觉得太简单了,完全没一个涵盖两个方面的讲解。

单表:

DDL(表操作):

创表语句:

约束:


常见的数据类型:

for example(例子):

sql 复制代码
create table address_book
(
    id            bigint auto_increment comment '主键'
        primary key,
    user_id       bigint                       not null comment '用户id',
    consignee     varchar(50)                  null comment '收货人',
    sex           varchar(2)                   null comment '性别',
    phone         varchar(11)                  not null comment '手机号',
    province_code varchar(12) charset utf8mb4  null comment '省级区划编号',
    province_name varchar(32) charset utf8mb4  null comment '省级名称',
    city_code     varchar(12) charset utf8mb4  null comment '市级区划编号',
    city_name     varchar(32) charset utf8mb4  null comment '市级名称',
    district_code varchar(12) charset utf8mb4  null comment '区级区划编号',
    district_name varchar(32) charset utf8mb4  null comment '区级名称',
    detail        varchar(200) charset utf8mb4 null comment '详细地址',
    label         varchar(100) charset utf8mb4 null comment '标签',
    is_default    tinyint(1) default 0         not null comment '默认 0 否 1是'
)
    comment '地址簿' collate = utf8mb3_bin;

查询语句:

修改语句:

那么IDEA中是如何操作DDL语句的呢? 需要特别说的是,IDEA中想执行哪部分代码,就左键选中代码块变色,再点击绿色的执行按钮

DML :

insert

java 复制代码
@Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
            " VALUES" +
            " (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
XML 复制代码
<insert id="insertBacth">
       insert into dish_flavor (dish_id, name, value)
       value
       <foreach collection="flavors" item="df" separator=",">
           (#{df.dishId},#{df.name},#{df.value})
       </foreach>
    </insert>

Update

java 复制代码
 <update id="update">
# yml中配置文件加的开启驼峰命名,只是java中Employee 类的 成员变量的驼峰命名 可以对应 数据库中的 参数名
        update employee
        <set>
            <if test="name != null ">
                name = #{name},
            </if>
            <if test="username != null ">
                username = #{username},
            </if>
            <if test="password != null ">
                password = #{password},
            </if>
            <if test="phone != null">
                phone =#{phone},
            </if>
            <if test="sex != null ">
                sex = #{sex},
            </if>
            <if test="idNumber != null ">
                id_Number = #{idNumber},
            </if>
            <if test="status != null ">
                status = #{status},
            </if>
            <if test="updateTime != null">
                update_Time = #{updateTime},
            </if>
            <if test="updateUser != null ">
                update_User = #{updateUser},
            </if>
        </set>
        where id = #{id}
    </update>

项目里一般都是动态SQL编辑数据,简单的update直接使用MP,根本没必要写

Delete

java 复制代码
 @Delete("delete from dish_flavor where dish_id = #{dishid}")
    void deleteByDishId(Long dishid);
XML 复制代码
<delete id="deleteByIds">
        delete from dish_flavor where dish_id in
        <foreach collection="dishIds" item="dishId" open="(" close=")" separator=",">
            #{dishId}
        </foreach>
    </delete>

DQL:

基本查询:

注意:代码中*是通配符,即查询所有

条件查询:

聚合函数:

分组查询:

排序查询:

分页查询:

但分页查询,不同于其他的DQL查询方式,它是项目较为重要的部分,我们一般会使用PageHelper这个插件,来简化我们的代码,以下是

分页查询的三点重要的步骤:

First:

先pom.xml导入maven对应依赖

XML 复制代码
<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>

Second:

在impl中写模板式代码

java 复制代码
 public PageResult pageQuery( EmployeePageQueryDTO employeePageQueryDTO) {
//        select * from employee limit 0,10
//        开始分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
//      这个是强制要求名字叫"page",不能改,所以这需要创建对象
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);

//        long total1 = employeeMapper.pageQuery(employeePageQueryDTO).getTotal();
//        List<Employee> records1 = employeeMapper.pageQuery(employeePageQueryDTO).getResult();

        long total = page.getTotal();
        List<Employee> records = page.getResult();

        return new PageResult(total,records);
    }

Third:

XML 复制代码
<select id="pageQuery" resultType="com.sky.entity.Employee">
        select * from employee
        <where>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
        </where>
        order by create_time desc
    </select>

多表:

分为逻辑外键和物理外键,见名知意,前者并没有物理层面的约束,后者则是有着物理层面的约束

以上是一份逻辑关联的SQL语句,这应该是企业最常用的方式

但,此方法也有缺点,若你没考虑周全,可能会误操作,所以物理外键可能更利于你保持数据完整性和同一性。

IDEA修改方法:

根据名称,进行添加,修改外键

多表查询:

先简单介绍一下笛卡尔积,两个集合一个集合数据量为2,另一个数据量为4,两个相乘则为8条,这就是笛卡尔积,

而我们肯定不需要冗余数据,所以需要消除冗余项

分类:

内连接就是取并集,外连接就是取AorB,子查询则是查询蓝色or橘色部分

内连接:

外连接:

子查询:

XML 复制代码
总结代码:
<select id="pageQuery" resultType="com.sky.vo.DishVO">
        select  d.* , c.name as category_name  from dish d left outer join category c on d.category_id = c.id
        <where>
            <if test = "name!=null">
                and d.name like concat('%',#{name},'%')
            </if>
            <if test = "categoryId != null">
                and d.category_id = #{categoryId}
            </if>
            <if test = "status != null">
                and d.status = #{status}
            </if>
        </where>
        order by d.create_time desc
    </select>

事务:

注意:

● 默认MySQL的事务是自动提交的,也就是说,当执行一条DML语句, MySQL会 立即隐式的提交事务。

索引:

当数据库的表中数据量很大时,DML等SQL语句会有很长的时耗。

索引(index)是帮助数据库高效获取数据的数据结构

..

语法:

相关推荐
nongcunqq31 分钟前
abap 操作 excel
java·数据库·excel
rain bye bye1 小时前
calibre LVS 跑不起来 就将setup 的LVS Option connect下的 connect all nets by name 打开。
服务器·数据库·lvs
冻咸鱼1 小时前
MySQL的配置
mysql·配置
阿里云大数据AI技术3 小时前
云栖实录|MaxCompute全新升级:AI时代的原生数据仓库
大数据·数据库·云原生
小韩博3 小时前
IDEA的简单使用
java·ide·intellij-idea
不剪发的Tony老师3 小时前
Valentina Studio:一款跨平台的数据库管理工具
数据库·sql
weixin_307779133 小时前
在 Microsoft Azure 上部署 ClickHouse 数据仓库:托管服务与自行部署的全面指南
开发语言·数据库·数据仓库·云计算·azure
六元七角八分3 小时前
pom.xml
xml·数据库
虚行4 小时前
Mysql 数据同步中间件 对比
数据库·mysql·中间件
奥尔特星云大使4 小时前
mysql读写分离中间件Atlas安装部署及使用
数据库·mysql·中间件·读写分离·atlas