工作时发现自己手写SQL能力很低,特此再来学习一遍SQL

SQL语法

①常用的数据库本身的操作

sql 复制代码
# 显示数据库列表
show databases;

# 使用某个数据库
use twbpm_dev;

# 创建一个数据库
create database db_test;

# 删除一个数据库
drop database if exists db_test;

# 显示数据库中所有的表
show tables;

# 查看MySQL的版本
select version();
show variables like 'version';

②创建操作

sql 复制代码
# 创建表
create table testTable(
    id int(19) not null primary key ,
    name varchar(50) not null ,
    address varchar(100),
    phone char(11)
);
create table 新表 as select * from 旧表

# 创建索引
create index i_testIndex on testTable(name);

# 创建视图
create view v_testView as select * from testTable;

③新增操作

sql 复制代码
# 插入数据
insert into testTable value (10000001,"张三","湖南省长沙市xxxxxxxx",12345678643);
insert into testTable values (10000001,"张三","湖南省长沙市xxxxxxxx",12345678643),(10000002,"李四","广东省深圳市xxxxxxxx",12345456789);

# 查询结果当新增
insert into testTable select * from testTable01;

# 批量插入大量数据
LOAD DATA INFILE '/path/to/data.txt'     //读取文件
INTO TABLE testTable                     //选择目标表
FIELDS TERMINATED BY ','                 //指定文件中字段分隔符
ENCLOSED BY '"'                          //在""中的字符都会被当做一个列数据
LINES TERMINATED BY '\n'                 //指定行分隔符
IGNORE 1 LINES                           //忽略第1行的数据
(column_list);                           //要插入的列,可以不写默认为全部列

为什么LOAD DATA比逐行INSERT效率要高?

④更新、删除操作

sql 复制代码
# 更新
update testTable set name = 'mike' where id = '11110011';
update testTable set name = 'amy';    //如果不加条件会把所有的数据的name行更新为amy

# 删除表数据
delete from testTable;                //如果不加条件会把所有的数据都删除(索引、表结构不会变)

# 删除索引
drop index if exists i_testIndex on testTable;

# 删除表本身
drop table if exists testTable;

# 删除视图
drop view if exists vivw_name;

⑤排序操作

sql 复制代码
# 排序
select * from testTable 
order by id , phone desc;   //先按照id升序排序,如果id相同的则按照phone降序排序

⑥修改操作

sql 复制代码
# 添加一个新的列
alter table testTable
add new_column varchar(255);

# 删除一个列
alter table testTable
drop column column_name;

# 修改列数据类型
alter table testTable
alter column new_column char(11);

⑦Limit、In、Union

sql 复制代码
# limit
select * from testTable limit 3;   //一个参数的时候是返回数据的前3行
select * from testTable limit 2(pageNum),3(pageSize);//两个参数的时候是分页,pageNum页码,pageSize页大小

# in
select * from testTable where id in (value1,value2.....、也可以接子查询、列表、集合)

# union   //union的两边必须要是相同的列
SELECT column1, column2, ...FROM table1
UNION                                           //union会去重
SELECT column1, column2, ...FROM table2;
# union all
SELECT column1, column2, ...FROM table1
UNION ALL                                       //union all会保留所有的数据
SELECT column1, column2, ...FROM table2;

⑧连接操作

⑨XML文件中的标签

XML 复制代码
<!--select标签-->   作用:与mapper接口对应,并且指定返回数据类型
<select id="selectByPrimaryKey"  resultType="com.exzample.bean.User">
   select ....
</select> 

<!--if标签-->       作用:test进行判断,如果条件满足,则会把标签内的sql语句拼到最终查询语句中
<select id="selectByPrimaryKey"  resultType="com.exzample.bean.User">
   select * from product
    <if test="name!=null">
        where name like concat('%',#{name},'%')
    </if> 
</select> 

<!--where标签-->     作用:当where标签中没有一个if标签成立时,where会被去掉
<select id="listProduct" resultType="com.exzample.bean.User">
    select * from product
    <where>
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>        
        <if test="price!=null and price!=0">
            and price > #{price}
        </if>
    </where>     
</select>

<!--foreach标签-->    作用:循环遍历集合(item:单个元素 collection:集合名字 separator:分隔符 index:元素的索引)
<select id="listProduct" resultType="com.exzample.bean.User">
    select * from product
    where id in
    <foreach item="code" collection="itemCodes" separator="," open="(" close=")" index="index">
        #{item}
    </foreach>
</select>

<!--sql标签 、include标签-->    作用:定义sql片段,并且引用拼接sql片段
<sql id="sqlTerm">
 order_id,cid,address,create_date
</sql>
<select id="listProduct" resultType="com.exzample.bean.User">
    select <include refid="sqlTerm" />
    from ordertable where order_id = #{orderId}
</select>

⑩写SQL时应该注意到的点

sql 复制代码
# null不能直接与数值型进行比较
select name from Customer where referee_id != 2 or referee_id is null;
相关推荐
阿斌_bingyu70931 分钟前
Arduino开发物联网ESP32快速入门指南(包含开发语言说明、学习路径和实战教程)
开发语言·物联网·学习
NULL指向我2 小时前
TMS320F28P550SJ9学习笔记14:EPWM_死区dead_baund
笔记·学习
未来的资深Java架构师2 小时前
MySQL报Lock wait timeout exceeded; try restarting transaction
数据库·mysql
傍晚冰川2 小时前
【STM32】解读启动文件startup_stm32f10x_md.s
linux·笔记·stm32·单片机·学习
java奋斗者2 小时前
新闻推荐系统(springboot+vue+mysql)含万字文档+运行说明文档
spring boot·后端·mysql
孙同学_2 小时前
【MySQL】002.MySQL数据库基础
数据库·mysql
码农101号3 小时前
数据库视图讲解(view)
数据库·sql
zhuyixiangyyds3 小时前
day26图像处理OpenCV
笔记·opencv·学习
虾球xz3 小时前
游戏引擎学习第197天
java·学习·游戏引擎
Acxymy3 小时前
SQL学习笔记七
笔记·sql·学习