Oracle数据库入门

--创建表空间
create tablespace oraclestudy
datafile 'E:oraclestudy.dbf'
size 100m
autoextend on
next 10;

--删除表空间
drop tablespace oraclestudy;

--创建用户
create user oraclestudy --用户名
identified by oraclestudy --密码
default tablespace oraclestudy --默认表空间

--给用户授权
--roacle数据库种常用角色
connect--连接角色 基本角色
resource--开发者角色
dba--超级管理员角色

--给 oraclestudy用户添加dba角色
grant dba to oraclestudy;

--切换到oraclestudy用户下


--切换到oraclestudy用户下

--创建一个person表
create table person(
      pid number(20),
      pname varchar2(10)
);

--修改表结构
--添加一列
alter table person add (gender number(1));
--修改列类型
alter table person modify gender char(1);
--修改列名称
alter table person rename column gender to sex;
--删除一列
alter table person drop column sex;

--添加一条记录
insert into person (pid,pname) values(1,'小明');
commit;

--查询表中记录
select * from person

--修改数据
--修改一条记录
update person set pname = '小王' where pid = 1;
commit;

--删除部分数据
delete person where pid = 1 and pname = '小明';
commit;

--三个删除
--删除表中全部记录
delete from person;
--删除表结构
drop table person;
--先删除表,再常见表,效果等同于删除表中数据
--在数据量大的情况下,尤其在表中有索引的情况下,该操作效率高
truncate table person;

--序列:默认从1开始,依次递增,主要用来给主键赋值使用
--序列不属于任何一张表,但是可以逻辑和表做绑定
create sequence s_person;

--dual:虚表,只是为了补全语法,没有任何意义
select s_person.currval from  dual;



--scott用户,密码默认tiger
--解锁scott用户
alter user scott account unlock;  
--解锁用户的密码(此句也可以用来重置密码)
alter user scott identified by tiger; 

--切换到scott用户


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

--字符函数
select upper('yes') from dual;--小写变大写
select lower('YES') from dual;--小写变大写

--数字函数
select round(26.11,1) from dual; --四舍五入
select trunc(28.192,2) from dual; --直接截取位数
select mod(10,3) from dual; --取余

--日期函数
--查询emp表中所有员工距离入职现在几天
select sysdate-e.hiredate from emp e;
--算出明天此刻
select sysdate+1 from dual;
--查询emp表中所有员工距离入职现在几月
select months_between(sysdate,e.hiredate) from emp e;

--转换函数
--日期转字符串
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual; 
--字符串转日期
select to_date('2020-08-09 09:52:55','fm yyyy-mm-dd hh:mi:ss') from dual;
--通用函数
--算出emp表中所有员工的年薪
select e.sal*12+nvl(e.comm,0) from emp e;



--多行函数【聚合函数】:作用于多行,返回一个值
select count(1) from emp e; --查询总记录数
select sum(sal) from emp e; --查询工资总和
select max(sal) from emp e; --查询工资最大值
select min(sal) from emp e; --查询工资最小值 
select avg(sal) from emp e; --查询工资平均值


--分组查询
--查询出每个部门的平均工资
--分组查询中,出现在group by 后面的的原始列,才能出现在select后面 
--没有出现在group by 后面的列,想要出现在select后面,必须加聚合函数。
select e.deptno,avg(e.sal)
from emp e 
group by e.deptno;

--查询每个部门平均工资高于2000的部门信息

select e.deptno,avg(e.sal)
from emp e 
group by e.deptno
having avg(e.sal)>2000;
--注意,所有条件都不能使用别名判断

----where是过滤分组前的数据,having是过滤分组后的数据。
---表现形式:where必须在group by之前,having是在group by之后。

---多表查询中的一些概念
---笛卡尔积
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;

select * from emp;
---查询出员工姓名,员工领导姓名
---自连接:自连接其实就是站在不同的角度把一张表看成多张表。
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, emp e2, dept d1, dept d2
where e1.mgr = e2.empno
and e1.deptno=d1.deptno
and e2.deptno=d2.deptno;



---oracle中专用外连接
select *
from emp e, dept d
where e.deptno(+) = d.deptno;

---自连接:自连接其实就是站在不同的角度把一张表看成多张表。
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, emp e2, dept d1, dept d2
where e1.mgr = e2.empno
and e1.deptno=d1.deptno
and e2.deptno=d2.deptno;


---子查询
---子查询返回一个值
---查询出工资和SCOTT一样的员工信息
select * from emp where sal in
(select sal from emp where ename = 'SCOTT')
---子查询返回一个集合
---查询出工资和10号部门任意员工一样的员工信息
select * from emp where sal in
(select sal from emp where deptno = 10);

---子查询返回一张表
---查询出每个部门最低工资,和最低工资员工姓名,和该员工所在部门名称
---1,先查询出每个部门最低工资
select deptno, min(sal) msal
from emp 
group by deptno;

---2,三表联查,得到最终结果。
select t.deptno, t.msal, e.ename, d.dname
from (select deptno, min(sal) msal
      from emp 
      group by deptno) t, emp e, dept d
where t.deptno = e.deptno
and t.msal = e.sal
and e.deptno = d.deptno;



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

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


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

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

---查询语句创建表
create table emp as select * from scott.emp;
select * from emp;
---创建视图【必须有dba权限】
create view v_emp as select ename, job from emp;
---查询视图
select * from v_emp;
---修改视图[不推荐]
update v_emp set job='CLERK' where ename='ALLEN';
commit;
---创建只读视图
create view v_emp1 as select ename, job from emp with read only;
---视图的作用?
---第一:视图可以屏蔽掉一些敏感字段。
---第二:保证总部和分部数据及时统一。


---索引
--索引的概念:索引就是在表的列上构建一个二叉树
----达到大幅度提高查询效率的目的,但是索引会影响增删改的效率。
---单列索引
---创建单列索引
create index idx_ename on emp(ename);
---单列索引触发规则,条件必须是索引列中的原始值。
---单行函数,模糊查询,都会影响索引的触发。
select * from emp where ename='SCOTT'
---复合索引
---创建复合索引
create index idx_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';---触发单列索引。


oralce插入数据库中文乱码问题解决:
  1.查看服务器端编码
    select userenv(‘language‘) from dual;
    我实际查到的结果为:AMERICAN_AMERICA.ZHS16GBK
  2.执行语句 select * from V$NLS_PARAMETERS
    查看第一行中 PARAMETER 项中为 NLS_LANGUAGE 对应的 VALUE 项中是否和第一步得到的值一样。
    如果不是,需要设置环境变量.
    否则PLSQL客户端使用的编码和服务器端编码不一致,插入中文时就会出现乱码.
  3.设置环境变量
    计算机->属性->高级系统设置->环境变量->新建
    设置变量名:NLS_LANG,变量值:第1步查到的值, 我的是 AMERICAN_AMERICA.ZHS16GBK
  4.重新启动PLSQL,插入数据正常

原文地址:https://www.cnblogs.com/sxblog/p/13463009.html