数据库学习内容复习

1.创建数据库
create database 数据库名称
删除数据库
drop database 数据库名称

2.创建表
create table 表名
(
列名 类型(长度) 自增长 主键 非空,
)
自增长:auto_increment
主键:primary key
非空:not null
外键:foreign key 列名 references 表名(列名)

删除表
drop table 表名

3.CRUD操作

insert into 表名 values(值)                                           创建
insert into 表名(列名) values(值)

delete from 表名 where 条件                                             删除

update 表名 set 列名=值 where 条件                         修改列名

select * from 表名
select 列名 from 表名                                            查询列名
select * from 表名 where 条件                  根据条件查
select * from 表名 where 条件1 or 条件2            根据多条件查询   或用or  并列用  and
select * from 表名 where 列名 like '%值%'                      模糊字查询
select * from 表名 where 列名 between A and B                           范围查询
select * from 表名 where 列名 in(值)                    离散查询  不在里面用not  in
select * from 表名 limit n,m                        分页查询  跳过m条取n条
select * from 表名 order by 列名 desc                                                 排序查询  降序 desc  默认升序
select * from 表名 group by 列名 having 条件                                        分组查询

聚合函数
select count(*) from 表名                                                       数量  数据的条数
select sum(列名) from 表名                                                       数据的和
select avg(列名) from 表名                                                      数据平均值
select max(列名) from 表名                                                       数据最大值
select min(列名) from 表名                                                            数据最小值
select distinct 列名 from 表名                                                     去重复

高级查询:
1.连接查询
select * from 表1,表2 where 连接条件

另一种写法
select * from 表1 join 表2 on 连接条件

2.联合查询
select 列名 from 表1
union
select 列名 from 表2

3.子查询
无关子查询
子查询和父查询没有关系,子查询可以单独执行
select * from 表 where 列=(select 列 from 表)
相关子查询
子查询和父查询存在互相的关系,子查询需要用到父查询的内容

原文地址:https://www.cnblogs.com/sq45711478/p/5983662.html