Oracle005 Oracle数据库基础知识点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010425839/article/details/52014561

切换其他用户:conn 用户名/口令

退出sqlplus:Crtl+C 或者 clear scr

解决: 默认Oracle10g的scott不能登陆,被锁定:
(1)conn sys/root as sysdba;//以DBA的身份登录
(2)alter user scott account unlock;// 然后解锁
(3)conn scott/tiger //弹出一个修改密码的对话框,修改一下密码就可以了,建议密码还用tiger

查询表结构:desc table_name

sql语句以 ; 或者 / 结束执行

--注释一行  /* ...*/注释多行

DML:update/insert/delete;DDL:create;DCL:grant

select emp.deptno,count(emp.deptno) from emp join dept using(deptno) group by emp.deptno;

常见查询语句结构:

  select avg(sal), deptno from emp where deptno is not null

  group by deptno

  having avg(sal)>2000

  order by deptno

三范式:

①要有主键,列不可分

②当有多个字段做组合主键时,其他非主键字段不能部分依赖于组合主键

举例:选课表(学生-教师 多对多 学生名称 课程 授课老师名称  主键:学号+工号 学生名称依赖部分主键学号),分为三张表

③表中不能包含其他表的非主键字段,比如员工信息表中有部门编号后,就不能再有部门名称等非主键字段。

pl/sql第四代语言:

declare声明变量

%type变量的属性,如empno number(8);  empno2 emp.empno%type; 好处:原字段empno的字段类型变了,empno2也会跟着自动变过来。

  declare

    v_num := 0;

   begin

    v_num := 2/v_num;

    dbms_output.put_line(v_num);

   exception

    when others then

        dbms_output.put_line('error');

  end;

table变量类型:type、record

  declare

    type type_table_emp_empno is table of emp.empno%type index by binary_integer;

    v_empnos type_table_emp_empno;

  begin

    v_empnos(0) := 7369;

    v_empnos(1) := 1;

    v_empnos(-1) := 8;

    dbms_output.put_line(v_empnos(-1));

  end;

declare

   type type_record_dept is record

    (

        deptno dept.deptno%type,

        dname dept.dname%type,

        loc dept.loc%type

    );

    v_temp type_record_dept;

  begin

    v_temp.deptno := 50;

    v_temp.dname := 'aaaa';

    v_loc.loc := 'bj';

    dbms_output.put_line(v_temp.deptno || '' || v_temp.dname);

  end;

用%rowtype声明record变量:v_temp dept%rowtype;

pl/sql中sql语句的使用,必须返回一条记录,用变量接收

declare

  v_ename emp.ename%type;

  v_sal emp.sal%type;

begin

  select ename, sal into v_ename, v_sal from emp where empno = 7369;

  dbms_output.put_line(v_ename || '' || v_sal);

end;

sql%rowcount 多少条记录被sql语句影响;

begin

  execute immediate 'create table T(nnn varchar2(20) default ''aaaa'')';

end;

异常处理too_many_rows/no_data_found

declare
 v_temp number(4);
begin
 select empno into v_temp from emp where deptno = 10;
exception
 when too_many_rows then
  dbms_output.put_line('返回记录太多');
 when others then
  dbms_output.put_line('error');
end;
/

declare

 cursor  c is  select * from emp;

 v_temp c%rowtype;

begin

 open c;

  loop

   fetch c into v_temp;

   exit when(c%notfound);

   dbms_output.put_line(v_temp.ename);

  end loop;

  close c;

end;

可更新游标for update 与where current of c 搭配使用:cursor c is select * from emp for update ;update emp set sal = sal * 2 where current of c;





猜你喜欢

转载自blog.csdn.net/u010425839/article/details/52014561