SQL 记录

SQL

DDL:create(新建表);alter(修改表);drop(删除表)
DML:select(查询表);insert(添加表);update(修改表)delete(删除表);
DCL:grant(授权角色);revoke(撤销权限)
TCL:commit(提交);rollback(回滚)

对表结构进行增修删操作
(1). 增加表名:alter table 表名 add 需要新增字段列名 数据类型(例:varchar2(10))
(2). 修改字段名:alter table 表名 rename column 旧字段名 to 新字段名
(3). 修改数据类型:alter table 表名 modify 字段 需要修改的数据类型
(4). 修改表名:alter table 旧表名 rename to 新表名
(5). 删除字段:alter table 表名 drop column 需要删除的字段列名

  1. 建表
    create table 表名(
    a_id number(10)
    a_name varchar2(20)
    a_age varchar2(20)
    )
    表内插入数据
    insert into 表名 (字段名1,字段名2,字段名3) values ('字段值1','字段值2','字段3')

  2. 查询:select * from 表名

  3. 修改数据
    update 表名 set 修改内容 where 条件

  4. 删除数据
    delete from 表名 where 条件

  5. 删除表
    drop table 表名

  6. 聚合函数
    平均值:avg()
    最大值:max()
    最小值:min()
    个数:count(*)
    去重: distinct()

  7. 倒序排序
    select * from 表名 where 条件 order by 字段(例数值) desc
    正序排序
    select * from 表名 where 条件 order by 字段 asc

  8. 分组:group by 语句用于结合合计函数,根据一个或多个列对结果集进行分组
    查询字段1对应的个数
    select 字段1,count(*) from 表名 where 条件 group by 字段1

  9. having分组后过滤
    where 时group by 之前进行筛选,having 是 group by 之后进行同ing及的筛选,一般having 和group by 一起使用,结合集合函数,统计之后进行筛选。
    举例:
    select 字段1 from 表名 group by 字段1 having count(name)>3
    select * from 表名 where 字段1 in (select 字段1 from 表名 group by 字段1 having count()>3) and 字段2(id) in (select min(id) from 表名 group by 字段1 having count()>3)
    使用顺序:where ... group by ... having ... order by...

  10. 多表关联
    左关联,右关联,内关联,全关联
    左关联: 表A left join 表B
    左关联以左表为主,会将左表中的数据全部查询出来,然后通过关联字段查询B表中数据,有对应数据就查询,没有就显示null
    右关联: 表A right join 表B
    右关联以右表为主,在进行关联查询时会将表B中的数据全部查出,表A中有对应数据就显示,没有则显示null
    内关联:inner join :从结果表中删除与其他被来凝结表中没有匹配行的所有行
    全关联:full join:展示两表中所有的数据。
    举例:left join 左关联
    select * from 表1 as 1 left join 表2 as 2 on 1.id =2.id

posted @ 2021-03-08 16:02  墨振  阅读(72)  评论(0编辑  收藏  举报