常用的Oracle資料庫物件的使用
阿新 • • 發佈:2019-01-08
常用的Oracle的資料庫物件介紹:表、檢視、序列、索引、同義詞、約束、儲存過程、儲存函式、包和包體、觸發器
檢視 view
create or replace view empinfoview as select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname from emp e,dept d where e.deptno=d.deptno; with read only;
ORA-01031: 許可權不足 grant create view to scott 授權建立檢視
desc empinfoview 名稱 是否為空? 型別 ----------------------------------------- -------- ---------------------------- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) SAL NUMBER(7,2) ANNSAL NUMBER DNAME VARCHAR2(14) select * from empinfoview; EMPNO ENAME SAL ANNSAL DNAME ------ ---------- ---------- ---------- -------------- 7369 SMITH 800 9600 RESEARCH 7499 ALLEN 1600 19200 SALES 7521 WARD 1250 15000 SALES
序列 sequence
create sequence myseq; create table testseq (tid number,tname varchar2(20)); select myseq.nextval from dual; // 序列的下個一值 NEXTVAL ---------- 1 select myseq.currval from dual; // 序列的當前值 CURRVAL ---------- 1 insert into testseq values(myseq.nextval,'aaa'); insert into testseq values(myseq.nextval,'aaa'); insert into testseq values(myseq.nextval,'aaa'); insert into testseq values(myseq.nextval,'aaa'); select * from testseq; TID TNAME ---------- -------------------- 2 aaa 3 aaa 4 aaa 5 aaa
索引 index
SQL的執行計劃
explain plan for select * from emp where deptno=10;
select * from table(dbms_xplan.display);
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 261 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 3 | 261 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
建立目錄(索引)
create index myindex on emp(deptno);
explain plan for select * from emp where deptno=10;
select * from table(dbms_xplan.display);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 261 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 3 | 261 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | MYINDEX | 3 | | 1 (0)| 00:00:01 |
同義詞(別名) synonym
show user
USER 為 "SCOTT"
select count(*) from hr.employees;
為hr.employees起別名 ---> 同義詞
create synonym hremp for hr.employees;
select count(*) from hremp;
COUNT(*)
----------
107
約束 constraint
create table student(
sid number constraint student_pk primary key,
sname varchar2(20) constraint student_name_notnull not null,
gender varchar2(2) constraint student_gender check (gender in ('男','女')),
email varchar2(40) constraint student_email_unique unique
constraint student_email_notnull not null,
deptno number constraint student_fk references dept(deptno) on delete set null
);
check 檢查
create table test3(
tid number,
tname varchar2(20),
gender varchar2(2) check (gender in ('男','女')),
sal number check (sal > 0)
);
儲存過程 procedure
--列印Hello World
--建立儲存過程
create or replace procedure sayhelloworld
as
--說明部分
begin
dbms_output.put_line('Hello World');
end;
--呼叫儲存過程:
1、exec sayhelloworld();
2、begin
sayhelloworld();
sayhelloworld();
end;
/
--給指定的員工漲100,並且列印漲前和漲後的薪水
create or replace procedure raiseSalary(eno in number)
is
--定義變數儲存漲前的薪水
psal emp.sal%type;
begin
--得到漲前的薪水
select sal into psal from emp where empno=eno;
--漲100
update emp set sal=sal+100 where empno=eno;
--這裡不用commit 誰呼叫誰提交
dbms_output.put_line('漲前:'||psal||' 漲後:'||(psal+100));
end raiseSalary;
/
--查詢某個員工的姓名 薪水和職位
/*
1、查詢某個員工的所有資訊 ---> out引數太多
2、查詢某個部門中的所有員工資訊 ----> 返回的是集合
*/
create or replace procedure queryEmpInformation(eno in number,
pename out varchar2,
psal out number,
pjob out varchar2)
is
begin
select ename,sal,job into pename,psal,pjob from emp where empno=eno;
end queryEmpInformation;
儲存函式 function
--查詢某個員工的年收入
create or replace function queryEmpIncome(eno in number)
return number
is
--定義變數儲存月薪和獎金
psal emp.sal%type;
pcomm emp.comm%type;
begin
--得到月薪和獎金
select sal,comm into psal,pcomm from emp where empno=eno;
--返回年收入
return psal*12+nvl(pcomm,0);
end queryEmpIncome;
包和包體 package and package body
--2、查詢某個部門中的所有員工資訊 ----> 返回的是集合
create or replace package mypackage is
type empcursor is ref cursor;
procedure queryEmpList(dno in number,empList out empcursor);
end mypackage;
create or replace package body mypackage is
procedure queryEmpList(dno in number,empList out empcursor)
as
begin
open empList for select * from emp where deptno=dno;
end;
end mypackage;
觸發器 trigger
每當成功插入新員工後,自動列印“成功插入新員工”
create trigger firsttrigger
after insert
on emp
declare
begin
dbms_output.put_line('成功插入新員工');
end;
/
強制審計
標準審計(配置)
基於值的審計
細粒度審計
管理員審計
/*
實施複雜的安全性檢查
禁止在非工作時間 插入新員工
1、週末: to_char(sysdate,'day') in ('星期六','星期日')
2、上班前 下班後:to_number(to_char(sysdate,'hh24')) not between 9 and 17
*/
create or replace trigger securityemp
before insert
on emp
begin
if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
to_number(to_char(sysdate,'hh24')) not between 9 and 17 then
--禁止insert
raise_application_error(-20001,'禁止在非工作時間插入新員工');
end if;
end securityemp;
/
/*
資料的確認
漲後的薪水不能少於漲前的薪水
*/
create or replace trigger checksalary
before update
on emp
for each row
begin
--if 漲後的薪水 < 漲前的薪水 then
if :new.sal < :old.sal then
raise_application_error(-20002,'漲後的薪水不能少於漲前的薪水。漲前:'||:old.sal||' 漲後:'||:new.sal);
end if;
end checksalary;
/
轉載自:https://blog.csdn.net/weixin_38008316/article/details/80543453