ORACLE

1. select employee_id, department_id
from employees e cross join departments d;
交叉连接
e表和d表交叉连接,出现笛卡尔积错误。

2. select employee_id, department_id
from employees e natural join departments;
自然连接
e表中与d表中的相同属性会出现多对值连接。

3. select employee_id, department_id
from employees e join departments d
using(department_id);
99式连接,使用using()来控制相同列连接。缺点:两个表的列名和参数类型必须一样;

4. select employee_id,d.department_id
from employees e join department_id
on e.department_id = d.department_id;
join...on...形式实现查询

5. select employee_id, department_id, city
from employees e join departments
on e.department_id = d.department_id
join locations l
on d.location_id = l.loaction_id;
使用join...on...实现多表查询。

6. select employee_id, e.department_id, department_name
from employees e left(right,full) outer join departments d
on e.department_id = d.department_id;
左外连接(右外连接,满外连接)

select employee_id, d.department_id
from employees e, departments d
where e.department_id(+) = d.department_id --右外连接
where e.department_id = d.department_id(+) --左外连接

7. select employee_id, e.department_id, department_name
from employees e,departments d
where e.department_id = d.department_id(+);
左外连接

8. 查询出 last_name 为 ‘Chen’ 的员工的 manager 的信息
select *
from employees e1, employees e2
where e1.manager_id = employee_id;

9. stdDev() 求某列的方差。

10. sum(), avg() 对非数字类型不适用。

11. min(), max(), count() 对任何类型都适用,count()不计算为null的值。

12. group by 列名  按列来划分

13. select department_id, job_id, avg(salary)
    from employees
    group by department_id, job_id;(group by 选项必须包含select选项,除去avg(),否则出错;但是相反却可以。)
    
14. 不能在 WHERE 子句中使用组函数
    可以使用having关键字来代替where
    
15. decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值);

16. 使用case 表达式 when 条件 then 结果;

    SELECT last_name, job_id, salary,
       CASE job_id WHEN 'IT_PROG'  THEN  1.10*salary
                   WHEN 'ST_CLERK' THEN  1.15*salary
                   WHEN 'SA_REP'   THEN  1.20*salary
                   ELSE      salary END     "REVISED_SALARY"
    FROM   employees;

17. 使用decode实现条件判断赋值;
    SELECT last_name, job_id, salary,
       DECODE(job_id, 'IT_PROG',  1.10*salary,
                      'ST_CLERK', 1.15*salary,
                      'SA_REP',   1.20*salary,
                        salary) REVISED_SALARY
    FROM   employees;

18. months_between() 返回两个日期的月差
    add_months() 向指定日期加上月数
    next_day() 返回下个对应的星期的时间
    last_day() 返回这个月最后一天的时间
    round() 四舍五入
    trunc() 截断

19. DML数据操纵语言(如增删改查)
    DDL数据定义语言(如创建删除表、索引)
    DCL数据控制语言(如设置权限)
    
20. create table emp(
        id number(10),
        number varchar2(20),
        salary number(10,2) 表示salary一共10位,小数位为2位,整数位为8位。
    );
    
21. create table emp2 (使用子查询创建表)
    as
    select employee_id id, salary, last_name name
    from employees; 从现有的表中选取属性来直接定义新表,而且新表还会自动录入旧表的信息。
    可通过过滤条件来筛选数据。
    
22. alter table 修改表
    alter table emp2
    add (email varchar2(10)),插入一列
    modify(salary varchar2(10))修改表中列的属性
    modify(salary number(20,2) default 2000)设置默认值为 2000
    
23. alter table emp2
    modify (salary varchar2(10))如果被修改的列中已存在数据,则会提示被修改的列数据必须为空。

24. alter table emp1
    drop column email;
    删除表emp1中列名为email的列;
    
25. alter table emp1
    rename column salary to sal;    重命名emp1表的salary列的名字为sal;

26. rollback;回滚 增删改;

27. drop table emp1;
    删除表emp1;
    
28. truncate table emp3;
    清空表emp3;不可回滚;
    
29. rename emp2 to employees2;重命名表emp2为employees2;

30. 大部分DDL命令执行后直接写入磁盘,无法回滚。

31. 插入数据
    insert into table_name(列名1, 列名2...) values(值1, 值2...);
    
31. 从其他表中拷贝数据,要求插入数据类型一致。
    insert into table_name(列名1, 列名2...)
    select 列名1, 列名2... from table_name2;
    
32. 创建脚本插入数据,使用&标识符,若值为char类型,则需要放在''中;
    insert into table_name(列名1, 列名2...) values(&值1, &值2);
    
33. 删除操作
    delete from emp
    where 条件;

34. 更新操作
    update table_name
    set 列名 = 值
    where 条件;
    
35. 约束 constraint;
    not null;
    unique;对null值无效。
    primary key;主键。
    foreign key references 表名(列名);外键。
    check;
    
