Oracle数据库入门基础

1 表空间

1.1 创建表空间

create tablespace cncs --创建名称为'cncs'的表空间
datafile 'c:\cncs.dbf' --创建数据库文件
size 100m --设置该数据文件大小为100M
autoextend on --设置自动文件大小每次自动增长10m
next 10m;

1.2 删除表空间

drop tablespace cncs;

2 用户

注意

创建用户、给用户授权的操作都需要先登录超级管理员或者拥有dba权限的账号。

2.1 创建用户

create user cncs --用户名
identified by cncs --密码
default tablespace cncs; --默认表空间

2.2 给用户授权

--给用户授权(否则无法登录)
--oracle数据库中常用角色
connect --连接角色,基本角色
resource --开发者角色(公司分配)
dba --超级管理员角色

--给cncs用户授予dba角色
grant dba to cncs;

切换到cncs角色

N8dl1U.png

3 表的管理

3.1 建表

--创建一个person表
create table person(
       pid number(20),
       pname varchar2(10)
);
create table person(
       pid number(20),
       pname varchar2(10),
       gender number(1) default(1),
       birthday date
);
insert into person (pid,pname,gender,birthday) values(1,'张三',0,to_date('2020-6-17','yyyy-mm-dd'));
commit;
select * from person;

3.2 修改表结构

--**********************************************************
--修改表结构
--添加一列
alter table person add gender number(1);
--添加多列
alter table person add (phone varchar2(11),address varchar2(20));

--修改列类型
alter table person modify gender char(1);

--修改列名称
alter table person rename column gender to sex;

3.3 删除表

--删除表结构
drop table person;

3.4 删除表结构的一列

--删除一列
alter table person drop column address;
alter table person drop column sex;
alter table person drop column phone;

4 数据的增删改

在使用SQL工具操作数据库,涉及到数据的增删改操作时,需要注意的是:

  1. 通过PL/SQL Developer工具插入数据不会自动提交;
  2. 在项目中使用框架或者使用connection对象,都会自动提交。

4.1 插入一条数据

--数据的增删改
--查询表中记录
select * from person;
--插入一条数据
insert into person (pid,pname) values(1,'张三');
commit;

4.2 三种删除

--三种删除
delete from person where pid = 1;
--删除表中全部记录
delete from person;
--删除表结构
drop table person;
--先删除表,再次创建表。效果等同于删除表中全部数据记录。
--在数据量大的时候,尤其是表带有索引的时候,该操作效率高。
--索引可以提升查询效率,但是会影响增删改效率。
truncate table person;

4.3 修改数据

--修改一条数据
update person set pname = '李四' where pid=2;
commit;

5 序列的使用

5.1 创建序列

--**********************************************************
--序列的使用
--序列:序列不属于任何一张表,但是可以在逻辑上与表产生关联。使用序列时用在自动增长的主键上比较多。
--dual:虚表,存在的意义是为了补全语法。
--创建一个序列(命名:s_表名,s代表sequence)
create sequence s_person;

5.2 添加记录

  1. 获取序列值
select s_person.nextval from dual;--dual是虚表,没有实际意义
select s_person.currval from dual;
  1. 添加一条记录
--s_person.nextval:每次查询会+1
--s_person.currval:每次查询是当前值
insert into person (pid,pname) values(s_person.nextval,'王五');
commit;
select * from person;

6 scott用户

scott用户,用户名:scott,密码:tiger,它拥有几个表可以用来学习和练习函数的操作。

6.1 使用方法

  1. 解锁scott用户

    alter user scott account unlock; 
    
  2. 解锁scott用户的密码

    alter user scott identified by tiger; 
    
  3. 切换到scott用户

7 单行函数

单行函数:作用于一行,返回一个值。

7.1 字符函数

select upper('no') from dual; --返回'NO'
select lower('NOtinG') from dual; --返回'noting'

7.2 数值函数

select round(23.82) from dual; --四舍五入函数
select round(54.435,2) from dual;
select round(54.435,-2) from dual;
select trunc(322.43) from dual; --直接截取,不看后面的数字
select trunc(322.48,1) from dual;
select trunc(322.48,-1) from dual;
select mod(10,4) from dual; --求余数

