Oracle database query and limited sort of display Detailed

Fifth, limited querying and sorting display

5.1, limited inquiry

5.1.1 limited understanding of inquiry

  • For example : if a table has 100w of data, once performed, "SELECT * FROM table" statement, all records in the table rows of data will be displayed on the screen, it is neither easy to browse, can also cause crash problem, so At this point it is necessary to filter the results of a query, select only their useful data can be, it can be specified by the query WHERE filter criteria.
  • Displayed together so many pieces of data is certainly not browsing. In addition, if the excessive amount of data displayed, it is possible that crash problem occurs. Therefore, the following first to observe the amount of data to query and display what problems will exist.
  • When the database during installation has a sample program database is installed, it must now be switched from the container into the CDB PDB.

Example : In the opened nolog sqlplus (command line terminal)

sqlplus /nolog

Examples : Use sys Administrator Login

CONN sys/chagne_on_install AS SYSDBA;

Example : switching into the PDB

ALTER SESSION SET CONTAINER=pdbmldn;

Examples : Open PDB

ALTER DATABASE pdbmldn OPEN;

Examples : View user's data table contents sh

SELECT COUNT(*) FROM sh.sales;
  • Now this table 9w multiple records exist, issue the following instruction directly if:
SELECT * FROM sh.sales;
  • Now display the result has been constantly changing, you can not view, press Ctrl + C to stop. So now a large amount of data, so it is not possible to directly access all the data, displays all the data lines impossible to use. Many times often need to be screened for the required data, and screening function is defined queries.
  • C ## scott now connected to the user:
conn c##scott/tiger;

5.1.2 defined syntax:

SELECT [DISTINCT] * | 列名称 [AS] [列别名],列名称 [AS] [列别名],...
FROM 表名称[表别名]
[WHERE 条件( s )];
  • Among this syntax, is one more than the previous syntax of the WHERE clause, you can set a series of filter conditions in the WHERE clause. These conditions may be provided a plurality, you can use logical connection between the plurality of operation conditions.

  • There are three logical operators:

    • And (AND): connecting a plurality of conditions, when a plurality of conditions are met returns TRUE, the result does not satisfy the condition that there is a FALSE;
    • Or (OR): connecting a plurality of conditions, among a plurality of conditions as long as there returns TRUE, the result is TRUE, if a plurality of conditions are returned FALSE, the result is FALSE;
    • Non (NOT): negation operation, can change TRUE FALSE, FALSE becomes TRUE.

    Logic truth table:

NO. Condition x Conditions y x AND y x OR y NOT x
1 TRUE TRUE TRUE TRUE FALSE
2 TRUE NULL NULL TRUE FALSE
3 TRUE FALSE FALSE TRUE FALSE
4 NULL TRUE NULL TRUE NULL
5 NULL NULL NULL NULL NULL
6 NULL FALSE FALSE NULL NULL
7 FALSE TRUE FALSE TRUE TRUE
8 FALSE NULL FALSE NULL TRUE
9 FALSE FALSE FALSE TRUE TRUE

Examples : a basic salary higher than the statistics of all employee information 1500

  • Now there have been a query conditions require, so in this case you must use the WHERE clause to set conditions.
SELECT * FROM emp WHERE sal>1500;
  • You can now find that not all the data have shown, only the data portion of the show, and this is part of a data condition is met.

  • Now for the SQL syntax, it will have three clauses:
    • The first step: the implementation of the FROM clause, to control the source of the data
    • Step two: the implementation of the WHERE clause, to filter the data qualifier line
    • The third step: SELECT clause is executed, it is determined to display data column

5.1.3 data defined queries

  • In the previously used ">" is a relational operators in the SQL standard defines many among operators.
  • Operators commonly defined:
NO Operators symbol description
1 Relational Operators >、<、>=、<=、=、!=、<> Or equivalent by size comparison, which is not equal to two:! = And <>
2 Judgment null IS NULL 、IS NOT NULL To determine whether the contents of a column is null
3 Logical Operators AND 、OR、NOT AND represents a plurality of conditions must be met simultaneously, OR represents only have to satisfy a condition, NOT represents a negated condition, namely: true becomes false, false becomes true
4 Range queries AND BETWEEN minimum maximum Looking up in a specified range, the lookup result is: "Min <= content <= the maximum value"
5 Range queries IN You can specify a range query by IN
6 Fuzzy query LIKE You can be fuzzy query for the specified field
5.1.3.1 Relational Operators
  • The relational operators is to determine the size, relatively equal relationship.

