zoukankan      html  css  js  c++  java
  • oracle 基础笔记


    sqlplus / as sysbda;-- 运行命令
    conn scott/tiger [as sysdba];-- 登录
    show user;查看当前用户
    alter user scott account lock;-- 锁定用户
    alter user scott account unlock;-- 解锁

    create tablespace nn_test datafile 'd:database ntest.dbf' size 10M;-- 创建表空间,指定路径和大小
    alter tablespace nn_test detafile 'd:database ntest.dbf' resize 20m;-- 修改表空间

    -- 删除表空间
    drop tablespace nn_test;-- 逻辑删除
    drop tablespace nn_test including contents;--逻辑删除表空间和段
    drop tablespace nn_test including contents and datafiles;--物理删除表空间和数据文件


    --创建表空间
    create tablespace stuPace datafile 'd:databasezxx.dbf' size 10M;
    --创建用户 用户名zxx 密码 指定表空间和临时表空间
    create user teacher identified by "123456" default tablespace stuPace temporary tablespace temp;
    drop user teacher;-- 删除用户
    -- oracle数据库
    -- 1,屋里存储:数据文件:数据存放后期产生的数据;控制文件:才能放数据库本身相关的数据;日志文件:操作过程中的日志记录。
    --2,逻辑存储结构:按照层次进行管理
    数据库-->表空间-->逻辑对象-->数据段-->数据区间(最小存储单元)-->数据块(逻辑管理的最基本的单元)
    一个表空间可以有多个数据文件,一个数据文件只属于一个表空间

    -- 系统权限
    三个常用的角色:DBA 拥有操作数据库的所有权限;connect 数据库的连接权限;resource 应用程序的开发角色权限。
    -- 对象权限
    对表的操作权限(基本操作)
    select,update,delete,insert,references(表外键关联权限操作),flashback(回滚到某个点),debug,query,rewrite,commit,refresh 等

    grant connect,resource to teacher;-- 赋权限
    grant select on scott.emp to teacher;--赋对象权限
    grant all privileges on scott.dept to teacher;-- 表的全部权限
    revoke insert on scott.emp from teacher;-- 权限回收
    revoke all privileges on scott.emp from teacher;
    -- DDL create table 表名,alter table 表名,truncate table 表名,drop table 表名。
    -- DML select insert update delete
    -- DCL grant / revoke

    -- 创建表
    create table studentinfo111 (
    stuid number(3) not null,
    stuname varchar2(10) not null,
    stusex varchar2(3) check(stusex='男'or stusex='女') not null,
    stubirthday date
    );

    -- 使用序列自增长stuid
    create sequence stu_id
    start with 1
    increment by 2
    maxvalue 5
    minvalue 0
    cycle
    cache 2;

    -- 插入数据
    insert into studentinfo111 values(stu_id.nextval,'小白','女',sysdate);
    insert into studentinfo111 values(stu_id.nextval,'小白','女',sysdate);
    insert into studentinfo111 values(stu_id.nextval,'小白','女',sysdate);
    insert into studentinfo111 values(stu_id.nextval,'小白','男',sysdate);
    insert into studentinfo111 values(stu_id.nextval,'小白','女',sysdate);
    commit;
    select * from studentinfo111;

    select * from scott.emp;

    select sal*12 年薪 from scott.emp;

    select * from scott.emp;
    select * from scott.dept;

    -- 部门为 SALES RESEARCH 的员工列出
    select * from scott.emp where deptno in
    (select deptno from scott.dept where dname='SALES' or dname='RESEARCH')

    -- 上级是 BLAKE 的员工
    select * from scott.emp where mgr in
    (select empno from scott.emp where ename='BLAKE');


    -- 同义词synonym
    create [or replace] [public] synonym stu for studentinfo111;--建同义词即是给表studentinfo111取个别名

    create synonym stu for studentinfo111;
    select * from stu;--默认是私有的
    drop public synonym stu;

    --视图操作view
    create [or replace] view v_sut
    as
    select * from sif;

    create view view_stu
    as select stuid,stuname from studentinfo111;
    select * from view_stu;

    drop view view_stu;
    -- 给表建立索引:索引是表中数据和相应存储位置的列表,index
    索引是一种树状结构,从逻辑设计和屋里设计实现两方面进行分类,
    逻辑设计:分为单索引列、组合索引、唯一索引、非唯一性索引和基于函数的索引等。
    物理实现:分区索引、非分区索引、B树索引、正向索引、反向索引、位图索引等
    create unique index i_stuid on studentinfo111(stuid);--创建唯一性索引,不能是null,不能重复,
    create bitmap index i_stusex on studentinfo111(stusex);--创建位索引

    alter index i_stuid rebuid;-- 重建索引
    alter index i_stuid coalesce;-- 合并索引
    drop index i_stusex;

    --序列:sequence
    create sequence sql_sid2
    start with 1
    increment by 1
    minvalue 0
    maxvalue 5
    cycle -- 入股序列达到最大值,会重最小值开始写 (默认 nocycle)
    cache 3 -- 缓存量,默认20,(默认 nocache)


    create table scroinfo(
    sid number(5) not null,
    score number(5,2) not null
    );

    -- 使用序列添加数据
    select * from scroinfo;
    -- 事务处理
    事务用于确保数据库数据的一致性,事务处理和锁是两个紧密联系的概念。
    oracle中的事务默认开启,需数据持久化则手动提交,提交后事务就结束;
    --事务的ACID属性
    原子性 atomicity:要么全部进行,要么全部撤销
    一致性 consistency :事务处理要将数据库从一种状态转变为另一种状态
    隔离性 isolation :事务处理提交之前,事务处理的效果不能由系统中其他事务处理看到
    持久性 durability :表示一旦提交了事务处理,它就永远生效。
    insert into scroinfo values(sql_sid2.nextval,782);
    insert into scroinfo values(sql_sid2.nextval,781);
    insert into scroinfo values(sql_sid2.nextval,782);
    savepoint poiOne;-- 设置保存点
    insert into scroinfo values(sql_sid2.nextval,783);
    insert into scroinfo values(sql_sid2.nextval,784);
    insert into scroinfo values(sql_sid2.nextval,785);
    savepoint poiTwo;

    select * from scroinfo;

    rollback to poiOne;--回滚到poiOne点,如果中间提交了事务则不能回滚
    select * from scroinfo;
    commit;
    rollback;-- 取消全部事务

    -- 修改序列递增值,不能修改序列的start with
    alter sequence sql_sid2
    increment by 2;

    drop sequence sql_sid2; -- 删除

    --常用函数
    select lower('Java35') 小写 from dual;
    select upper('Java35') 大写 from dual;

    select ltrim(' Java35 ') 左空格 from dual;
    select rtrim(' Java35 ') 右空格 from dual;
    select trim(' Java35 ') 空格 from dual;
    select length('Java35') 长度 from dual;
    select replace('小白小小白小白小小白小白小小白小白小小白','小','黑') 替换 from dual;

    -- 数学函数
    select abs(-589) 函数 from dual;
    ceil()向上取整 floor()向下取整 round()四舍五入 trunc(n,[m])截取数字
    select 'trunc:' || trunc(1234.423) from dual;
    select 'trunc:' || trunc(1234.423,2) from dual;
    select 'trunc:' || trunc(1234.423,-2) from dual;

    -- 函数转换

    select to_number('3.1415') 函数 from dual;

    select to_char(sysdate,'yyyy-MM-dd HH:mi:ss') 函数 from dual;

    select to_date('10:25:52','hh:mi:ss') 函数 from dual;

    select nvl(expr1,expr2) 函数 from dual; -- 如果expr1是null,则返回expr2.两者可以是任意类型,但两者的数据类型必须要匹配。
    -- 如果expr1不是null,则返回expr2;如果expr1是null,则返回expr3;
    select nvl2(expr1,expr2,expr3) 函数 from dual;

    -- 执行过程插入表中数据
    create or replace procedure pro_insertEmp(empno number,ename varchar2,sal number)
    is
    begin
    insert into scott.emp(empno,ename,sal) values(empno,ename,sal);
    end;

    -- 调用有参存储过程
    declare
    empno number;
    ename varchar2(10);
    sal number;
    begin
    pro_insertEmp(2003,'dhuisdu',5200);
    end;

    begin
    pro_insertEmp(empno => 2004,ename =>'小白',sal=>2344);
    end;


    -- 存储过程输出系统当前时间
    create or replace procedure pro_sysdate
    as
    begin
    dbms_output.put_line(to_char(sysdate,'yyyy-MM-dd hh:mi:ss'));
    end;

    -- 调用
    begin
    pro_sysdate;
    end;
    -- 输出参数的存储过程

    create or replace procedure pro_add(value1 number,value2 out number)
    is
    begin
    value2:=value1+20;
    end;

    -- 调用并输出
    declare
    re number;
    begin
    pro_add(10,re);
    dbms_output.put_line(re);
    end;

    declare
    re number;
    value1 number;
    begin
    value1:=25;-- 赋值
    pro_add(value1,re);
    dbms_output.put_line(re);
    end;

    -- 函数

    create or replace function fun_selectEmp(namev varchar2)
    return number -- 返回类型
    as
    gz number; --返回变量
    begin
    select sal into gz from scott.emp where ename=namev;
    return gz;--返回值
    end;
    -- 调用函数,输出返回值
    declare
    namev varchar2(30):='SMITH';--参数变量
    returnv number;--返回变量
    begin
    -- namev:='SMITH';--赋参数值
    returnv:=fun_selectEmp(namev);
    dbms_output.put_line('smith的工资为:'|| returnv);
    end;


    -- 命名规范
    v_变量名 c_常量名 pro_过程名 fun_函数名 view_视图名 i_索引名 seq_序列名
    declare
    v_name varchar2(20);
    c_tax constant number(5,2);
    v_sal number(6,3):=233.232;
    flag boolean not null default true;-- 定义布尔变量必须制定默认值

    -- oracle 分三块
    declare
    /*定义部分*/
    begin
    /*执行部分*/
    exception /*异常处理块*/
    end;

    declare
    v_name scott.emp.ename%type;
    v_sal scott.emp.sal%type;
    begin
    select ename,sal into v_name,v_sal from scott.emp where empno=&输入员工编号;
    dbms_output.put_line('姓名:'||v_name);
    dbms_output.put_line('工资:'||v_sal);
    exception
    when no_data_found then
    dbms_output.put_line('输入的员工编号不正确');
    end;

    drop procedure pro_sal;
    -- 条件语句
    create or replace procedure pro_empsal(v_name varchar2)
    as
    v_sal number;
    begin
    select sal into v_sal from scott.emp where ename=v_name;
    if v_sal<2000 then
    update scott.emp set sal=v_sal+500 where ename=v_name;
    commit;
    end if;
    exception
    when no_data_found then
    dbms_output.put_line('该用户不存在');
    end;
    --
    declare
    v_name varchar2(20):='ALLEN';
    begin
    pro_empsal(v_name);
    end;

    select * from scott.emp;
    --
    create or replace procedure pro_comm(v_name varchar2)
    as
    v_comm number;
    begin
    select comm into v_comm from scott.emp where ename=v_name;
    if v_comm >0 then
    update scott.emp set comm=v_comm+100 where ename=v_name;
    commit;
    else
    update scott.emp set comm=v_comm+500 where ename=v_name;
    commit;
    end if;
    exception
    when no_data_found then
    dbms_output.put_line('无此用户');
    end;
    --
    declare
    v_name varchar2(20):='ALLEN';
    begin
    pro_comm(v_name);
    end;
    -- case条件语句
    create or replace procedure pro_jobcomm(v_id number)
    as
    v_job varchar2(30);
    begin
    select job into v_job from scott.emp where empno=v_id;
    case v_job
    when 'SALESMAN' then
    update scott.emp set comm=nvl(comm,0)+2 where empno=v_id;
    commit;
    when 'MANAGER' then
    update scott.emp set comm=nvl(comm,0)+3 where empno=v_id;
    commit;
    when 'CLERK' then
    update scott.emp set comm=nvl(comm,0)+4 where empno=v_id;
    commit;
    else
    update scott.emp set comm=nvl(comm,0)+5 where empno=v_id;
    commit;
    end case;
    exception
    when no_data_found
    then dbms_output.put_line('查无此人');
    end;
    --
    begin
    pro_jobcomm(7902);
    end;

    -- 循环语句
    create table teacher_info(
    tea_id number,
    tea_name varchar2(20)
    );
    select * from teacher_info;
    -- loop循环
    declare
    i int:=1;
    begin
    loop
    insert into teacher_info values(i,'小白'||i);
    commit;
    i:=i+1;
    exit when i>10;
    end loop;
    end;
    -- while 循环
    declare
    i int:=11;
    begin
    while i<20
    loop
    insert into teacher_info values(i,'小白'||i);
    commit;
    i:=i+1;
    end loop;
    end;
    -- for 循环
    declare
    i int :=1;
    begin
    for i in 1..10 loop -- 1到10之间,每次自动增加1
    dbms_output.put_line(i);
    end loop;
    end;
    -- 游标
    -- 使用游标提取部门no 为 20 的全部员工姓名个工资
    declare cursor emp_cursor(id number)
    is
    select ename,sal from scott.emp where deptno=id;
    v_name scott.emp.ename%type;
    v_sal scott.emp.sal%type;
    begin
    if not emp_cursor%isopen then
    open emp_cursor(20);
    end if;
    loop
    fetch emp_cursor into v_name,v_sal;
    exit when emp_cursor%notfound;--退出
    dbms_output.put_line('第'||emp_cursor%rowcount||'个员工.'||'姓名:'||v_name ||',工资:'||v_sal);
    end loop;
    close emp_cursor;
    end;

    select * from scott.emp;

    -- 使用游标更新和删除,游标的for循环简化了处理,会隐士打开和关闭游标
    declare cursor update_cursor
    is
    select sal from scott.emp for update;-- 必须加上for update
    begin
    for emp_record in update_cursor loop
    if emp_record.sal <1000 then
    update scott.emp set sal=sal+500 where current of update_cursor;
    elsif emp_record.sal<=2000 and emp_record.sal>1000 then
    delete from scott.emp where current of update_cursor;
    end if;
    end loop;
    commit; -- 统一提交
    end;

    select * from scott.emp;

    --分页查询

    select * from ( select rownum rw,e.* from scott.emp e order by e.empno desc ) where rw>0 and rw<6 ;

    --按照工资排序后,取出第二页的条数。每页5 条 ROWNUM为序列值1,2,3,。。。
    SELECT *
    FROM (SELECT A.*, ROWNUM RN
    FROM (SELECT *
    FROM scott.emp
    ORDER BY sal DESC) A
    WHERE ROWNUM <= 5)
    WHERE RN >= 1;

    -- 数据优化方案:
    查询时,尽量少使用*,
    多表查询时,尽量使用连接查询(勿用笛卡尔积)
    数据量比较大时,考虑添加索引字段,
    对于更新类操作比较频繁情况,多使用commit提交事务,释放事务所占资源,
    查询中多用 exists 替换 in。

    -- 连接oracle 数据库
    驱动driver: oracle.jdbc.driver.OracleDriver
    连接url: jdbc:oracle:thin:@localhost:1521:orcl
    用户名: scott
    密码: tigger

  • 相关阅读:
    day15
    day13
    day12
    使用modelsim直接仿真IP(FIFO)
    整理:FPGA选型
    Quartus 中调用modelsim的流程及*.vt或*.vht自动生成
    quartus ip核破解
    LDO ,开关电源DC-DC的优缺点
    硬件工程师面试经历2015---笔试篇
    出色的硬件工程师必备的几项“硬”实力
  • 原文地址:https://www.cnblogs.com/nn369/p/7905466.html
Copyright © 2011-2022 走看看