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函数最重要!
Oracle中sql的基本使用的更多相关文章
- oracle中sql语句的优化
oracle中sql语句的优化 一.执行顺序及优化细则 1.表名顺序优化 (1) 基础表放下面,当两表进行关联时数据量少的表的表名放右边表或视图: Student_info (30000条数据)D ...
- Oracle中SQL语句分类
Oracle中SQL语句分类如下:1.DML语句 insert/delete/update/select/merge/explan plan/lock table2.DDL语句 create/atlt ...
- 转:Oracle中SQL语句执行过程中
Oracle中SQL语句执行过程中,Oracle内部解析原理如下: 1.当一用户第一次提交一个SQL表达式时,Oracle会将这SQL进行Hard parse,这过程有点像程序编译,检查语法.表名.字 ...
- oracle 中SQL 语句开发语法 SELECT INTO含义
oracle 中SQL 语句开发语法 SELECT INTO含义 在ORACLE中SELECT INTO是如何使用的,什么意思?和SQL SERVER的不一样? 和sqlserver的不一样sql ...
- 跟踪oracle中sql语句运行过程及相关知识拓展
select * from v$sqlarea; select * from v$sqlarea where first_load_time>'2010-11-27/09:30:00'; 这种方 ...
- oracle中SQL根据生日日期查询年龄的方法
方法:SELECT Trunc(MONTHS_BETWEEN(SYSDATE,BIRTH_DATE)/12) FROM 某表 Trunc函数在这里对带有小数位数的数字取整数部分: SYSDATE为or ...
- oracle中sql查询语句的执行顺序
查询语句的处理过程主要包含3个阶段:编译.执行.提取数据(sql查询语句的处理主要是由用户进程和服务器进程完成的,其他进程辅助配合) 一.编译parse 在进行编译时服务器进程会将sql语句的正文放入 ...
- Oracle中SQL调优(SQL TUNING)之最权威获取SQL执行计划大全
该文档为根据相关资料整理.总结而成,主要讲解Oracle数据库中,获取SQL语句执行计划的最权威.最正确的方法.步骤,此外,还详细说明了每种方法中可选项的意义及使用方法,以方便大家和自己日常工作中查阅 ...
- Oracle 中sql文件的导入导出
导出 一般导入的时候我用的是命令行 imp c##zs/@orcl fromuser=c##zs touser=c##zs file=D:\java\.dmp ignore=y c##zs 是创建的用 ...
随机推荐
- 使用OCI向Oracle插入Geometry数据
使用C/C++操作Oracle数据库,使用OCI可谓是最强大,当然也是最难的方式.Oracle是一个功能复杂而强大的数据库,它可以很好的支持空间数据(Oracle spatial).如何使用OCI向O ...
- php变量的判空和类型判断
(1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...
- Cannot change network to bridged: There are no un-bridged host network adapters解决方法
首先,在你安装上了虚拟机后要确保你也安装了桥接的协议,这可以通过点击右键“网上邻居”,在其中可以看到有两个虚拟出来的网络一个VMnet1,另一个是VMnet8, 如下图所示. 如果没有安装,可以通过下 ...
- jstl表达式替换某些字符
转自:http://www.yiibai.com/jsp/jstl_function_replace.html fn:replace() 函数替换一个字符串与另一个字符串的所有匹配. 语法 fn:re ...
- Android Toast效果
Android Toast效果是一种提醒方式,在程序中使用一些短小的信息通知用户,过一会儿会自动消失,实现如下: FirstActivity.java package org.elvalad.acti ...
- ubuntu添加共享出错
早上设置一个共享目录share. 右键共享,之后系统自动安装软件samba,之后共享出错: "net usershare"返回错误 255:net usershare: canno ...
- Python 字符串处理大全.
Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- I - Tri Tiling
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status #in ...
- InitGoogleLogging坑爹
google::InitGoogleLogging(argv[0]); //::google::InitGoogleLogging(argv[0]); 加上这句,竟然没有日志