36. create talbe emp(
    --列级约束
    id number(10) constraint emp_id_nk unique,
    email varchar2(20),
    salary number(10,2),
    --表级约束
    contraint emp_email_uk unique(email)
)

37. 列级约束时,foreign key 的表达方式为
    id number(10) constraint fk references 表名(列名)。
    
38. on delete cascade; 级联删除:当父表中的列被删除时,子表中相对的列也被删除。整个行删除。
    on delete set null; (级联置空): 子表中相应的列置空。
    no action; 指当删除主表中被引用列的数据时,如果子表的引用列中包含该值,则禁止该操作执行。
    
39. 设置约束not null;
    alter table emp
    modify(department_id number(10) not null);
    
40. 删除约束
    alter table emp
    drop constraint 约束名;

41. 添加约束
    alter table emp
    add constraint 约束名 约束类型(列名);
    
42. 有效化约束和无效化约束

    无效化约束
    alter table emp
    disable constraint 约束名;
    
    有效花约束
    alter table emp
    enable constraint 约束名;
    
43. 查询约束
    select constraint_name, constraint_type, search_condition
    from user_constraints
    where table_name = '表名';
    
44. 查询表中定义了约束的列
    select constraint_name, column_name
    from user_cons_columns
    where table_name = '表名';
    
45. 创建视图
    create view empview
    as
    select employee_id, last_name, salary
    from employees;
    视图和基表相关联。
    
46. create or replace view empview
    as
    select * from employees
    where department_id = 100;
    若原来存在empview视图,使用create or replace来替换原来的视图,
    原来的视图就不存在了。
    
47. 屏蔽视图DML操作
    添加 with read only;
    
48. 复杂视图列名如果基表没有,则需要命名一个列名。

49. rownum伪列;

50. 序列
    create sequence sequence
        increment by 10
        start with 10
        maxvalue 100
        min 10
        cycle/nocycle
        nocache/cache
        
51. 查询序列的当前值或下个值
    select sequence(序列名).currval from dual;查询当前值
    select sequence(序列名).nextval from dual;查询下一个值
    
52. 索引 加快查询速度,一般使用在数据量大,经常被访问且不经常更新的列上。
    创建索引:
    create index myindex
    on employees(employee_id);
    
53. 同义词synonym
    创建同义词
    create synonym e for employees;
    
54. 用户的创建
    通过oracle控制台中的安全性下的用户创建。
    
55. 角色 配置一些系统权限的集合。

56. 概要文件 配置系统执行的参数、口令方面的管理。

57. 默认新创建的用户只有连接数据库的权限,若需要其他权限需要管理员为其配置权限。

58. 通过命令行创建用户
    create user user_name
    identified by password;
    
59. 授予用户权限 grant 权限名 to user_name;
    例如: grant create table to enche;
    
60. 授予用户表空间并指定大小
    alter user enche quota 5m(表空间大小) on users(表空间大小);
    
61. 修改用户密码
    alter user user_name
    identified by new_password;

62. 创建角色
    create role role_name;
    
63. 授予角色权限
    grant 权限名 to role_name;
    
64. 授予用户角色
    grant role_name to user_name;
    
65. 数据库对象
    表、视图、序列、同义词...
    
66. 将数据库对象的权限分配给用户(执行授权的角色必须拥有该表的权限)
    grant select(权限名)
    on scott.employees(表名)
    to enche(用户名);
    
66. with grant option 可以将该权限授予他人。
    grant select
    on scott.employees
    to enche
    with grant option; enche用户可以将查询scott用户的权限授予他人,并且自己也有查询的权限。

67. to public 将权限设置成公共的
    grant select
    on scott.employees
    to public; 授予所有用户查询的权限
    
68. 撤回权限revoke
    revoke select
    on employees
    from enche; 将查询employees表的权限从enche用户撤除。
    
69. set操作符
    union/union all 并集/并集(不去重)
    intersect 交集
    minus 差集
    
70. 使用set操作符时,列数、数据类型必须一致,且必须上下一致。
    
71. union操作,若两个集合的各自内部有相同的值,则会自动去重后再进行union操作。
    union all操作,则不会各自去除各自内部的相同的值。
    intersect操作结果会去重
    minus操作结果会去重

72. set操作默认以第一个集合的列名为默认的列名,默认以asc方式排序。

73. set操作上集合的列不一致时,可以用to_char(),to_number(),to_Date()等来补充空余的列或转换数据类型

74. 按某个列来排序
    select 'I' 'MyDream',1
    from dual
    union
    select 'Am',2
    from dual
    union
    select 'Student',3
    from dual
    group by 2 desc;
    按照第二列的降序进行排列。
    
75. 设置不打印的序列名
    column test noprint;
    使用时将列的别名设置成noprint即可不打印该列。
    
76. 多列子查询
    select *
    from employees
    where (department_id,manager_id) in (select department_id,manager_id
                                            from employees
                                            where employee_id in(140,150));
    查询与工号140或150相同管理人和部门的信息    