Examples : check out all the requirements of the basic salary of all employees equal to less than 2000 information

SELECT *
FORM emp
WHERE sal<=2000;

Examples : SMITH found that the minimum wage according to the results of the previous query, can now hope to get details of SMITH.

SELECT *
FORM emp
WHERE ename='SMITH';

Examples : check out all the clerks (CLERK) of employee information

SELECT *
FORM emp
WHERE job='CLERK';
  • However, when using relational operators determine the character data, please be sure to write the main issue of the case. Because Oracle is case-sensitive.

Example: Error code

SELECT *
FORM emp
WHERE job='clerk';  // 不会有结果返回

Example : After obtaining all the information clerks, and employees to other positions contrast, the decision now and then check out all the employee information is not a clerk.

  • Now that is not a clerical job, then certainly it does not mean that the use of symbols (<>,! =)

    • A realization:
    SELECT * 
    FORM emp
    WHERE job<>'CLERK';   
    • Achieve two:
    SELECT * 
    FORM emp
    WHERE job!='CLERK';

Examples : check out all the information the employee salary range (both included) in 1500 to 3000

  • This judgment is now two conditions, but these two conditions certainly need to meet, then use the AND conditional connection
SELECT *
FORM emp
WHERE sal>=1500 AND sal<=3000;

Example : search jobs sales, and higher than the basic wage for all employee information 1200

SELECT * 
FORM emp
WHERE job='SALESMAN' AND sal>1200;

Examples : check out the information in the 10 department manager or a salesman of 20 departments

SELECT * 
FORM emp
WHERE (deptno=10 AND job='MANAGER') OR (deptno=20 AND job='CLERK');

Examples : the query is not greater than the clerks and all the basic salary of the employee information 2000

  • A realization: the basic realization
SELECT * 
FORM emp
WHERE job!='CLERK' AND sal>2000;
SELECT * 
FORM emp
WHERE job<>'CLERK' AND sal>2000;
  • Achieve two: Use NOT to negate the condition
SELECT * 
FORM emp
WHERE NOT(job='CLERK' OR sal<=2000);
5.1.3.2 range queries

Examples : Using BETWEEN ... AND ... query operators pay all employee information in a range of 1500 (including) to 3000 (inclusive)

SELECT * 
FORM emp
WHERE sal BETWEEN 1500 AND 3000;

Examples : Query all employee information in 1981 hired

SELECT * 
FORM emp
WHERE hiredate BETWEEN '01-1月-81' AND '31-12月-1981';

Indeed here realizes the function to convert between the data string and the date of the operations.

5.1.3.3 null judgment:
  • Determine if content is null: IS NULL, IS NOT NULL

  • grammar:
    • It determines NULL: Field | value IS NULL;

    • Is determined not NULL: Field | value IS NOT NULL; (NOT field | value IS NULL;)

      NULL is an unknown data, so the process of NULL, if the operation is determined using the relation directly, there is not the result of

Examples : use = NULL carried compare

SELECT * 
FORM emp
WHERE comm=null AND empno=7369;
  • The results show that there is no data to return as unavailable = NULL determination.

Examples : check out the complete information on all employees receive commission

  • Commission field is comm, the concept of receiving commission on the part of the commission is not null.

    • Achieve a: direct use of complete IS NOT NULL
    SELECT * 
    FORM emp
    WHERE comm IS NOT NULL;
    • Achieve two: Use IS NULL and negate the NOT completed
    SELECT * 
    FORM emp
    WHERE NOT comm IS NULL;

Examples : check out the complete information on all employees who do not receive commissions

SELECT * 
FORM emp
WHERE comm IS NULL;

Examples : Lists all employees who do not receive bonuses, but also requires all employee information base salary of these employees is greater than 2000

SELECT * 
FORM emp
WHERE comm IS NULL AND sal > 2000;

