Advanced statement oracle database

- view
- the view is a query window provides all the data from the original table

- Create a table query
the Create the Table emp AS the SELECT * Scot from
the SELECT * from emp; t.emp;

- Create a view must have dba privileges]
the Create View v_emp AS the SELECT ename, the Job from emp;
- Query view
the SELECT * from v_emp;
- modify the view
Update v_emp the SET the Job = 'CLERK' the WHERE ename = 'ALLEN';
the commit ;
- create a read-only view
create view v_emp1 as select ename, job from emp with read only;

select * from v_emp1;

- view of the role of
- 1, the view can shield sensitive field
- 2, to ensure timely and unified headquarters and branch data


- Index
- the index of the concept: index builds a binary tree above the columns in the table
- the purpose of the most substantial increase in the efficiency of the query, but will affect the additions and deletions to the index of efficiency
- single-page index
- the index to create a singleton

- separate index trigger rules: conditions must be the original value of the index column
- one-way function, fuzzy queries will affect the trigger index
create index idx_ename on emp (ename) ;

- composite index
create index idx_enamejob on emp (ename, job);

- Index trigger
select * from emp where ename = ' SCOTT' and job = 'xx'; - a composite index trigger
select * from emp where ename = ' SCOTT' or job = 'xx'; - not trigger index
select * from emp where ename = 'SCOTT' ; - triggering a separate index

--pl / SQL programming language
- more efficient than ordinary procedural programming language
--pl / sql become the main programming language used to write stored procedures

- stated method
DECLARE
I Number (2): = 10;
S VARCHAR2 (10): = 'Bob';
ENA emp.ename% type; - a reference variable

emprow emp% rowtype; - record variable
begin


dbms_output.put_line(s);
dbms_output.put_line(i);
select ename into ena from emp where empno = 7788;
dbms_output.put_line(ena);

End;
IF Analyzing --pl / sql is
- less than 18 digital inputs, outputs minor
- less than 18 numbers out of 40 is greater than the output of the middle-aged
- the input is greater than 40, the output of the elderly


declare
i number(3) :=ⅈ

the begin
IF I <18 is the then
DBMS_OUTPUT.PUT_LINE ( 'minor');
ELSIF I <40 the then
DBMS_OUTPUT.PUT_LINE ( 'middle-aged');
the else

DBMS_OUTPUT.PUT_LINE ( 'elderly');
End IF;
End;
- ----------------------
--PL circulating SQL /
- output numbers 1 to 10 in three ways
--while cycle
DECLARE
I number ( 2): =. 1;
the begin
the while I <. 11 Loop
DBMS_OUTPUT.PUT_LINE (I);
I: = I +. 1;
End Loop;


end;
--exit 循环
declare
i number(2) :=1;
begin
loop
exit when i>11;
dbms_output.put_line(i);
i :=i+1;
end loop;

end;
--for循环
declare

begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;

 

- Cursor: Similar to the collection of java
- output emp table the names of all employees
DECLARE
the Cursor Cl IS the SELECT * from emp;
emprow emp% ROWTYPE;
the begin
Open Cl;
Loop
FETCH INTO emprow Cl;
Exit the when Cl% NOTFOUND;
DBMS_OUTPUT.PUT_LINE (emprow.ename);
End Loop;

Close Cl;

End;

--给指定部门员工涨工资
declare
cursor c2(eno emp.deptno%type)
is select empno from emp where deptno = eno;
en emp.empno%type;

begin
open c2(10);
loop
fetch c2 into en;
exit when c2%notfound;
update emp set sal =sal+100 where empno = en;
commit;
end loop;

close c2;
end ;

- Query No. 10 sector wages
the SELECT * from emp the WHERE deptno = 10;
-------------------------


- stored procedures
- stored procedures: advance has been compiled for a pl / sql language, placed on the database side
- can be directly invoked this period of pl / sql generally fixed steps

- up to 100 dollars a designated staff

create or replace procedure p1(eno emp.empno%type)

is

