MySQL 表增删查改重点

1. 数据库约束

约束类型说明示例
NULL约束使用NOT NULL指定列不为空name varchar(20) not null,
UNIQUE唯一约束指定列为唯一的、不重复的name varchar(20) unique,
DEFAULT默认值约束指定列为空时的默认值age int default 20,
主键约束NOT NULL 和 UNIQUE 的结合id int primary key,
外键约束关联其他表的主键唯一键foreign key (字段名) references 主表(列)
CHECK约束(了解)保证列中的值符合指定的件check (sex =‘男’ or sex=‘女’)

2. 标的关系

  • 一对一
  • 一对多
  • 多对多: 一般需要创建中间表来映射两张表的关系

3. 新增

insert into [表名] [] from [];

4. 查询

  1. 聚合函数:MAX、MIN、AVG、COUNT、SUM
  2. 分组查询:GROUP BY… HAVING …
  3. 内连接:
select ... from1,表2 where 条件
-- inner可以缺省
select ... from1 join 表2 on 条件 where 其他条件
  1. 外连接:
select ... from1 left/right join 表2 on 条件 where 其他条件
  1. 自连接:
select ... from1,表1 where 条件
select ... from1 join 表1 on 条件
  1. 子查询:
-- 单行子查询
select ... from1 where 字段1 = (select ... from ...);
-- [NOT] IN
select ... from1 where 字段1 in (select ... from ...);
-- [NOT] EXISTS
select ... from1 where exists (select ... from ... where 条件);
-- 临时表:form子句中的子查询
select ... from 表1, (select ... from ...) as tmp where 条件
  1. 合并查询:
-- UNION:去除重复数据
select ... from ... where 条件
union
select ... from ... where 条件
-- UNION ALL:不去重
select ... from ... where 条件
union all
select ... from ... where 条件
-- 使用UNIONUNION ALL时,前后查询的结果集中,字段需要一致

注意:
SQL查询中各个关键字的执行先后顺序: from > on> join > where > group by > with > having >select >distinct > order by > limit

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值