MySQL 笔记

2022-09-30  本文已影响0人  会爬虫的小蟒蛇

常用数据类型

数字类型

float(m,d) 其中m表示有效位,d表示小数位
m最大值为7 d不能大于m
double(m,d) 其中m表示有效位,d表示小数位
m最大值为15 d不能大于m
当有一个非常大的数字,且不希望精度丢失时使用
decimal(m, d), 其中m表示有效位,d表示小数位 m最大值为65

不会产生精度问题的原因是,他本质是字符串

字符类型

时间类型

常见运算符

常用函数

数字运算

字符运算

CRUD

update table_name set name1=value1 where 表达式
delete from table_name where 表达式

集合操作

交差并补.png
select name form table1 表达式
    union
select name form table2 表达式
// 子语句 select 必须拥有相同数量的列,且列的数据类型页相同
select * from
    (select name form table1 表达式) as t1
        join
    (select name form table2 表达式) as t2
        on t1.name = t2.name
select * from
    (select name form table1 表达式) as t1
        left join
    (select name form table2 表达式) as t2
        on t1.name = t2.name
where t2.name is null
select * from
    (select name form table1 表达式) as t1
        left join
    (select name form table2 表达式) as t2
        on t1.name = t2.name
where t2.name is null
    union
select * from
    (select name form table1 表达式) as t1
        left join
    (select name form table2 表达式) as t2
        on t1.name = t2.name
where t1.name is null
// A与B的补集 = A对B的差集 + B对A的差集

约束

create table table_name(
    id int primary key auto_increment,
    name varchar(255), 
)
alter table table_name add primary key (`id`);
alter table table_name drop primary key;

变量

存储过程

简单的认为是SQL中的函数

call functionName(1)
drop procedure functionName

触发器

和存储过程一样,都是嵌入到mysql中的一段程序,区别就是存储过程需要显示调用,而触发器是根据对表的相关操作自动激活执行。

create trigger 触发器名称
before insert  # 触发时机 before[after] [insert, update, delete]
on 表名
for each row
begin
    if new.department not in ("男装事业部", "女装事业部") then
        set new.department = "unknow";
    end if;
end
show triggers from 数据库名;
drop trigger 触发器名称;

索引

# 创建索引
CREATE INDEX 索引名 on 表名(字段名1, 字段名2)

# 查看索引
SHOW INDEX FROM 表名

# 删除索引
ALTER TABLE 表名 DROP INDEX 索引名

# 查看是否命中索引
EXPLAIN SELECT * FROM 表名 where 表达式
上一篇 下一篇

猜你喜欢

热点阅读