77. 多列子查询分为成对和不成对
    成对:查询结果以一行的形式作为结果单位
    不成对:查询结果以单列的形式作为结果单位
    
78. from子句
    select * from (select * from employees where employee_id>50)
    where employee_id < 70
    查询在employee_id<50的情况下,小于70的employee信息
    
79. exists/not exists
    查询的departments不在employees表中的department_id
    select department_id
    from departments
    where not exists(
        select department_id
        from employees e
        where e.department_id = d.department_id
    )
    
80. with子句 将查询的结果先存放起来
    查询比Abel工资高的员工employee_id和salary
    with abel_sal as
    (select salary from employees where last_name = 'Abel')
    
    select employee_id,salary
    from employees
    where salary > (select salary from abel_sal);
    
81. ql/sql 有三部分组成declare ... begin ... exception.... end

82. 设置serveroutput
    set serveroutput on/off
    
83. 若程序没有异常,需要把exception部分删去。

84. pl/sql使用--为注释符

85. 打印语句
    dnms_output.put_line();
    
86. 将查询结果赋值给变量
    select ... into ... from ...;
    
87. %type 动态获取变量类型;
    定义变量与某个类型一样
    v_salary employees.salary%type;

88. record记录类型 --类似java的类;
    定义一个record类型
    type type_name is record(..., ..., ...);
    注意:括号中以逗号隔开,最后不需要符号。
    
89. 条件判断
    方式一: if...then ... elsif... then ... else ... end if;
    方式二: case ... when ... then ... end;
    
90. 循环结构
    方式一: loop... exit when ... end loop;
    方式二: while ... loop ... end loop;
    方式三: for i in ... loop ... end loop;

91. 游标的使用(类似java中的Iterator);

92. 赋值
    变量 := 值;
    
93. pl/sql执行部分的sql语句使用 = 号,不是 :=
    declare
        v_id employees.employee_id%type;
    begin
        v_id := 123;
        update table employees
        set salary = salary +100
        where employee_id = v_id;
    end;
    
94. %rowtype;
    返回数据库表的类型。

95. case... when... then ... 中when后面只能是准确的值,不能是表达式。
    多个when...then...直接不需要用分号隔开,且then后面不能打印语句也不能直接在then后面执行赋值。
    case语句会把对应的then后的值返回,可在case开头使用一个变量来指向这个值。
    
96. 循环结构loop
    1) loop
            eixt when ...;
            循环体
            迭代条件
       end loop;
    
    2) for的使用(变量自动加一)
        for 变量 in 下限 .. 上限 loop
        循环体
       end loop;
       
97. 定义游标
    cursor 名字 is 查询语句;
    cursor emp_sal_cursor is select salary from employees where department_id = 80;

98. 游标的使用
    open 游标名; 打开游标
    fetch 游标名 into 变量名; 将游标内容提取到变量·

99. for在游标中的使用
    for c in name_cursor loop
        dbms_output.put_line(c.salary);  --c的属性和定义游标时所要查询的属性一样
    end loop;
    
100. 带参数的游标
    cursor emp_sal_cursor(dept_id number, sal number) is
        select salary+1000 sal, department_id dept
        from employees
        where department_id > dept_id and salary >sal;
    使用方法
    for c in emp_sal_cursor(dept_id=> 80, sal =>2000) loop
    ...
    end loop;
    
101. sql%notfound; 返回sql语句是否顺利执行。

102. Exception 异常
    预定义异常
    exception
        when exception_name then ...;
        
    非预定义异常
    declare
        e_deleteid_exception exception;
        pragme exception_init(e_deleteid_exception, 错误代号);
        
    begin
        ...
    
    exception
        when e_deleteid_exception then ...;
        
    end;
    
    自定义异常
    declare
        e_too_high_sal exception;
    
    begin
        raise e_too_high_sal; --使用raise抛出异常
        
    exception
        when e_too_high_sal then...;
        
103. 有返回值的称为存储函数,没有返回值的称为存储过程;

104. 存储函数的定义
    create or replace function func_name(参数)
    return ... --返回值类型
    is
        ... --变量声明
    begin
        ...
    exception
        ...
    end;
    
105. 函数的调用
    1) 在pl/sql中调用
        直接只用函数名
        例:begin
            dbms_output.put_line(function_name);
            end;
    2) 在命令行中使用
        select function_name from dual;
        
106. 实现返回多个参数
        in 参数不可以被修改
        out 参数在函数执行完后将最终的值赋给传入的参数
        in out 参数在函数执行中可以继续改变,不用等到执行完毕

107. 触发器组成:
        触发事件    何种情况下触发trigger
        触发时间    在事件发生前还是事件发生后
        触发器本身    
        触发频率    语句级(statement),行级(row)
        
    create or replace trigger my_trigger
    select on employees
    for each
    begin
        ...
    end;
    
    每个触发器最多有12个触发器 3*2*2=12
    insert、select、update  
    before、after
    for each或行控制
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值