Oracle-day1:scott用户、查询、取整、截取、模糊查询、别名——23/8/23

1、scott用户

-- 以system管理员登录锁定scott用户
alter user scott account lock;

-- 以system管理员登录解锁scott用户
alter user scott account unlock;

-- 以system管理员用户设置scott用户密码
alter user scott identfied by tiger;

前提条件:

使用system管理员账号登录了,这时解锁了scott普通用户

在scott用户下,有emp员工信息表

-- 前提:在scott普通账号下,有一个emp表
-- emp 员工信息表
   -- empno 员工编号 number数字类型
   --ename 员工姓名 varchar2字符串类型
   --job 工作岗位 varchar2字符串类型
   --mgr 上级领导 number数字类型
   --hiredate 入职日期 date时间类型
   --sal 工资,number类型
   --comm 奖金,number类型
   --deptno 部门编号 number

后面的内容都基于scott的emp表进行操作

2、DQL操作 -- 查询

       DQL操作:对已有的表进行数据的查询和筛选
       select -- 选择、查询
       select * from 表名; scott用户可以省略
       *:通配符,表示所有列和列名同时存在时,应写为:表名.*

2.1、select 查询语句

-- 1、查询全部数据
-- 此时在管理员账号,查询scott账号下的emp表
select * from scott.emp;
-- 如果当前是scott,可以省略scott
-- select * from emp;

-- 2、查询指定列的数据
-- select 列1,列2 from 表;
select empno,ename from scott.emp;

-- 3、查询满足条件的值
-- 查询工资>3000的人
select empno,ename from scott.emp where sal >= 3000;

select emp.*,sal from scott.emp where sal >= 3000;

2.2、对数字列进行计算

-- 4、对数字列进行运算
/*
   +
   -
   *
   /
   % -- 取余
*/
select emp.*,sal+200,sal-50,sal*1.3,sal/1.2 from scott.emp;
-- emp.*是查询到了emp表的所有列,然后后面就可以拼接其他列,一共查询到12列
-- 对列进行运算,会产生一个新的列
-- 每一列都是独立的!都是独立的重新从原有基础上进行运算,而不是从前一列再运算

2.3、向上ceil/下floor取整

/*
   取整:
       1、ceil() 向上取整
       2、floor() 向下取整
*/
-- 当小数不为零,ceil向上(往大的数走)取整
select emp.*,ceil(sal+200.5) from scott.emp;

-- 当小数不为0,floor往下(往小的数走)取整
select emp.*,floor(sal+200.2) from scott.emp;

-- 如果小数部分为0,ceil和floor直接取整数部分

-- celi和floor并不是四舍五入,如果是-4.1,同时使用celi和floor
-- ceil(-4.1)取到的值是-4(向上取整,往大了取)
-- floor(-4.1)取到的值则是-5(向下取整,往小的取)

2.4、时间date

/*
   时间:date

*/
-- sysdate 系统时间,返回值是 日期
-- current_date 当前时间,返回值是 日期
select sysdate,current_date from dual;

2.5、截取日期

-- 2、截取sal表中的日期:hiredate
/*
   a的值为时间列,n的值可以有以下:
   1、mm - 返回当月第一天
   2、yy - 返回当年第一天
   3、dd - 返回当前年月日
   4、yyyy - 返回当年第一天
   5、d - (星期天)返回当前星期的第一天
   6、hh - 当前时间
   7、mi - 没有秒的精准度
*/
select trunc(hiredate,'dd') from scott.emp;    
select trunc(hiredate,'yy') from scott.emp;

2.6、四舍五入

/*
       四舍五入、截取
       1、round(a,n) 四舍五入,n表示小数位,n省略则四舍五入到整数部分
       2、trunc() 用于截取时间和数字的函数
       3、trunc(a,n) 用于截取时间和数字的函数,n表示小数的位数,n省略表示截取到整数部分
*/
select * from scott.emp;
-- 1、运算 sal工资,乘1.2然后四舍五入
select round(sal * 1.2,2) from scott.emp;

2.7 列别名

/*
       取别名:
       1、给列取别名(列可以是已有的列,也可以是运算生成的列) 在低版本语法中需要加上as
       select age 别名,name 别名
       注意:
              1、as可以省略
              2、列名不能重复
              3、别名建议不重复,别名可以用双引号进行包裹
              4、别名不要使用关键字       
*/
select * from scott.emp;
select ename "名字" from scott.emp; -- 给emp的ename取别名

2.8 表别名

/*
       对表取别名
       表取别名后,出现表名时候必须要使用别名,否则报错
       别名不能重复,表别名可以使用双引号
*/
select * from scott.emp a; -- 这是给表取了个别名a
select * from scott.emp "a+1"; -- 这种别名可以用双引号包括

