Oracle Database -sql statement

 

Single-table query

1, all data lookup table select * from table name; * on behalf of all
  select * from emp;
2, look-up tables specify values select field name field 1, field name 2, ... from the table name
   select empno, ename from emp;
3, to the fields in the query results using an alias
  using the keyword field name as "alias" in the name field
  action: for easy viewing results
  Note: as keywords can be omitted, alias no special characters in double quotation marks can be omitted to write.          
  select empno employee number, ename "employee name", job as work, mgr as "No leadership" from emp;
4, connector: select field name || 'character' || || ..... from table field names name
  || sql statement to link the symbol characters, and select from between in
  character linking format 'character' field name to field name || ||
  Note: a spliced connection in the result is displayed as a new field can be used to optimize field displays the aliases.
  select empno || 'name is' || ename as "information", job || 'ha' || MGR from EMP;
. 5, deduplication select distinct field names, field names, table names ... FromN
  NOTE: Removal repeat rule is removed by rows, whichever is the same as a plurality of data lines
  select distinct job,

  1) Single Field sorting
    select * from table order by field name asc ascending order asc can be omitted
    select * from table order by field name desc descending sort
  2) multiple-field sorting
    select * from EMP order by field name 1, field 2 ... name
    first field sorted according to 1, the same as the value of 1 if the field is sorted according to field 2, ....
    the SELECT * from emp the Order empno desc-- single field sorting by descending
    select empno, ename, job from emp order by ename asc-- single field ascending
    select * from emp order by empno, ename-- multi-field sorting
7, a logical operation field
  field between the keyword and select a keyword from the four arithmetic operations can be performed directly
  field as the field of operation may be performed directly between the
  Note: field is numeric type
  select EMPNO, ename, Job, SAL + 2 * 1000, SAL + COMM from EMP
. 8, where clause query filter
  select field names, field names, from ... table where the filters
  1) single filters
    used to filter operator =,>,> =, <, <=, <> individual conditions
    NOTE: If the value of the character conditions, must use single quotes Come
    Query for all employees salary information
    select empno, ename, sal + comm as salary from emp
    query SMITH personal information
    select * from emp where ename = ' SMITH'
    query salary information SMITH, logical operators =
    SELECT EMPNO, ename, SAL , sal + comm from emp where ename = 'SMITH'
    query salary greater than the employee information 1000, logical operators>
    SELECT * from EMP WHERE SAL> '2000'
    query salary is not equal to the employee information 3000 to
    select * from emp where sal <> 3000 SAL by the Order
  2) multi-condition screening (where clause keywords: and, or, like, is null, is not null, in, between and)
    query wage employee information between 2000-3000
      - and use the keyword , conditions are true multi-screening using keywords and conditions connected
                 select * from emp where sal> = 2000 and sal <3000
      - Use keywords between and connecting conditions, contains two data
                 select * from emp where sal between 2000 and 3000
    Employee information inquiries as SALESMAN, ANALYST, MANAGER of
                 - use or keyword, or conduct screening criteria.
                * the Job from the WHERE emp the SELECT = 'SALESMAN' or the Job = 'ANALYST' or the Job = 'MANAGER' the Order by the Job
      - use in keywords, or can also be screened, but only in the contents of a field value .
      select * from emp where job in ( 'SALESMAN', 'ANALYST', 'MANAGER')
    Queries included in s name to the beginning of the s, s of the ending, the second character is A. (Fuzzy queries)
      -% of any of a plurality of arbitrary character number table
      select * from table where field name like '%% characters' query contains the specified character data
      select * from emp where ename like ' % S%' - comprising S the
      select * from table where field names like 'character%' queries that begin with the specified character data
      select * from emp where ename like ' S%' - beginning with S
      select * from table where field names like '% character '

      select * from table where field name like 'character _%' queries position specified character data
        --_ represents an arbitrary character
        select * from emp where ename like ' _A%' - the second character is A
      - -select * from table where field names like '% character 2 character 1%' escape 'character 2'
      escape the specified character becomes the escape character: escape character can be special characters into common character
      select * from emp where ename like '% / _%' escape '/'
    query has subsidized employee information
      select * from table where field name is null field value is null
      select * from table where field name is not null field value is not null
      more conditions of use and key connection, filtering the data meets all the conditions of the
      select * from table where conditions 1 and filter condition and .... 2
      select * from emp where COMM iS not null and COMM> 0 

Oracle function (one-way function, multi-line function, transfer function, other functions)

One-way function (character function, numerical functions, date function)