7.3 日期函数

--查询出emp表中所有员工入职距离现在几天。
select * from emp;
select round(sysdate-e.hiredate) from emp e;

--算出明天此刻
select (sysdate+1) from dual;

--查询出emp表中所有员工入职距离现在几月。
select months_between(sysdate,e.hiredate) from emp e;

--查询出emp表中所有员工入职距离现在几年。
select months_between(sysdate,e.hiredate)/12 from emp e;

--查询出emp表中所有员工入职距离现在几周。
select (sysdate-e.hiredate)/7 from emp e;

7.4 转换函数

--日期转字符串
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;

--字符串转日期
select sysdate from dual;
select to_date('2020-06-13 23:03:34','yyyy-mm-dd hh24:mi:ss') from dual;

7.5 通用函数

--算出emp表中所有员工的年薪
--奖金里面有null值,如果null值和任意数字做算术运算,结果都是null。
select e.sal from emp e;
select  e.sal*12+nvl(e.comm,0) from emp e;

8 条件表达式

8.1 mysql和oracle通用写法的条件表达式

--给emp表中员工起中文名
select e.ename from emp e;
select e.ename,
       case e.ename
         when 'SMITH' then '张三'
           when 'ALLEN' then '樱花'
             when 'WARD' then '小乔'
               --else '姑娘'
                 end             
 from emp e;


---判断emp表中员工工资,如果高于3000显示高收入,如果高于1500低于3000显示中等收入,
-----其余显示低收入
select e.sal from emp e;
select e.sal,
       case 
         when e.sal>3000 then '高收入'
           when e.sal>1500 then '中等收入'
             else '低收入'
               end
 from emp e;

8.2 oracle专用表达式

 --oracle中除了起别名都用单引号,起别名可以省略双引号。
 select e.sal,
          decode( e.ename,
          'SMITH',  '张三',
            'ALLEN' , '樱花',
              'WARD' , '小乔',
               '无名') "中文名"
 from emp e;

9 多行函数

--多行函数【聚合函数】:作用于多行,返回一个值
select count(1) from emp;--求总数,count(*)最后同样走count(1),所以直接写count(1)
select sum(sal) from emp;--求所有员工工资总和
select max(sal) from emp;--求所有员工最多工资
select min(sal) from emp;--求所有员工最少工资
select avg(sal) from emp;--求所有员工平均工资

10 分组查询

---分组查询
---查询出每个部门的平均工资
---分组查询中,出现在group by后面的原始列,才能出现在select后面
---没有出现在group by后面的列,想在select后面,必须加上聚合函数。
---聚合函数有一个特性,可以把多行记录变成一个值。
select e.deptno,avg(e.sal)
from emp e 
group by e.deptno;

---查询出平均工资高于2100的部门信息
select e.deptno,avg(e.sal) esal 
from emp e 
group by e.deptno 
having avg(e.sal)>2100;
---所有条件都不能使用别名来判断。
--比如下面的条件语句也不能使用别名当条件
select ename, sal s from emp where sal>1500;
select ename, sal s from emp where s>1500;--报错,无效参数

--where是过滤分组前的数据,having是过滤分组后的数据。
--表现形式:where必须在group by之前,having是在group by之后。
--查询出每个部门工资高于800的员工的平均工资,然后再查询出平均工资高于2000的部门
select e.deptno,avg(e.sal) esal 
from emp e 
where e.sal>800 
group by e.deptno 
having avg(e.sal)>2000;

11 多表查询

---一些概念,笛卡尔积
select * from emp e,dept d;
--等值连接
select * 
from emp e,dept d
where e.deptno = d.deptno;
---内连接
select *
from emp e inner join dept d
on e.deptno = d.deptno;
---查询出所有部门,以及部门下的员工信息。【外连接】
select *
from emp e right join dept d
on e.deptno = d.deptno;
---查询所有员工信息,以及员工所属部门
select *
from emp e left join dept d
on e.deptno = d.deptno;
--查询出所有部门,以及部门下的员工信息。【外连接】
---*oracle中专用外连接*
--右外连接,'+'号在左,反之。
select *
from emp e,dept d
where e.deptno(+) = d.deptno;