Examples : find out do not receive commissions or receive a commission of less than 100 employees

SELECT * 
FORM emp
WHERE comm IS NULL OR comm < 100;

Examples : find a different job commission staff

  • Since it is now looking for jobs, it is likely that duplicate, duplicate data must be made to eliminate the use of DISTINCT.
SELECT DISTINCT job 
FORM emp
WHERE comm IS NOT NULL;
5.1.3.4 list range Find: IN, NOT IN
  • The list of so-called range refers to the number of users of fixed reference value, this value is satisfied that they meet the conditions.

  • grammar:
    • Within a specified range of data: Field | values ​​IN (value, value, ...);
    • Data is not within the specified range: Field | value NOT IN (value, value, ...);

Examples : check out the employee number of the employee information 7369,7788,7566

  • Then the face of such an operation, if the determination at this time do not use the IN operator and can use a plurality of conditions connected with OR.
SELECT * 
FORM emp
WHERE empno = 7369 OR empno = 7788 OR empno = 7566;
  • IN implemented using the following codes Excellent
SELECT * 
FORM emp
WHERE empno IN (7369,7788,7566);

Examples : Now inquiry of employee information in addition 7369,7788,7566

SELECT * 
FORM emp
WHERE empno NOT IN (7369,7788,7566);
  • However, when using the NOT IN operation One thing to note, the issue of NULL:

    • If the IN operator determination range data among contains NULL, it will not affect the final query result.
    SELECT * 
    FORM emp
    WHERE empno IN (7369,7788,null);
    • However, if using NOT IN, there are NULL, a direct consequence is that there is no data to display.
    SELECT * 
    FORM emp
    WHERE empno NOT IN (7369,7788,null);
  • NOT IN or IN using only shows part of an object, if there is a data now can not NULL, and NOT IN which satisfies the conditional null, it means that all data is the query. In this way it is possible to cause the amount of data acquired too much cause the program to crash.

  • So, NOT IN in imposes a limit, can not have null, null there is no data, this is a dead restrictions.

5.1.3.4 Fuzzy query: LIKE, NOT LIKE
  • grammar:
    • Meet fuzzy query: Field | The LIKE match mark
    • Does not meet the fuzzy query: Field | value NOT LIKE match mark
  • If you now want to query a column fuzzy queries, you can use the LIKE clause to complete, can be fuzzy search by keyword LIKE, there are two wildcards in a LIKE clause:
    • Percent (%): the character can match any type and length, if the Chinese use two percent (%%)
    • Underscore (_): matches any single character, it is used to limit the expression of the character length

Examples : Query the employee names based on all employees at the beginning of the S information

  • After proof of S content may be any data, it may be 0, 1 or more bits.
SELECT * 
FORM emp
WHERE ename LIKE 'S%';

Examples : check out the names of the employees of the second letter is all the information the employee M

  • Now just a second letter, the first letter can prove any, so use "_."
SELECT * 
FORM emp
WHERE ename LIKE '_M%';

Examples : check out employee information employee name contains the letter F in any position of

  • Now may be the beginning, it could be the end or in the middle, so we must take into account both before and after the problem, then use%.
SELECT * 
FORM emp
WHERE ename LIKE '%F%';

Examples : employee name query length is 6 or more than 6 employee information.

  • The length of the name is 6, then surely you can write six "_" If more than 6, it is plus one percent.
SELECT * 
FORM emp
WHERE ename LIKE '______%';

Examples : check out all of the employees included in the employee's basic salary is to hire 1 or in 81 years

  • Are directed to the operation of the character data, and for all queries before in terms LIKE among can be in digital or; use the date type.
SELECT * 
FORM emp
WHERE sal LIKE '%1%' OR hiredate LIKE (%81%);
  • But there is little need to be reminded that, if you do not set a keyword set when the fuzzy query, it means all queries, such as:
SELECT * 
FORM emp
WHERE sal LIKE '%%' 
    OR hiredate LIKE (%%) 
    OR ename LIKE '%%' 
    OR job LIKE '%%';

The results showed that all the data.

