阶段五 oracle 数据库详解 1

orcale数据库详解

1.Oralce简介

oracle 俗称甲骨文 一般大公司使用 小公司使用mysql

功能:存储 大量数据 进行增删改查

2.oracle的安装

Linux :一般由运维进行安装 我安装了几次都会莫名其妙保错 我感觉是我linux系统的问题

windos:csdn有

3.oracle了解

1.oralce体系

了解客户端提交代码的流程

2.oralce基本概念

逻辑概念:

物理概念:

段 区 块

数据库逻辑图

数据库 ->表空间->逻辑对象 .....

3.基本元素

方案 表 视图 存储过程 序列 索引 同义词

4.sqlplus

输入 sqlplus +用户名 scott 密码

///SQL>conn scott/tiger 连接成功

5.scott方案

TNAME TABTYPE CLUSTERID


l 员工信息表EMP(Employee)

l 部门表DEPT(Department)

l 工资等级表SALGRADE

l 奖金表BONUS

6.sql语言介绍

dml

增删查改

ddl

数据定义语言

dcl

数据控制

7.基本查语句 select

1.基础查询

1.select * from emp;

2.select empno, ename, sal, sal * 12

​ from emp; 有了表达式

3.select empno as "哈哈" ,ename"姓名",sal 月星,sal*12 年星 from emp
2 ; 替换列名 ->换成别名

4.select DISTINCT deptno from emp; 去重

5.edit 进行记事本直接修改

2.算数运算

select deptno,ename,sal,sal12,comm,comm+sal12 from emp;

计算年工资加上奖金后的

3.空值问题

如上 计算年工资报错 因为任何运算碰到Null都变空

空值是无效的,未指定的,未知的或不可预知的值

滤空函数:nvl(a, b) 如果a为NULL, 函数返回b。所以:sal * 12 + nvl(comm, 0) 年收入。

4.过滤和排序

where过滤

select * from emp where deptno=10; 部门为10

日期格式

select * from emp where hiredate='17-11月-81';

修改日期格式

SQL> select * from v$nls_parameters (数据字典,类似于tab)

alter session set NLS_DATE_FORMAT='yyyy-mm-dd';

select *from emp where hiredate='1981-11-17';

5.比较运算

普通比较运算符:

= 等于(不是==) > 大于

>= 大于等于 < 小于

<= 小于等于 <> 不等于(也可以是!=)

1.查询薪水不等于1250的员工信息:

select *from emp where sal<>1250;

2.查询工资在1000-2000之间

1.select *from emp where sal>=1000 and sal<2000;

2.between and:

select * from emp where sal between 1000 and 2000;

注意:1.包含边界值(闭区间) 2. 小值在前,大值在后。 (对于日期也是如此)

6. in not in

查询部门号为10或20的员工信息:

select *from emp where deptno in(10,20);

查询30

select *from emp where deptno not in(10,20);

not in ->空值是否允许 not in &&与运算 导致永远假

in-> or || 有假也无所谓

7.like 模糊查询

l ‘%’匹配任意多个字符。

l ‘_’匹配一个字符。

查询名字以S开头的员工:

select *from emp where ename like'S%';

查询名字中包含_的员工:

select * from emp where ename like '%_% ' escape '\'

转义单引号本身:

SQL> select 'hello '' world' from dual 使用两个单引号来完成转义。

8.逻辑运算

AND 逻辑与

OR 逻辑或

NOT 逻辑非

9.order by排序

l 使用 ORDER BY 子句排序

• ASC(ascend): 升序。默认采用升序方式。

• DESC(descend): 降序

l ORDER BY 子句在SELECT语句的结尾。

SELECT last_name, job_id, department_id, hire_date

FROM employees

ORDER BY hiredate ;

1.查询员工信息,按月薪排序:

select *from emp order by sal;

2.order by + 列名,表达式,别名,序号。 注意:语法要求order by 子句应放在select的结尾。

select ename ,sal,sal12 from emp order by sal12 desc;

3.多条件排序

如果:SQL> select * from emp order by deptno, sal 会怎么样排序呢?

order by后有多列时,列名之间用逗号隔分,order by会同时作用于多列。上例的运行结果会在同一部门内升序,部门间再升序。

4.空值影响排序

​ 结果前面的值为NULL, 数据在后面,如果是一个100页的报表,这样显示肯定不正确。较为人性化的显示应该将空值放在最后, 即:

SQL> select * from emp order by comm desc nulls last (注意:是nulls 而不是null)

10.单行函数

单行函数:只对一行数据进行计算,产生一个结果。函数可以没有参数,但必须要有返回值。如:concat、nvl

11.字符函数

lower、upper、initcap:

select lower('HeLlo,WORld') from dual;

concat(a,b):连接字符串a和b,只能连接两个

select concat('hello','world') as "你好" from dual;

注意:SQL双引号“”表示别名,使用‘’来表示字符串。

另一种连接字符串的方法,使用||,可以做到多个字符串连接

select 'hello' || ' world' || ' Oracle' as "你好" from dual

substr(a, b):从a中,第b位开始取(计数从1开始),取到结尾

SQL> select substr('helloworld', 3) from dual

substr(a, b, c):从a中,第b位开始,向右取c位。

SQL> select substr('hello world', 3, 5) from dual

posted @ 2023-07-31 22:53  大橘|博客  阅读(20)  评论(0)    收藏  举报