Features 1: Do not change the real data, but the data do show a further modification or processing.
Feature 2: can be mixed and fields

  • Character functions:

  Discover all employee information, employee name in lowercase.
  select empno, ename, lower (ename ), job, mgr, sal, lower ( 'HH') from emp
  query all the employee information, employee name capitalized.
  select empno, INITCAP (ename) capitalize the first letter of the name, lower (ename), job from emp

  • Numerical functions: numerical calculation data type

 

  Pseudo table: the presence of real tables in order to facilitate the presence of temporary authentication data table. Table name: Dual
  SELECT ABS (-1), ceil (2.2), Floor (3.3), Power (2,3), MOD (5,2), round (4.55), the trunc (10 / 3,2-) from dual

  • Date function:

  select months_between ('01 - January -2018 ',' dated 24-6 -2017 ') from dual - Returns the number of months between the two dates 
  select add_months ('01 - April -2018', - 4) from dual - returns the specified number of months after the date of
  select next_day ('16 - April -2018 ',' Tuesday ') from dual-- inquiry date of the last week of
  select last_day ('16 - April -2018') from dual- - returns the last day of the month of the date of
  select round (to_date ('19 - April -2018 '),' dAY ') from dual-- be rounded off in accordance week

Multi-line function (max, min, avg, sum, count) is important

  作用:对查询的数据进行统计
  使用:select 多行函数名(字段名),多行函数名(字段名)..from 表名
  注意:多行函数不能和普通字段以及单行函数混用,除非分组
  max(字段名) 返回该字段的最大值
  min(字段名) 返回该字段的最小值
  sum(字段名) 返回该字段的和
  avg(字段名) 返回该字段的平均值
  count
    count(*) 返回表的记录数
    count(字段名) 返回非空值的数量
    count(distinct 字段名) 去除重复后的字段值的数量
  查看员工的最高工资
  select max(sal) from emp  --多行函数不能和字段直接混用,除非分组。
  select lower(ename),max(sal) from emp  --多行函数 不能和单行函数混用,除非分组
  查看员工的最低工资
  select min(sal) from emp
  查看员工的平均工资
  select avg(sal) from emp
  查看所有的员工工资之和
  select sum(sal) from emp
  查询公司有多少员工
  select count(*) from emp--查询表的记录数
  查询有津贴的员工人数
  select count(comm) from emp  --查询字段的值的数量,null会自动过滤
  查询公司有多少工作种类    
  select distinct job from emp
  select count(distinct job) from emp
  select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp 

 转换函数

  to_number(数值类型的字符):将字符转换为数值
  to_char(数值或者是日期):将数值或者日期转换为字符
  to_date(日期格式的字符):将字符转换为日期
  ----------------数值和字符的互转-----------------------
  1)字符转换为数字char---->number
  select to_number('123')+2  from dual
  2)数字转换字符number--->char
  指定显示格式:
    9表示位置占位,例如999,999,999会将数字按照三个一组使用逗号隔开。
    L表示人民币符号,$表示美元符号
    0可以进行占位分组,但是如果真实数据位数不足,会使用0进行补位。
    select to_char(12345,'$999,999,999') from dual   --$12,345
    select to_char(12345,'L999,999,999') from dual   --¥12,345
    select to_char(12345678,'000,000,000,000.000') from dual   --000,012,345,678.000
  查询工资大于2000的员工信息
  数值和字符之间的转换可以隐式转换。to_number可以省略不写.
  select * from emp where sal>'2000';
  select * from emp where sal>to_number('2000');
  ---------------日期和字符的互转---------------------------
  一般使用方式:新增数据使用to_date(),查询数据使用to_char()
  1)字符转换为日期 char--->date
  使用to_date('要转换的字符',日期格式)函数将字符转换为日期
  注意1:字符必须符合日期格式
  注意2:oralce默认的转换格式为日月年,例如'01-1月-2018' oracle认为是一个日期
  常用日期格式:
    yyyy-mm-dd
    yyyy/mm/dd
  查询员工入职日期在82年后的信息
  select * from emp where hiredate >to_date('1982-01-01','yyyy-mm-dd')
  select * from emp where hiredate >to_date('1982/01/01','yyyy/mm/dd')     
  select * from emp where to_char(hiredate,'yyyy-mm-dd') >'1982-01-01'
  2)日期转换为字符  date--->char
  使用to_char('要转换的日期',转换格式)
  注意:如果不指名转换格式,则使用默认格式,日月年例如:'01-1月-81'
  常用转换格式:
    yyyy-mm-dd
    yyyy/mm/dd
    'yyyy"年"mm"月"dd"日"'
  select to_char(hiredate) from emp  --使用默认格式将日期转换为字符
  select to_char(hiredate,'yyyy-mm-dd') from emp  --使用指定格式  yyyy-mm-dd
  select to_char(hiredate,'yyyy/mm/dd') from emp  --使用指定格式 yyyy/mmm/dd
  select to_char(hiredate,'yyyy"年"mm"月"dd"日"') from emp  --使用指定格式 'yyyy"年"mm"月"dd"日"'