-- 给表的列取别名
select emp.*,sal/1.2 sal1,trunc(sal/1.2) sal2 from scott.emp;
-- 此时,如果给表取一个别名,这个时候在列的部分使用 emp就不对了
select e.*,sal/1.2 sal1,trunc(sal/1.2) sal2 from scott.emp e;
-- 这个时候emp就应该换成别名来代替了,如果不用别名,提示标识符错误
-- 例如这样
select e.ename from scott.emp e;

2.9 where查询

/*
       带where条件的查询:
       -- 可带有 > < = <= >=  (!+ <>)不等于
       -- 比较运算符两边的数据类型需要一致
*/
-- 精准查询
select * from scott.emp where deptno = 10; -- deptno 部门号
-- 数字类型是可以加上 单引号的
select * from scott.emp where deptno = '10';

-- 范围筛选:> < >= <= 
select * from scott.emp where sal < '1000';

2.10 模糊查询

-- 模糊查询
-- -1、以 A开头的: A%
-- -2、以A结尾的:%A
-- -3、包含A:%A%
-- -4、
-- 语法:查询 where 条件列 like '模糊条件'
-- 找出以A开头的
select * from scott.emp where ename like 'A%';

-- 找出以A结尾的
select * from scott.emp where ename like '%A';

-- 找出包含A的
select * from scott.emp where ename like '%A%';

2.11 用函数来模糊查询

-- 在数据量大的情况下,link的弊端会体现出来
/*
   这个时候,就可以使用到函数substr()
    substr(str,begin,[num])在str中从begin位置开始连续截取num数量的字符
    可以正向和反向截取
    正向从1开始,反向从-1开始
    substr(str,begin)在str中从begin位置开始截取str字符串的末尾,如果没有就返回空值
    字符串的最后一位正向数是字符串的长度 反向数是-1
   1、以A开头:
       substr(字符串,开始的位置,截取的长度) = 'A
       substr(字符串,1,1) = 'A'
   2、以A结尾
       substr('字符串',-1,1) = 'A'
       substr(字符串,-1) = 'A' 从末尾结尾时,后面的1可以省略
*/
-- 语句:查询以A开头
select * from scott.emp where substr(ename,1,1) = 'A';

-- 语句:查询以A结尾
select * from scott.emp where substr(ename,-1,1) = 'A';
select * from scott.emp where substr(ename,-1) = 'A';

/*
      也可以用字符串的长度函数来操作
      substr(字符串,-length(字符串),1) = 'A'
      substr(字符串,length(字符串),1) = 'A'
      substr(字符串,length(字符串)) = 'A'
*/
-- 1、以A开头
select * from scott.emp where substr(ename,-length(ename),1) = 'A';

-- 2、以A结尾
select * from scott.emp where substr(ename,length(ename),1) = 'A';
select * from scott.emp where substr(ename,-length(ename)) = 'A';

-- 如果在这里给列取了别名,那么在 查询条件中使用列别名,就会出错,因为
-- 别名是在查询结束以后才会产生,在查询期间是无效的


--3、查询员工姓名第三位是A的
select * from scott.emp where ename like '__A%';
select ename,substr(ename,3,1) from scott.emp where substr(ename,3,1) = 'A';

-- 4、查询第三位是A第五位是E的记录
select *  from scott.emp where ename like '__A_E%';

select ename,substr(ename,3,1),substr(ename,5,1) from scott.emp 
where 
substr(ename,3,1) = 'A'
and
substr(ename,5,1) = 'E';

2.12 逻辑运算符

/*
   逻辑运算符:
   and:两个条件同时满足,并且
   or:两个条件满足其一,和
   not:不是
   -- 执行顺序:先not、再and,后or
*/

逻辑运算符练习

-- 1、查询10号部门和20号部门的员工信息
select * from scott.emp where deptno = 10 or deptno = 20;

-- 2、查询10号部门中工资超过2000的员工信息(两个条件同时满足条件,并且)
select * from scott.emp where deptno = 10 and sal > 2000;

-- 3、查询10号部门中工资超过2000的员工信息和20号部门且工资小于1000的(两个条件同时满足条件,并且)
select * from scott.emp where deptno = 10 and sal > 2000;
select * from scott.emp where deptno = 10 and sal > 2000 
or deptno = 20 and sal < 1000;

-- 4、查询10号和20号两个部门中,工资都高于2000的员工信息(并且)
select * from scott.emp where (deptno = 10 or deptno = 20) and sal > 2000;


-- 5、查询不是20号部门的员工
select * from scott.emp where deptno <> 20;
select * from scott.emp where deptno != 20;

-- 6、找出工资大于800的员工 CLERK 和工资大于900的销售员 SALESMAN
select * from scott.emp where (job = 'CLERK' and sal > 800) or (job = 'SALESMAN' and sal > 800);


猜你喜欢

转载自blog.csdn.net/qq_63141957/article/details/132459956
今日推荐