select single-table queries and sqlplus

1. Use a query statement to select a particular table of contents of all data
    Syntax:
    select [DISTINCT] * {col_name1, col_name2, ..}
    from tb_name;
    Note: Syntax appearing in brackets [] indicate the optional part
    * : that all columns, just as a test and learn to use, does not appear in corporate parlance as inefficient and poor readability
    col_name1: column names, you will need access to the data fields listed, you can view multiple column values between the column name use, can be divided
    s_emp: employee information table
    s_dept: staff department table
demand: see all records s_dept table
    the SELECT *
    from s_dept;

    the SELECT id, name, region_id
    from s_dept;
exercise: View all records s_dept table of id and name
    the SELECT id, name
    from s_dept;


exercise: see id of all employees, the name (last_name) and payroll (salary)
    the SELECT id, last_name, salary
    from s_emp;

2.Select statement can be doing arithmetic on all the values in the column specified .
    Syntax:
    SELECT col_name digital operator
    from tb_name;
demand: view employee id, name and salary of each employee.
    the above mentioned id the SELECT, last_name, salary * 12
    from s_emp;
Note: select statement is never right to modify the original data.
Exercise: Check each employee's employee id, name and salary rose 100 annual salary after
    the SELECT id, last_name, (salary + 100) * 12
    from s_emp;
from column 3 to the query alias
    syntax:
    the SELECT old_column [AS] new_column_name
    from tb_name;
demand: employees view employee id, name and salary, salary column called Annual
    the SELECT id, last_name, salary * 12 AS Annual
    from s_emp;

4. || can make use of the value of multiple columns or columns and special string into one display column
    syntax:
    SELECT col_name || 'spe_char' || col_name
    from tb_name
    'spe_char': If the value of the column to keep a particular string connected to the display, using this syntax.
Demand: View staff employee id, the full name of
    the SELECT id, first_name last_name ||
    from s_emp;
Exercise: View staff employee id, full name and job title, full name and job title are combined into one display, and the format is: name, job title
    select id, first_name || '' || last_name || ',' || title as name
    from s_emp;

5. worthy replacement for null operation
    nvl () function
    syntax:
    the SELECT NVL (col_name, change_value)
    from tb_name;
demand: view employee id, name and all employees of the commission, if mention become empty, displayed as 0
    the SELECT the above mentioned id , last_name, NVL (a commission_pct, 0) a commission_pct
    from s_emp;
6. the use of distinct keywords may be displayed in duplicate records show only one
    syntax:
    SELECT distinct col_name, ... col_name
    from tb_name;

    Note 1: distinct keywords only keywords can select on the back
    such as: select the above mentioned id, title distinct
        from s_emp;
    the statement syntax wrong !!!!!
    Note 2: If the distinct keyword if there is more behind the columns represent multi-column joint de-emphasis, namely multiple columns values are considered to be the same time will duplicate records.
    test table:
    the above mentioned id id2
    1 2
    1 3
    2 4
    3 4
    3 4
    the SELECT DISTINCT id, id2
    from the Test;
    show results:
    the above mentioned id id2
    1 2
    1 3
    2 4
    3 4
Demand: See all employees of job title and department id, the same position in the same department the only show once
    the SELECT DISTINCT title, dept_id
    from s_emp;

7.sqlplus command
   after sqlplus login, you can use the buff (cache) to store / execute / modify the sql statement to execute
   the characteristics of the buff here:
      1.buff can only store a sql statement (but this sql statement may have a lot of lines)
      2. each placed in a new sql statement will overwrite the previous
      3. each implementation of sql statement, sql statement will put this into buff inside

    l View sql statement cache
    a in [Location] behind the row of additional new content
    i is inserted in the new line [positioned] in the row below
    Alternatively c [targeting] the line that certain of the string
        c / old string / new string
    del Delete [targeting] the contents of the row
    behind the content may be rewritten to this was added n rows
    ! command terminal connected behind! Clear : clear the screen windows for example in the use of the $ symbol: $ CLS
    / execute sql command buffer

    clear buffer: clear the current cache command

        
    save test.sql buff sql statement stored in a file test.sql
    get test.sql in the test.sql content loaded into the buff, but it is not running
    start test.sql the contents test.sql in the buff loaded into and executed
    @ test.sql the contents test.sql in the buff loaded into and executed
    edit file_name use the default editor to edit the file



    spool file_name save the run and the results of the next sql sql statement to a file
        SQL1
        result1
        SQL2
        result2
        ...
    spool OFF closed spool function
    exit: exit
    