其他函数:

  1)nvl():nvl(字段名,新的值)
  如果字段值不为null,则返回该字段的值。如果为null则返回新的值
  2)nvl2():nvl2(字段名,处理1,处理2)
  如果字段值不为null,则执行处理1,为null执行处理2
  3)decode():decode(字段名,值1,处理1,值2,处理2,值3,处理3,...,公共处理)
  如果字段的值和decode中的条件值相同则执行对象的处理。如果都没有则执行公共处理
  查询员工的工资信息
  select ename,job,sal from emp
  查询员工的薪水信息
  select ename,job,sal+nvl(comm,0),sal+comm,sal from emp
  select ename,job,nvl2(comm,sal+comm,sal) from emp
  显示员工的职称
  select ename,job,decode(job,'MANAGER','经理','PRESIDENT','董事长','SALESMAN','销售','普通员工') from emp

分组查询&筛选学习

  关键字:group by 分组字段名,分组字段名....
  注意1:使用了分组后,在select语句中只允许出现分组字段和多行函数。
  注意2:如果是多字段分组,则先按照第一字段分组,然后每个小组继续按照第二个字段继续分组,以此类推。
  注意3:在where子句中不允许出现多行函数。
  分组筛选
    关键字:having
    作用:针对分组进行分组后的数据筛选,允许使用多行函数。
    注意:having关键必须和分组结合使用。不允许单独使用。  
    where和having的比较:
      where子句不允许出现多行函数,having允许出现多行函数
      where子句和having都可以使用普通字段直接进行筛选,但是where的效率高于having
      where执行顺序: from--->where--->group by-->select-->order by
      having执行顺序:from--->group by-->select--->having--->order by
    结论:在分组语句中,使用where进行字段级别的筛选,使用having进行多行函数的筛选。   
  查询最高工资和员工数
  select max(sal),count(*) from emp
  查询不同部门的最高工资
  select deptno,max(sal) from emp group by deptno
  查询不同工作岗位的员工数
  select job, count(*) from emp group by job
  查询不同部门的不同工作岗位的人数
  select deptno ,lower(job),count(*) from emp group by deptno,job order by deptno
  查询不同部门的不同工作岗位的并且人数大于1的信息
  select deptno ,lower(job),count(*) from emp  group by deptno,job having count(*)>1 order by deptno
  查询部门号大于10的不同部门的不同工作岗位的人数
    使用having关键字
    select deptno ,lower(job),count(*) from emp group by deptno,job  having deptno>10  order by deptno
    使用where关键字
    select deptno,job,count(*) from emp where deptno>10 group by deptno,job  order by deptno

数据库的增删改&数据备份

  注意:增加删除修改的数据SQL语句执行完毕后,不会立马进行数据的写入。
    还需要手动对数据进行提交,如果数据有问题还可以回滚
  主键:非空唯一的字段可以设置为主键。
    在一张表中,某个字段的值是非空唯一的,将此字段设置为主键。
    主键的作用:唯一的标识一条数据。

增加数据

  insert into 表名(字段名,字段名,...)values(值1,值2,值3....);
  注意1:主键必须给值,允许为空的字段可以不给值。
  注意2:插入语句表名后跟的字段名为要赋值的字段,值和字段数量和顺序必须是一一对应的。
  注意3:如果是全字段插入,可以省略字段名部分 insert into 表名 values(值1,值2,.....)
  在部门中新增一个新的部门信息,信息内容为 编号:50,名称:a学院,地址:北京
  insert into dept(deptno,dname,loc)values(50,'a学院','北京');
  insert into dept(deptno,dname,loc)values(60,'a学院','北京');
  insert into dept values(60,'a学院','北京');
  在部门中新增一条数据,只有部门编号和名称,没有地址。
  insert into dept(deptno,dname)values(70,'a学院');

删除数据

  delete from 表名 删除表中的所有记录
  truncate table 表名  删除表中的所有记录,但是效率高于delete
  delete from 表名 where 条件 --删除指定的数据,只要符合条件就会删除
  delete from dept where deptno=50 --删除指定的数据
       delete from dept --清空表数据
       truncate table dept--清空表中数据

更新数据

  update 表名 set 字段名=新的值,字段名=新的值...(会将字段的值全部改为新的值)
  update 表名 set 字段名=新的值,字段名=新的值... where 条件(将符合条件的数据的字段改为新的值)
  update dept set dname='a学院',loc='上海'
  update dept set dname='a学院',loc='上海' where deptno=50

数据的备份

  注意:只会备份表结构和表的数据,约束不会备份。
  表级别备份
    全部备份:create table 新的表名 as select * from 备份表名
    部分备份:create table 新的表名 as select 字段名,字段名,...from  备份表名
  数据整体插入
    insert into 插入表名 select * from 表名
    注意:查询语句结果的字段数据必须和插入表名的字段数量一致,类型要一致。
       create table deptBak as select * from dept  --全部备份
       create table deptBak2 as select deptno,dname from dept  -- 部分备份
       insert into deptBak2 select deptno,dname from dept

Guess you like

Origin www.cnblogs.com/sunny-sml/p/11946620.html