SQL 小技巧

-------查看锁

SELECT object_name, machine, s.sid, s.serial#
FROM gv$locked_object l, dba_objects o, gv$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid;

-------解除锁

ALTER system kill session 'sid,serial';

--------日期格式

select to_char(to_date(to_char(to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi:ss'),
'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss'),
'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss') times
from dual

---------查看用户的总表

select count(*) from user_tables

---------所用用户的总表

select OWNER , TABLE_NAME from all_tables order by OWNER

--------所有视图

select view_name from view_views

--------索引个数和类别

select *  from index_name,index_type,table_name from user_indexes order by table_name

一、基本概念
    表(table):用于存储数据,由行(row)和列(column)构成
    行(row):代表表中的一条数据,也叫实体(entity)
    列(column):代表数据的一个属性,也叫字段(field)
    关系(relationship):表和表之间的联系
        主键(Primary Key)、外键(Foreign Key)


二、服务器端/客户端(Client/Server)
    必须要启动的服务:OracleServiceXE、OralceXETNSListener
    
    1、sqlplus:oracle提供的一个客户端软件,用于进行数据库的操作。
        开始-->运行-->输入cmd-->在DOS界面,输入 sqlplus 用户名/密码
        
        对HR用户解锁:1)管理员登录
                            sqlplus / as sysdba
                      2)解锁(激活用户)
                            alter user 用户名 account unlock;
        --查询和当前用户相关的表
        select table_name from user_tables;
        
        退出:exit;
    2、iSqlPlus:oracle提供的一个基于IE的客户端软件,用于进行数据库的操作。
        进入:http://localhost:8080/apex
        
        sqlPlus命令:在oracle提供的客户端工具中可以使用的命令。
            sqlPlus
            isqlplus
            --查看表结构
            desc 表名
        SQL命令:用于对数据进行增删改查操作、对数据库进行管理;
            适用于所有的RDBMS;
            结构化查询语句 Structure Query Language
        PLSQL:oracle在标准SQL基础上,进行了功能扩展。

三、排序
    select 列名 from 表名 order by 列名 asc(升,默认)/desc(降序),列名2 asc/desc
    1、单列排序
        --查询所有员工信息,并按工资从高到低显示
        select * from employees order by salary desc;
        --查询所有员工信息,并按工资从低到高显示
        select * from employees order by salary asc;
    2、多列排序
        --查询所有员工信息,并按工资从高到低显示,如果工资相同,再按部门编号从大到小排
        select * from employees order by salary desc, department_id desc;
        --先按工资升序,如果工资相同,再按部门降序
        select * from employees order by salary , department_id desc;

五、条件查询

  select 列1,列2 from 表名 where 过滤条件表达式 order by 排序列 asc/desc
    1、等值查询
        --查询工资是24000的员工编号、姓名、工资
        select employee_id, first_name, salary from employees where salary=24000;
        --查询员工姓是king的员工编号、姓名、工资
        select employee_id, first_name, salary from employees where first_name='King';
        注意:在比较时,如果字段类型是字符或字符串类型,必须使用‘’
                同时,单引号中的内容,严格区分大小写
    2、> >= < <= != and or
        --查询工资是24000同时姓King的员工编号、姓名、工资
        select * from employees where salary=24000 and last_name='King';
        --查询工资高于20000的员工编号、姓名、工资
        select * from employees where salary > 20000;
        --查询部门90和100下的所有员工
        select * from employees where department_id=90 or department_id=100;
    3、列名 is [not] null --字段值是否为null
        --查询没有奖金的员工信息
        select * from employees where COMMISSION_PCT is null;
        --查询有奖金的员工信息
        select * from employees where COMMISSION_PCT is not null;
    4、列名 [not] between 小值 and 大值
        --查询工资介于10000到20000之间的员工信息
        select * from employees where salary >=10000 and salary<=20000;
        select * from employees where salary between 10000 and 20000;
        
        注意:小值在前,大值大后;包含边界值。
    5、列名 [not] in (值1,值2,。。。)
        --查询部门90、100、60下的员工
        select * from employees where department_id=90 or department_id=100 or department_id=60;
        select * from employees where department_id in (90,100,60);
    6、模糊查询【重点】
        列名 [not] like '格式字符串',其中格式字符串中通常包含通配符:
            %(0-N多字符);_(仅代表1个字符)
        --查询姓中包含L的员工
        select * from employees where last_name like '%L%';
        --查询姓中第2个字母是L的员工
        select * from employees where last_name like '_L%' or last_name like '_l%';
        --查询姓的长度是5,并且第2个字母是L的员工
        select * from employees where last_name like '_L___' or last_name like '_l___';
        --查询名字中不包含L的员工
        select * from employees where last_name not like '%L%';
六、去掉查询结果中的重复数据行
    select distinct 列名 from 表名

七、case when then else end  类似JAVA中的switch语句
    case
        when 条件1 then 表达式1
        when 条件2 then 表达式2
        else 表达式N
    end  as 列别名

    --查看员工的工资情况:>20000 ***** ; 15000-20000  ****;10000-15000 ***;其它 *
    select employee_id, last_name, salary,
        case
            when salary>20000 then '*****'
            when (salary between 15000 and 20000) then '****'
            else '*'
        end as 工资情况
    from employees
    
    employee_id    last_name    salary    工资情况
    100        King        24000        *****
    105        A            17000        ****
    111        B            22000        *****
    55        C            5000        *
    
    注意:一个case...end 代表的是1列的信息,可以给这个列取别名。

提示,oracle中的日期默认字符串格式为"d-m月-yy"'1-5月-97' '31-12月-97'

原文地址:https://www.cnblogs.com/liuruipeng/p/7880780.html