12 自连接

---查询出员工姓名,员工领导姓名
---自连接:自连接其实就是站在不同的角度把一张表看成多张表。
select e1.ename,e2.ename
from emp e1,emp e2
where e1.mgr = e2.empno;
------查询出员工姓名,员工部门名称,员工领导姓名,员工领导部门名称
select e1.ename,d1.dname,e2.ename,d2.dname
from emp e1, dept d1,emp e2,dept d2
where e1.mgr = e2.empno
and e1.deptno = d1.deptno
and e2.deptno = d2.deptno;

13 子查询

---子查询
---子查询返回一个值
---查询出工资和SCOTT一样的员工信息
select * from emp where ename in ('SCOTT');
select *
from emp e
where e.sal = (select sal from emp where ename in ('SCOTT'));
---子查询返回一个集合
---查询出工资和10号部门任意员工一样的员工信息
select sal from emp e where e.deptno = 10;
select *
from emp
where sal in (select sal from emp e where e.deptno = 10);
---子查询返回一张表
---查询出每个部门最低工资,和最低工资员工姓名,和该员工所在部门名称
--1.查询出每个部门最低工资
select min(e.sal) msal
from emp e
group by e.deptno;
--2.再三表(第一步子查询)联查
select d.deptno,msal,e.ename,d.dname
from (select deptno,min(e.sal) msal
from emp e
group by e.deptno) t,emp e,dept d
where t.deptno = e.deptno 
and t.msal = e.sal
and e.deptno = d.deptno;

14 oracle中的分页

--**********************************************************
---rownum行号:当我们做select操作的时候,
--每查询出一行记录,就会在该行上加上一个行号,
--行号从1开始,依次递增,不能跳着走。

----排序操作会影响rownum的顺序
select rownum,e.* from emp e order by sal desc;
----如果涉及到排序,但是还要使用rownum的话,我们可以再次嵌套查询。
select rownum,e.*
from (select * from emp order by sal desc) e;

----emp表工资倒叙排列后,每页五条记录,查询第二页。
----rownum行号不能写上大于一个正数。
select * from (
     select rownum r,tt.* from (
            select * from emp order by sal desc
     ) tt where rownum<11
) where r>5;

15 视图

视图的概念?

视图就是提供一个查询的窗口,所有数据来自于原表。

视图的作用?

  1. 视图可以屏蔽掉一些敏感字段。
  2. 保证总部和分部数据及时统一。

15.1 创建视图

---查询语句创建表
create table emp as select * from scott.emp;
select * from emp;
--创建视图,必须有【dba权限】
create view v_emp as select ename,job from emp; 

15.2 查询视图

--查询视图
select * from v_emp;
--修改视图[不推介,会直接修改到主表数据],[需要commit]
update v_emp set job = 'CLERK' where ename = 'ALLEN';
commit;

--从视图创建视图,最后操作的数据都会作用原来的表
create view v_emp1 as select * from v_emp;
select * from v_emp1;
update v_emp1 set job = 'SLAESMAN' where ename = 'ALLEN';
commit;

15.3 删除视图

--删除视图
drop view v_emp1;

15.4 创建只读视图

create view v_emp1 as select * from v_emp with read only;

16 索引

索引的概念?

索引就是在表的列上构建一个二叉树

索引的作用?

达到大幅度提高查询效率的目的,但是索引会影响增删改的效率。

16.1 单列索引

--单列索引
--创建索引
create index idx_ename on emp(ename);
---单列索引触发规则,条件必须是索引列中的原始值。
---单行函数,模糊查询,都会影响索引的触发。
select * from emp where ename = 'SCOTT';

16.2 复合索引

--复合索引
--创建复合索引
create index idex_enamejob on emp(ename,job);
--触发复合索引
---复合索引中第一列为优先检索列
---如果要触发复合索引,必须包含有优先检索列中的原始值。
select * from emp where ename = 'SCOTT' and job='xx'; --触发复合索引
select * from emp where ename = 'SCOTT' or job = 'xx'; --不触发复合索引
select * from emp where ename = 'SCOTT'; -- 触发单列索引
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值