ID 8.select, last_name, FIRST_NAME, the salary, the dept_id
  from s_emp
  the Where rownum <= 10;

  the result does not look good, column by our nice display interface.

    COLUMN last_name FORMAT a15;
    can be abbreviated as:
    COL last_name for A15;
    the COLUMN FIRST_NAME the FORMAT A15;



Chapter II: Sorting and restrict the query
1. Sort: the so-called sorting is based on the value of a field case according to the ascending or descending a record query out
    grammar:
    the SELECT col_name, ...
    from tb_name
    order by col_name [asc | desc], ...
    Note: Sort order by using words that show only adjustment clause query records, does not change the query results, so. minimum executive powers, and final execution.
    2. Sort Key words:
        ASC: ASC (default, default meaning without keywords when default sort order living)
        desc: Descending
    3. If there are multiple columns are sorted, the sorted column is provided behind the front row sorted after having repeated (same) value.

    Examples:
    ID ID2
    . 1 2
    2. 3
    . 4. 3
    . 4. 1
    . 4 2

    statement:
    SELECT ID, ID2
    from Test
    Order by ID, ID2 desc;
    
    Results:
    ID ID2
    . 1 2
    2. 3
    . 3. 4
    . 4 2
    . 4. 1
    Note: The first column of the first row, first column if there are duplicate value again ranked second column, and so on
demand: View employee id, name and salary, displayed in descending order according to salary.

2. restrict the query, the query that is specified query
    syntax:
    SELECT col_name, ...
    from tb_name
    where col_name comparison operation expression
    logical operator
          col_name comparison operation expressions
    ...
    Note:
    1. Limit the query, using the where clause
    2. the plurality of conditions, and logical operators () integrated logic conditions
    the highest priority 3.where clause
    4. the comparison operation by the operator, and value expression consisting of
        Common operations:
        a "logical comparison operators
        => <!> = <= =
        2" is not equal: three have said meaning not equal (= often is!)
        = <!> ^ =

Demand: View employees staff salaries less than the id and name 1000
    the SELECT id, last_name, salary
    from s_emp
    the WHERE salary <1000;

        2 "SQL comparison operators
        between and: to what extent the
requirements: Check in staff wages id between 700-1500, and the name of
    the SELECT the above mentioned id, last_name, salary
    from s_emp
    the WHERE salary the BETWEEN 700 and 1500;
        in (list): in a list of
demands: View staff numbers 1,3,5,7,9 wages
    the SELECT the above mentioned id, last_name, salary
    from s_emp
    WHERE ID in (1,3,5,7,9);
        like: fuzzy query, i.e., the value is not the exact value of the time use
        a wildcard that can replace any symbolic content
        %: 0 to a plurality of character wildcard
        _: If and only if a wildcard character

        escape character:
        the default is \, you can specify the designated time indicated to escape with symbols, characters can only escape the escape character after
demand: view employee names beginning with the letter C employee id, salary.
    id SELECT, last_name, the salary
    from s_emp
    WHERE last_name like '% C';
Exercise: view employee name length not less than 5, and the fourth letter of the letter n id and employees wages
     SELECT id, last_name, the salary
    from s_emp
    WHERE last_name like '___ n_%';
demand: view employee names replacement staff id and wage a _ of
    the SELECT id, last_name, salary
    from s_emp
    the WHERE last_name like '% \ _%' Escape '\';


        iS null: operating characteristics of the null value defined operator, can not use =
demand: See employees to provide employees become empty id and name
    SELECT id, last_name, a commission_pct
    from s_emp
    WHERE iS a commission_pct null;
    3. logical operators
    When there are multiple conditions when using
        and: and logical
        or: or logical
        Note: and logic is higher than logic or
    not: illogical
demand: view employee id of 41 departments and job titles for the Stock Clerk (warehousing administrator) of employee id and name
    select id, last_name, dept_id, title
    from s_emp
    the WHERE dept_id = 41
    and
          title = 'Stock Clerk';
exercise: view employee sector 41 or 44 departments and wages greater than the employee id and name 1000 of
    select id, last_name , dept_id, title
    from s_emp
    the WHERE salary> 1000
    and
          (dept_id = 41
          or
          dept_id = 44);
      view employee sector 41 and the salary is greater than 1000 or 44 department employees id and the name of
    the SELECT id, last_name, dept_id, title
    from s_emp
    the WHERE salary> 1000
    and
          dept_id = 41
    or
          dept_id = 44;





Guess you like

Origin www.cnblogs.com/yxj808/p/12022495.html