• sql的一点总结<一>


    sql总结

    1.常见的数据库对象有哪些?
    表(table) 视图(view) 序列(sequence) 索引(index) 同义词(synonym)
    存储过程(procedure) 存储函数(function) 触发器(trigger)

    2.表:数据的主要存储方式,由行和列组成
    视图:存储起来的select语句。
    对视图中数据的DML操作,会导致创建视图使用的表中的数据的修改。
    create view emp_vu
    as
    select department_id,avg(salary) dept_avg_sal
    from employees
    group by department_id;
    --with read only
    select * from emp_vu;
    序列:提供了一系列有规律的数值,常用来作为表的主键的值
    create sequence emp_id_seq
    start with 1001
    increment by 1
    maxvalue 10000
    --minvalue
    --cycle/nocycle
    --cache/nocache
    1)nextval / currval
    select emp_id_seq.currval from dual;

    select emp_id_seq.nextval from dual;

    create table emp(
    id number(10),
    name varchar2(15)
    )

    insert into emp
    values(emp_id_seq.nextval,'BB');

    select * from emp;

    裂缝:①多个表共用一个序列②出现回滚③出现异常

    索引(index):当使用索引作用的列作为查询条件进行查询时,可以提高查询的效率。
    --如何创建索引:①自动创建(声明为主键或唯一性约束的列) ②手动创建
    create index emp_sal
    on employees(salary);

    3.重点:表
    DDL:CREATE TABLE;ALTER TABLE;TRUNCATE TABLE;DROP TABLE;RENAME .. TO ..
    不可回滚,即意味着:自动提交
    --1.创建表
    --1.1“白手起家”
    create table dept(
    dept_id number(10),
    dept_name varchar2(15),
    location_id varchar2(10),
    birth Date
    )

    select * from dept;

    --1.2基于现有的表,创建
    create table emp1
    as
    select employee_id id,last_name name,hire_date,salary
    from employees
    --where department_id = 80;
    where 1=2;

    select * from emp1;
    --1)对现有的表的复制/空表
    create table emp_copy
    as
    select * from employees
    --where 1=2;

    select * from emp_copy;

    --2.修改表
    --2.1增加一个列
    ALTER table emp
    add(salary number(10,2) default 2000);

    select * from emp;
    --2.2修改现有的列
    alter table emp
    modify(salary number(15,2));

    insert into emp(id,name)
    values(1004,'CC');

    --2.3重命名现有的列
    alter table emp
    rename column salary to sal;

    --2.4删除现有的列
    alter table emp
    drop column sal;

    --3.重命名现有的表
    rename emp to employee;

    select * from employee;

    --4.清空表
    truncate table employee;

    rollback;

    --5.删除表
    drop table employee;

     

    DML:增、删、改、查
    --增insert into ...
    --1.一条一条的添加
    select * from dept;

    insert into dept
    values(,,,);

    insert into dept(dept_id,location_id,dept_name)
    values(,,);
    --2.导入数据
    insert into dept(dept_id,location_id,dept_name)
    select department_id,location_id,department_name
    from departments;

    alter table dept
    modify(dept_name varchar2(20));

    --删
    delete from dept
    where dept_id < 40;

    --改
    update dept
    set location_id = 1700
    where dept_id = 20;

    commit;

    --查询(重中之重)
    select .... --分组函数(count / max / min / avg / sum)
    from ...,....--多表连接
    where ...--过滤条件和 多表的连接条件(若不写,会出现笛卡尔积的错误)
    group by ...--凡是在select中出新了分组函数,则没有使用分组函数的列要作为group by的条件
    having avg(..) ...--分组函数作为过滤条件,要使用having
    order by ... asc/desc;

    --存储起来的pl/sql语句:
    declare
    --声明变量、记录类型、游标

    begin
    --执行部分

    exception
    --异常处理部分
    end;
    --存储过程(procedure) :没有返回值
    create or replace procedure ... (param1 param1_type,param2 param2_type)
    is

    begin

    end;


    --存储函数(function) :有返回值
    create or replace function ...(param1 param1_type,param2 out param2_type)
    return 变量类型
    is

    begin
    ...
    return ...;
    end;

    --触发器(trigger)
    create or replace trigger ...
    after/before
    update/delete/.. on 表名
    begin

    end;

     

    <!-- Start -->

    获知及时信息,请关注我的个人微信订阅号:0与1的那点事

     

    <!-- End -->

     

    本文为博主原创文章,转载请注明出处!

    http://www.cnblogs.com/libingbin/

    感谢您的阅读。

  • 相关阅读:
    CSS3旋转动画
    CSS3的动画属性
    CSS选择器
    JS事件委托
    js 轮播图效果
    JS事件冒泡和事件捕获
    JS自定义播放器
    js闭包for循环只执行最后一个值得解决方法
    交通红绿灯
    汉明距
  • 原文地址:https://www.cnblogs.com/libingbin/p/6391243.html
Copyright © 2020-2023  润新知