加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle-03

发布时间:2020-12-12 13:46:45 所属栏目:百科 来源:网络整理
导读:数据查询语言----DQL 1、基础查询 ? ? 1)查询员工表 select empno,ename from emp_xu; //查询指定字段(员工号/员工姓名) select * from emp_xu; // * 表示全部字段,查询所有 select * from emp_xu where empno=1005; //有条件查询 ? ? 2)列别名---给列起

数据查询语言----DQL

1、基础查询

? ? 1)查询员工表

select empno,ename from emp_xu; //查询指定字段(员工号/员工姓名)
select * from emp_xu; // * 表示全部字段,查询所有
select * from emp_xu where empno=1005; //有条件查询

? ? 2)列别名---给列起别名

列名 as 列名1
select empno,ename,salary,salary*12 [as] year_sal from emp_xu; //错误

? ? 3)空值 null

? ? ? a. 任何数据类型都可以取空值null(插入数据)

? ? ? b. 空值和任何的数据类型进行算数运算,结果都是null

? ? ? c. 空值和字符数据做连接操作(||),结果相当于空值不存在

例如:计算员工月薪(薪水+奖金)
select ename,bonus,salary+bonus as mon_salary from emp_xu; //错误

?

? ? 4)空值处理函数

? ? ? 语法:nvl(d1,d2); ?//如果d1为空(null),则用d2代替;反之,d1不为空,则直接用d1

select ename,salary+nvl(bonus,0) as mon_salary from emp_xu; //不为空时取bonus,为空取0

? ? ? 注意:空值处理函数的两个参数可以是数值、字符、日期,但是两个参数的数据类型必须一致

? ? 5)插入null

? ? ? 要求:empno 1013;ename ?欧阳锋,其他字段都为空

a.全部字段
insert into emp_xu values(1013,‘欧阳锋‘,null,null);
注意:全部字段的写法,如果最后缺少一个空会报错,null和什么都不写是不同的
b.指定字段
insert into emp_xu(empno,ename) values(1013,‘欧阳锋‘);

? ? 6)连接(拼接)操作 ?||

eg:查询员工信息,将员工的姓名和职位连接在一起
select empno,ename||position from emp_xu;
select empno,ename|| ‘is‘ nvl(position,‘no position‘) meg from emp_xu;

? ? ? 注意:“||”符号表示两个数据进行连接操作,类似于java中的两个字符串之间的“+”号

? ? 7)复制表

select * from emp_xu;
create table temp_emp as select * from emp_xu; //复制表

? ? 8)去重 ?distinct

? ? ? distinct表示去重,必须只能紧跟在select后面

? ? ? eg:查询有哪些部门号

//选择部门表,可能会存在某种情况,在员工表中可能存在某个部门没有员工
select distinct deptno from dept_xu;

? ? ? eg:查询每个部门不重复的职位(不同部门是可以有相同职位)

select distinct deptno,position from emp_xu;

? ? 9)大小写问题

? ? ? 注意:sql语句大小写不敏感(不区分大小写),数据区分大小写(单引号里面的数据是区分大小写)

//查询职位是‘Analyst‘的员工信息
SELECT * from emp_xu where POSITION=‘Analyst‘;
select * from emp_xu where position=‘analyst‘; //找不到,单引号里面的数据是区分大小写

? ? ? 注意:大小写转换函数(用于忽略大小写)

? ? ? ? lower():字符数据转换成小写

? ? ? ? upper():字符数据转换成大写

select lower(‘CD‘) from dual;//dual是虚表,‘cd‘
select * from emp_xu where lower(position)=‘analyst‘;//加上lower就找到了

? ? 10)介于两个数据之间

? ? ? between 低值 and 高值 ?--------> 闭区间 [低值,高值]

eg:查询入职日期在2009年的员工姓名和入职日期
select ename,hiredate from emp_xu where hiredate between ‘01-1月-09‘ and ‘31-12月-09‘;

? ? 11)in 关键字的使用

? ? ? 表示判断在不在列表项中,只要满足其中一个即可

eg:查询职位是‘Manager‘或者‘Analyst‘的员工的姓名的职位
select ename,position from emp_xu where position in(‘Manager‘,‘Analyst‘,null); //关键字 in(表示判断在不在列表项中,只要满足其中一个即可)

? ? ? ? 注意:

? ? ? ? ? ?a. 添加了null对结果没有影响

? ? ? ? ? ?b,其中‘欧阳锋‘的position为空,在结果中并没有出来(两个null不能比较,两个空不等,空值null不能和任何数据类型(包括自己)进行比较,position是null,列表项中有null,判断结果是false)

? ? 12)模糊查询

? ? ? 模糊查询使用‘like‘关键字

? ? ? ? a. %表示0到多个字符

? ? ? ? b. _表示1个字符

eg: 查询员工姓名,包含‘张‘字的员工信息
select * from emp_xu ename like ‘%张%‘;
eg:查询职位中第二个字符是‘a‘的员工姓名和职位
select ename,position from emp_xu where position like ‘_a%‘;
eg:查询当前用户下表名是EMP_开头的表
select table_name from user_tables where upper(table_name) like ‘EMP_%‘ escape ‘‘;//_有特殊含义,要转义,escape指明转义字符注意:如果要查询的数据中有特殊字符(_%),在模糊查询匹配是需要进行转义

? ? 注意:如果要查询的数据中有特殊字符(_%),在模糊查询匹配是需要进行转义

? ? 13)

? ? ??eg:查询哪些员工没有奖金?

select ename,bonus from emp_xu where bonus=null; //为选定行,错误,null不可以与任何数值类型进行比较
select ename,bonus from emp_xu where bonus is null;

? ? ? 注意:测试null值时需要用is null,null不能用等于和不等于(<>)跟任何值进行判断

? ? 14)否定形式

eg:查询哪些员工有奖金
select ename,bonus from emp_xu where bonus is not null;
eg:查询薪水不在5000至10000的员工
select ename,salary from emp_xu where salary not between 5000 and 10000;//不包含临界值
eg:查询不是20号部门也不是30号部门的员工
select ename,deptno from emp_xu where deptno<>20 and deptno<>30;//null值显示不出来
select ename,deptno from emp_xu where deptno not in(20,30);//表示不在列表项中,需要同时满足
select ename,30,null);//错误,为选定行

? ? ? 注意:not in 时,列表项中如果有null,结果就是未选定行报错

? ? ? ? ? ? ? ? ?in 时,列表项中有null,对结果没有影响

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读