数据库思维导图

数据 类型

字符串

        varchar

        char

数值

        int

        float

日期

        date

        datetime

增删改查

增:

insert into 表名( 字段 , 字段 , ... ) values( 数据1 , 数据2 , ... )

删:

drop table 表名

改:

update 表名 set 需要修改的字段=需要修改的数值 where 条件字段=条件

查:

基本查询:

select * from 表名

where条件查询:

单一条件:

select 字段 , 字段 , ... from 表名 where 条件字段 = 条件

多个条件:

select 字段 , 字段 , ... from 表名 where 条件字段1 = 条件1 and 条件字段2 = 条件 2

注解:and 条件必须全部满足
           or 条件满足其一即可

排序查询:

select * from 表名 where 条件字段 = 条件 order by 排序字段 排序方式

注解:排序方式:
           asc/desc 排序的方式默认为升序(asc)
           升序可以不写关键字asc,降序(desc)必须填写order by 子句必须放在整个SQL的最后

分组查询:

selec 分组字段 from 表名 group by 分组字段

去重查询:

全部去重:

select distinct * from table

单一条件去重:

select distinct 字段1 , 字段2 , ... from 表名

表关联查询

两表关联:

内连接 inner join

select * from 表1 备注1 inner join 表2 备注2 on 备注1.关联字段 = 备注2.关联字段;

左连接 left join

select * from 表1 备注1 left join 表2 备注2 on 备注1.关联字段 = 备注2.关联字段;

右连接 right join

select * from 表1 备注1 right join 表2 备注2 on 备注1.关联字段 = 备注2.关联字段;

全连接 
    左连接 union 右连接

select * from 表名 where gender='1'
union
select * from 表名 where gender='0'

解释:
union    结构相同,写法相同的两个结果集链接在一起

三表关联:

表一关联表二,在关联表三

select * from 表1 备注1
left join 表2 备注2 on  表1.关联字段 = 表2.关联字段
left join 表3 备注3 on  表1.关联字段 = 表3.关联字段

子查询

单个值

select* from 表1 where id = (select id from  表2 where 字段 =  ‘数值’)

多个值
    作为范围

select* from 表1 where id in ((select id from  表2 where 字段 =  ‘数值1’) , ...)


    作为中间结果

 select* from 表1 where id = (select id from  表2 where 字段 =  ‘数值1’) and (select id from  表2 where 字段 =  ‘数值2’)

视图

直接创建

create view 视图名 as select * from 表名

查看

describe 视图名

索引 

普通索引

alter table 表名 add index 索引名 ( 索引字段 )  using btree

复合索引

alter table 表名 add index 索引名 ( 索引字段1 , 索引字段2 , ... )

事务

开启

setautocommit=0
start transaction

提交

commit

回滚    

rollback

例子

#开启事务
setautocommit=0
start transaction
#编写一组事务的语句
update account set balance=500 where username='张无忌';
update account set balance=1500 where username='赵敏';
#结束事务
commit;
select * from account;

触发器

插入语句触发

delimiter 自定义结束符号
create trigger 触发器名字 触发时间 触发事件 on 表 for each row
begin
    -- 触发器内容主体,每行用分号结尾
end
自定义的结束符合

delimiter ;

存储过程

# 创建存储过程

CREATE PROCEDURE 名称()

BEGIN

语句

END$$

# 调用存储过程

CALL 名称()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值