(2) Oracle ------ one-way function and multi-table queries

1. Introduction SQL function

2, one-way function

3, multi-table queries

Introduces SQL function

1. What is SQL function?

Here Insert Picture Description

2 Both SQL functions:

One-way function and the multi-line function

One-way function

Here Insert Picture Description

1, the control function capitalization

Here Insert Picture Description
---- INITCAP (ename) first letter capitalized, upper (upper case), lower (lowercase) ----
the SELECT A. * A emp from the WHERE INITCAP (a.ename) = 'James' and Upper (a.ename) = 'JAMES'

2, character control function

Here Insert Picture Description
example:

---- names include all alternative A is 0 ----
SELECT Replace (a.ename, 'A', '0') from EMP ename A
---- salary 5, if not from the left 5 start with added 0 ----
SELECT LPAD (a.sal, '. 5', '0') A SAL from EMP

3, numeric functions
  • DUAL is a 'pseudo-table', it can be used to test the expression and function

        ---- round: rounding ----
        SELECT round (45.625, 2) from Dual; -> 45.63

        ----trunc:截断----
        select trunc(45.625, 2) from dual; -->45.62

        ----mod:求余----
        select mod(500, 300) from dual; -->200

4. Date
  • Date data type in Oracle actually contains two values: the date and time.
  • SYSDATE function returns: the date, time, for example: select sysdate from dual; -> 2020/3/12 00:00:00
  • Date Functions
    Here Insert Picture Description
    5, the transfer function

Here Insert Picture Description

  • Oracle automatically following conversion (implicit data type conversions)
    Here Insert Picture Description
    For example: select '11' + 2 from dual; -> 13;

  • The display converter
    Here Insert Picture Description
            ---- TO_CHAR date string rotation, string digital-to- ----
            SELECT TO_CHAR (SYSDATE, 'the MM-dd-YYYY'), TO_CHAR (2121.13, 'L000,999.99') from Dual; -> 2020-03-12, ¥ 002,121.13
            ---- TO_DATE string transfer date ----
            SELECT TO_DATE ( '2020-03-12', 'the MM-dd-YYYY') from Dual; -> 2020 /. 3 / 12 is
            ---- TO_DATE string-to-digital ----
            SELECT to_number ( '¥ 002,121.13', 'L000,999.99') from Dual; -> 2121.13
    Here Insert Picture Description

  • Date format element
    Here Insert Picture Description

    6, generic function
  • NVL (expr1, expr2) -----> If expr1 is null, then the value of expr2 choice, otherwise choose their own

        ---- query table does not have the staff department employee number, department number to write no department ----
        the SELECT a.ename, NVL (the TO_CHAR (a.mgr), 'no department') job from emp a;

  • NVL2 (expr1, expr2, expr3) -----> If the selection is not empty expr1 expr2, if the selection is empty expr3

        ---- queries sector no bonus prize of 1, plus there's a bonus ---- 0.5
        the SELECT a.ename, NVL2 (a.comm, a.comm + 0.5, 1) from emp A

  • NULLIF (expr1, expr2) -----> equal returns NULL, ranging from the return expr1
  • COALESCE (expr1, expr2, ..., exprn) -----> advantages compared with COALESCE COALESCE NVL in that a plurality of values ​​can be processed simultaneously alternately. If the first expression is empty, the next expression returns on other parameters COALESCE
