Oracle中sql的基本使用

oracle数据库

 

 

select {distinct} * | 具体列  别名

from table

{where 条件}

{order by 排序字段1,排序字段2 asc | desc}

 

 

 

简单查询语句

灵活显示查询信息 

select '编号是:'||empno||'的雇员,姓名是:'||ename||',工作是:'||job from emp;

使用四则运算

select ename, sal*12 income from emp;

 

 

 

 

限定查询

查询出工资大于1500的所有雇员信息

select * from emp 

where sal>1500;

 

查询每月可以得到奖金的雇员信息

select * from emp

where comm is not null;

 

无奖金的雇员

select * from emp

where comm is null;

 

要求查询出,基本工资大于1500,同时可以领取奖金的雇员信息

select * from emp

where sal>1500 and comm is not null;

 

select * from emp

where sal>1500 or comm is not null;

 

select * from emp

where not (sal>1500 and comm is not null);

 

要求查询基本工资1500--3000的全部工资

select * from emp

where sal>1500 and  sal<3000;

 

    ========between min  and  max========

select * from emp

where sal between 1500 and 3000;  包括等于

 

要求查询出在1981年雇佣的全部雇员信息(1981-1-1,1981-12-31)

select * from emp

where hiredate between ‘1-1月 -81’ and ‘31-12月 -81’; 

where hiredate like '%81%'

 

查询出姓名是smith的雇员信息

select * from emp

where ename='SMITH'; =====oracle区分大小写======

 

查询出雇员编号是7369,7499,7521的雇员的具体信息

select * from emp

where empno=7369 or empno=7499 or empno=7521;

    where empno in (7369,7499,7521);

 

======模糊查询like========

_ : 可以匹配一个字符        % :可以匹配多个字符

 

select * from emp

where ename like '_M%';

where ename like '%M%';

 

 

 

=========order by子句===========<排序肯定是放在整个sql语句的最后执行>

工资低到高排序

select * from emp order by sal;  //默认低到高

 

工资由高到底排序

select * from emp 

order by sal desc;

 

查询10个部门的所有雇员信息,工资由高到低排序,工资相等则按雇员日期由早到晚排序

select * from emp 

where deptno=10 

order by sal desc,hiredate asc;

 

 

 

==================================================================

        ¥¥¥¥¥¥¥¥¥¥单行函数¥¥¥¥¥¥¥¥¥¥¥

==================================================================

、、、、、、数据库中最大的区别就是函数的支持上不同、、、、、、

 

字符函数: 接受字符输入并且返回字符/数值

==将小写字母变为大写字母==<upper()>

select upper('smith') from dual;

 

==将字符串变为小写字母表示==<lower()>

select lower('HELLO, WORLD') from dual;

 

==将每个单词首写字母大写,其他小写==<initcap()>

select initcap('HELLO,WORLD') from dual;

==连接操作==  ||  和concat()函数

select concat('hello ','world') from dual;

 

==字符串截取 substr()---字符串长度length()--字符串替换replace()==

      ----substr()截取点从0或者1的截取效果一样,较智能-----

 

select substr('hello',1,3)  截取字符串,

  length('hello')  字符串长度,

  replace('hello','l','x')  字符串替换

  from dual;  

===显示雇员的姓名以及姓名的后三个字符===

select ename, substr(ename,length(ename)-2)) from emp;

select ename, substr(ename,-3,3) from  emp;

 

 

 

数值函数: 接受数值输入并且返回数值

四舍五入: round()

select round(789.536) from dual;  ====>789

select round(789.536,2) from dual; ====>789.54

select round(789.536,-2) from dual;=====>800

 

截断小数位:trunc()

select trunc(789.536) from dual; =====>789

select trunc(789.536,2) from dual;

 

取余 mod()

select mod(10,3) from dual;

 

 

 

==================================================================

^^^^^^^^^^^^^^^^^日期函数: 对日期型数据进行操作^^^^^^^^^^^^^^^^^^^^^^^

==================================================================

日期 + / - 数字 = 日期        日期 - 日期 = 天数

 

==显示部门10雇员进入公司的周数==

当前日期 ===>可使用sysdate()函数

  当前日期-雇佣日期 =天数 / 7 = 周数

  select empno,ename,round((sysdate-hiredate)/7) from emp;

 

===求出给定日期范围的月数months_between()====

select empno,ename,round(months_between(sysdate,hiredate)) from emp;

 

=======在指定日期加上指定的月数,求之后的日期add_months()  ========

select add_months(sysdate,4) from dual;

 

======= 下一月的今天是哪个日期next_day() ========

select next_day(sysdate,'星期一') from dual;

 

======= 求出给定日期的最后一天last_day() ========

select last_day(sysdate) from dual;

 

 

   |===========================================================================|

   |*************转换函数: 从一种数据类型转换成另一种数据类型*************************|

   |===========================================================================|

   to_char: 转换成字符串

to_number: 转换成数字

to_date: 转换成日期

   -----查询所有雇员的雇员编号,姓名,雇用日期----------

select empno,ename,to_char(hiredate,'yyyy') year,

to_char(hiredate,'mm') months,

to_char(hiredate,'dd') day

from emp;

 

select empno,ename,to_char(sal,'$99,999') from emp;  

   select empno,ename,to_char(sal,'L99,999') from emp;  

 

select to_number('123')+to_number('123') from dual;

 

   select to_date('2016-02-25','yyyy-mm-dd') from dual;

   

  

|===========================================================================|

   |************************通用函数: NVL函数,DECODE函数*************************|

   |===========================================================================|

 

==求出每个雇员的年薪==    nvl函数=将指定的null值变为指定的内容

select empno,ename,nvl(comm,0),(sal+nvl(comm,0))*12 income from emp;

 

====验证decode函数,类似于if-else语句  ====

select decode(1,1,'内容=1',2,'内容=2',3,'内容=3') from dual;

 

总结:

1,oracle的主要用户:

超级管理员:sys/manager

普通管理员:system/change_on_install

普通用户:scott/tiger

 

2,sqlplus的一些常用命令:

set linesize 300

set pagesize 30

ed a    /  @a

conn sys/manager as sysdba

 

3,sql基础语法的格式

4,单行函数,decode函数最重要!

 

原文地址:https://www.cnblogs.com/askDing/p/5217396.html