Examples : Find all the clerks in department 20 10 All managers, department managers are not neither a salary greater than or equal clerk but detailed records of all employees in 2000, and requires that these employees among the names contain the letter S or the letter K.

  • Now exists on the following conditions:
    • 10 department managers: a condition
    • The second condition: 20 department clerk
    • Three conditions: not a manager and clerk, but the salary is greater than or equal to 2000
    • Condition IV: refiltered After all of the above conditions are met, the letter contains the letter S or K
SELECT * 
FORM emp
WHERE ((deptno = 10 AND job = 'MANAGER')
        OR (deptno = 20 AND job = 'CLERK')
        OR (job NOT IN (MANAGER,CLERK) AND sal >= 2000))
        AND (ename LIKE '%S% OR ename LIKE '%K%');
  • summary:
    • Defining a primary sort WHERE clause, the data for the selected control.
    • The main defining query operators: relational operators, BETWEEN ... AND, IN, IS NULL, LIKE

5.2 Ordering Display

5.2.1 know the sort

  • When traditional data queries only in accordance with the setting of the primary key order. If you now want to operate on the specified column to sort, then it must be done by controlling the ORDER BY clause.

5.2.2 Sort grammar

SELECT [DISTINCT] * |列名称 [AS] 列别名,列名称 [AS] 列别名
FORM 表名称 表别名
[WHERE 条件(S)]
[ORDER BY 排序的字段|列索引序号 ASC | DESC,
        排序的字段2 ASC | DESC ...]...;
  • You can specify the fields to be sorted in the ORDER BY clause, then the field has two sort modes:
    • ASC: ASC, default
    • Descending: DESC, you need to write.
  • Among all the clauses, we must remember, ORDER BY clause is the last line in the query, the last execution, its execution order: FROM, WHERE, SELECT, ORDER BY, ORDER BY since the SELECT executed after, it means that the ORDER BY clause can use aliases in the SELECT clause set.

Examples : Query and complete information on the employee's basic wage sorted by descending

SELECT * 
FORM emp
ORDER BY sal DESC;

Example : before modifying the query requirements in accordance with the basic wage to sort from low to high

  • A realization:
SELECT * 
FORM emp
ORDER BY sal;
  • Achieve two:
SELECT * 
FORM emp
ORDER BY sal ASC;

Examples : check out all the details clerks (CLERK), and according to the basic salary from low to high Sort

  • Now no longer is for all data sorting, screening needs to be performed on the data, then use the WHERE clause to complete.
SELECT * 
FORM emp
WHERE job = 'CLERK'
ORDER BY sal;

Examples : Query all employee information required in accordance with the basic salary in descending order, if wages are equal sorted by date of employment, in accordance with the order from morning till night

  • Now sorting sort fields need to set two: sal (DESC), hiredate (ASC)
SELECT * 
FORM emp
ORDER BY sal DESC,hiredate ASC;
  • Results show a little problem, no problem with the syntax of this program, the problem lies in the data. Because the results are now in the post-processing of data, if it is to observe the normal data, you can switch to the PDB database, find the original scott data.
  • Methods: After the first implementation of the following two lines of code in DBA_Connection in research
ALTER SESSION SET CONTAINER = pdbmldn;
ALTER DATABASE pdbmldn OPEN;

SELECT * 
FROM scott.emp
ORDER BY sal DESC ,hiredate ASC;
  • But for ordering, in addition to using field outside a serial number may be provided, but this is not recommended.
SELECT empno, ename, sal, job  
FORM emp
ORDER BY sal DESC;

SELECT empno, ename, sal, job  
FORM emp
ORDER BY 3 DESC;

The above two statements perform the same effect, but personally think that, at the time of writing it is recommended that sort of writing on the field, do not use numbers, not convenient.

  • summary

    • SQL syntax:
    SELECTL [DISTINCT] * |列 [AS][别名], 列 [AS][别名],...
    FROM 表名称 [别名]
    [WHERE 限定条件(s)]
    [ORDER BY 排序字段 [ASC|DESC][,排序字段[ASC|DESC]...]];
    • ORDER BY clause to sort query results, ORDER BY clause must be written in all the last query statement.

Description: The learning materials are based on Oracle development LiXingHua finishing the combat classic

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160467.htm