7, the conditional expression
  • Using the IF-THEN-ELSE logic in SQL statements
  • There are two ways:
    the CASE expression
    DECODE function

        ----- department number for employees to query information 10, 20, 10 printing sector wages 1.1 times, 1.2 times the wages in other sectors print (the CASE) ----
        the SELECT a.ename, a.deptno, Case A .deptno when 10 then a.sal * 1.1 else a.sal * 1.2 end new_sal from emp a;

        -----(DECODE)----
        select a.ename,a.deptno,decode(a.deptno,10,a.sal * 1.1,a.sal * 1.2) new_sal from emp a ;

  • A training practice

        ---- 1. Print out the "2020 March 12 09:05:08" the current system date and time format (Note: Use double quotes to add character to date)
        the SELECT the TO_CHAR (sysdate, 'YYYY' in "MM" month "DD" day "HH: MI: SS ') from dual; ----> 2020 March 12 09:05:08

        ---- 2, the format numbers: 1,234,567.89 to 1,234,567.89
        SELECT TO_CHAR (1234567.89, '999,999,999.99') from Dual;

        ---- 3, to digital string, if no special characters in the string, an implicit conversion may be:
        SELECT '1234567.89' from + 100 Dual;

        ---- 4, if the special characters string, e.g. "1,234,567.89", implicit conversion is not necessary to use to_number () to complete the
        select to_number ( '1,234,567.89', ' 999,999,999.99') + 100 from dual; - ---> 1,234,667.89

        ---- 5, the date for the query as a query, generally use to_date () to a string to date, so do not have to focus on the date format
        select a.ename, a.hiredate from emp a where a.hiredate = to_date ( '1981-2-20', ' yyyy-mm-dd');

        ---- 6, the end of each month inquiry penultimate two days recruits information
        . Select a * from emp a where a.hiredate = last_day (a.hiredate) - 2;

        ---- 7. calculated employees salary
        select a *, a.sal * 12 + nvl (a.comm, 0) salary from emp a.;

Multi-table query

1, Cartesian product
  • TABLE EMPLOYEES (20 rows), Table DEPARTMENTS (8 lines), the Cartesian set: 20x8 = 160 rows

  • Descartes rally produced under the following conditions:

        Connection condition omitted
        join condition is invalid
        in all rows in all tables are connected to each other

  • How to avoid Cartesian set:

        May be added in effective connection conditions WHERE.

2, alias table
  • Using aliases, simplify the query. Use table prefix can improve the efficiency.
    Such as:
SELECT e.employee_id,
       e.last_name,
       e.department_id,
       d.department_id,
       d.location_id
  FROM employees e, departments d
 WHERE e.department_id = d.department_id
3, a plurality of connection tables
  • Connecting the n-tables, at least n-1 connections conditions. For example: three connection table, at least two connection conditions.
4, non-equivalent connection
  • Staff query table (emp) in wages belong to the pay scale table (sal_grades) in the first few files:
select a.ename, a.sal, b.level
  from emp a, sal_grades b
 where a.sal between b.low_sal and hight_sal;
5, the inner and outer joins
  • En :
    combined with two or more rows of the same column of a table, the result set does not contain a table and another table rows that do not match

  • External connection :
    two tables satisfy the join condition in addition return line also returns the left (or right) in the table row condition is not satisfied during the connection, this connection is called a left (or right) outer join. No matching row, the corresponding result table is empty (NULL). WHERE clause conditions are similar to the outer connecting the internal connection, but is connected to the back of the list does not match the condition to increase the outer connecting line operator, i.e., with parentheses plus sign (+) of

Example (right outer join):

select * from emp a, dept b where a.deptno(+) = b.deptno;
6, natural connection
  • NATURAL JOIN clause creates the equivalent connection with the conditions listed in the two tables with the same name. In the table data query to satisfy the equivalent conditions. If only the same column name and different data types, an error occurs.
7, ON clause to create a connection
  • Natural join condition is listed as a connection with the same name of. ON clause can specify additional connection conditions. The connection condition is separate from other conditions. ON clause makes statements higher legibility.
  • ON clause to create a multi-table joins
SELECT employee_id, city, department_name
  FROM employees e
  JOIN departments d
    ON d.department_id = e.department_id
  JOIN locations l
    ON d.location_id = l.location_id;
8, since the connection
  • Connect to their own

E.g:

  select m.*
  from employees e, employees m
  where e.manager_id = m.employee_id and e.last_name = 'Chen'  
9, left (and right) outer join, and full outer joins
  • During the connection the two tables in addition to the return line further satisfy the join condition to return the left (or right) table row condition is not satisfied, this connection is called a left (or right) outer join.

  • During the connection the two tables in addition to the return line further satisfy the join condition in the return line condition is not satisfied two tables, this connection is called a full external connection.
    Here Insert Picture Description


The next chapter, (3) Oracle ------ grouping function, sub-queries, create and manage tables and data processing

Published 67 original articles · won praise 19 · views 9854

Guess you like

Origin blog.csdn.net/qq_41530004/article/details/104809399