1, multi-line function] [aggregate functions: acting on a plurality of lines, a return value
select count (1) from "table"; ---- the total number of queries (1 * and action are the same)
select sum (sal) from "table"; - the sum of wages
select max (sal) from "table"; - maximum wage
select min (sal) from "table"; --- minimum wage
select avg (sal) from "table"; - the average wage
2, grouping query
// all conditions can not be used aliases
--- grouping query, after the group by the original column to appear in the back of select; does not appear in the group by, an aggregate function must have wanted to appear
// check out the average wage sector (sal)
. Select e foreign key, avg (e.sal) from "table" e group by sal; --e table alias
// check out sector wages (sal) "2000's
select e foreign key, avg (e.sal) from "table" e having avg (e.sal)> 2000;. --e table alias
// query each sector wages higher than the average wage of employees of 800 and then check out the sector wages (sal) "2000's
. Select e foreign key, avg (e.sal) from "table" e where e.sal> 800 group by e foreign key having avg (e.sal)> 800;. --E table alias
3, a multi-table Cartesian product
select * from emp e,dept d;
3.1, the equivalent connection (equivalent to the effect of the connection)
Select * from emp e,dept d where e.deptno=d.deptno;
3.2, check out the employee information in all sectors and departments outside the [Connect]
select * from
emp e right join dept d
on e.deprno=d.deptno;
3.3, Oracle dedicated external connection
// equivalent to 3.2
select * from emp e,depe d
where e.deptno(+)=d.deptno;
3.4, query employees and their leader's name (from the connection used
Since the connection: we are standing at different angles to a table as multiple tables)
select e1.ename,e2.ename
from emp e1,emp e2
where e1.mgr=e2.empno;
4, sub-queries
4.1 sub-query returns a value
// query wages and Scott same employee information
select * from emp where sal=(select sal from emp where ename="scott");
4.2 sub-query returns a collection
// Query the employees and any employee number 10 the same information department
select * from emp where sal in (
select sal from emp where deptno=10);
4.3 sub-query returns a table
// query the minimum wage, the minimum wage and the names, and each department where the department name
- query each sector minimum wage
select deptno,min(sal) from dept group by deptno
- three tables linked to the investigation, to obtain the final result
--- masl virtual field aliases
select t.deptno,t.msal,e,name,d.dname
from(select deptno,min(sal) masl
from dept group by deptno) t,emp e,dept d
where t.deprno=e.deptno
and t.msal=e.sal
and e.deptno=d.deptno;
5, Oracle pagination
--rownum line number: When do select queries
- did not check his data, in the line plus a line number
- The line number starting with 1, in ascending order, can not go dancing
*** sort operation will affect rownum order (best to sort nested query)
select rownum,t.* from (
select roenum,e.* from emp e order by e.sal desc)t;
5.1, emp table wages flashback arrangement, 5Records data, queries the second page
--rownum can not write more than one positive number
select * from (
select rownum alias rn, e. * from (
select * from emp order by sal desc) e
where rownum<11)
where rn>5
--- The second order is not supported by the wording wording
6, the view (provided the query window, all the data from the original table)
// create a query table
create table "表名" as select * from scoot."表名";
// Create a view ename, job is the field name
create view "View Name" as select ename, job from "table";
// Query View
select * from "View name"
// Modify the view (not recommended)
update "View Name" set job = 'ss' where ename = 'sd' from "table";
commit;
// create a read-only view
create view "视图名" as select ename,job from "表名" with read only;
8.1 action, the view
1. shield sensitive field
2. To ensure a timely and unified part of the total data segment
9, the index
- The concept: binary tree is constructed in the column of the table
- to improve query efficiency, but will affect the efficiency of CRUD
9.1, a separate index
// create a separate index
create index "from the name of the" on "the table name (table fields)";
// separate index trigger rules, conditions must be the original value of the index column (one-way function, fuzzy inquiry will affect the index trigger)
select * from emp where ename="scott";
9.2, composite index
// Create composite index
create index "from the name of the" on "the table name (table fields, table fields)";
10.pl/sql programming language (language process-oriented)
- mainly used to write stored procedures and stored functions, etc.
--emp.ename% type acquiring a database field types
- assignment can be used: = can also use the query into an assignment
- Method Statement
declare
i number(2) :=10;
s varchar (10): = 'Hsiao Ming';
ena emp.ename% type; - a reference variable
emprow emp% rowtype; - record variable
begin
- processing business logic
// output
dbms_output.put_line(i);
dbms_output.put_line(s);
select field name into ena from table where field name = "value";
dbms_output.put_line (ena); - easy to see the success of the console output
select * into emprow from table where field name = "value";
dbms_output.put_line (emprow.ename || 'work for' || emprow.job);
end;