begin
update emp set sal = sal+100 where empno = eno;
commit;
end;

--- test p1

declare
begin
p1(7788);

End;
- using a stored procedure query
select * from emp where empno = 7788 ;


- Computes the annual salary of employees by storing function
Create or Replace function f_yearsal (ENO type emp.empno%) Number return
IS
S Number (10);
the begin
SELECT * SAL 12 is + NVL (COMM, 0) from S INTO EMP WHERE EMPNO ENO =;
return S;
End;

--test

declare
s number(10);
begin
s := f_yearsal(7788);
dbms_output.put_line(s);
end;


--out type parameter how to use
- using the stored procedure to count salary
Create or Replace Procedure p_yearsal (ENO% emp.empno type, yearsal OUT Number)
IS
S Number (10);
C emp.comm% type;
the begin
SELECT * SAL 12 is, NVL (COMM, 0) INTO S, C = ENO from EMP WHERE EMPNO;
yearsal: = S + C;
End;

- test p_yearsal
DECLARE
yearsal Number (10);
the begin
p_yearsal (7788, yearsal);
DBMS_OUTPUT.PUT_LINE (yearsal);
End;

What --in and out type parameter difference is?
- all related to the assignment into query or: = parameter assignment, must use out two modified
------------- 44

- the difference between stored procedures and stored functions
- Syntax difference: keyword is not the same,
-------------- storage function two more return than stored procedures
------- ------- essential difference: storage function returns a value, the stored procedure does not return a value
-------------- If you want to implement a stored procedure returns a value of the business, you must use out type parameters


- we can use the stored function returns a value of properties, from the definition of the function
- rather than using stored procedures to customize the function
------- case needs: check out the employee name, employee's department name;
---- the preparatory work, the assignment dept worksheet under the scott user to the current user under
the Create the table dept AS the SELECT * from scott.dept;
- matters that traditionally the case needs
the SELECT e.ename, d.dname
from E emp, dept d
the WHERE = d.deptno e.deptno;
- create storage function to implement a department number, a department name output
the create function or the replace the FDNA (dno dept.deptno% of the type) return dept.dname% of the type
IS
DNA dept.dname% of the type ;
the begin
SELECT DNAME INTO DNA from Dept WHERE DEPTNO = DNO;
return DNA;
End;

- testing of the storage function
SELECT e.ename, FDNA (e.deptno)
from EMP E;
--------------------


- Trigger: Specify a rule, automatically triggered when we do crud operation, no need to call
- statement trigger is not included for each row
- row-level triggers: Contains for each row is row-level trigger too
- - plus for each row in order to use: old or: new Object

- two kinds of triggers
- statement-level trigger

- Insert a record output of a new employee
the Create the Trigger T1 or the replace
the After
INSERT
ON the Person
DECLARE

the begin
dbms_output.put_line ( 'new hires');
End;

- trigger T1
INSERT INTO Person values (. 1, 'red');
the commit;

select * from person;

- Row-level triggers
- can not give employees a pay cut
the Create or the replace the Trigger T2
the before
Update
ON emp
for the each Row
DECLARE

the begin
IF: old.sal>: new.sal the then
RAISE_APPLICATION_ERROR (-20 001, 'not to pay cut employees');
End IF;
End;

- Test trigger
Update SET SAL = SAL EMP WHERE EMPNO -1 = 7788;
- Query
select * from emp where empno = 7788 ;


- Trigger implement a primary key increment
- Analysis: Before the user to insert, to get the data that will be inserted in
- to the data of the primary key assigned
the Create or the replace the Trigger the SUID
the before
INSERT ON the Person
for the each Row
DECLARE
the begin
the SELECT INTO s_person.nextval : new.pid from Dual;

End;
the SELECT * from the Person;

- use triggers to achieve the self-energizing a primary key
INSERT INTO Person (pname) values ( 'A');
the commit;

--oracle 10g ojdbc14.jar
---oracle 11g ojdbc

 

Guess you like

Origin www.cnblogs.com/purpleone/p/11701495.html