Oracle详细入门教程
一、Docker安装Oracle,Navicate连接Oracle
1.安装Docker
2.Docker安装Hadoop
3.安装Oracle
3.1 拉取Oracle
[root@hadoop_zxy docker]# docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
Using default tag: latest
latest: Pulling from helowin/oracle_11g
ed5542b8e0e1: Pull complete
a3ed95caeb02: Pull complete
1e8f80d0799e: Pull complete
Digest: sha256:4c12b98372dfcbaafcd9564a37c8d91456090a5c6fb07a4ec18270c9d9ef9726
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g:latest
registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g:latest
3.2 查看镜像
[root@hadoop_zxy docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
6053537/portainer-ce latest 9cb527671ac0 3 weeks ago 265MB
portainer/portainer latest 580c0e4e98b0 9 months ago 79.1MB
registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g latest 3fa112fd3642 6 years ago 6.85GB
3.3 创建容器
[root@hadoop_zxy docker]# docker run -d -p 1521:1521 --name oracle11g registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
26778a7f73a3708a8a351ea56aaa1a8507df9b3ab58cbad9c64433a19d608bee
3.4 进入容器
[root@hadoop_zxy docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26778a7f73a3 registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" About a minute ago Up About a minute 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp oracle11g
[root@hadoop_zxy docker]# docker exec -it 26778a7f73a3 bash
[oracle@26778a7f73a3 /]$
3.5 进行软连接
[oracle@26778a7f73a3 /]$ sqlplus /nolog
bash: sqlplus: command not found
[oracle@26778a7f73a3 /]$ su root
Password:
[root@26778a7f73a3 /]#
在Oracle用户下没有找到sqlplus,切换到root用户下,密码一律为helowin
3.6 配置环境变量
[root@26778a7f73a3 dbhome_2]# vi /etc/profile
[root@26778a7f73a3 dbhome_2]# source /etc/profile
[root@26778a7f73a3 dbhome_2]#
## 在profile末尾添加以下环境变量
export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
export ORACLE_SID=helowin
export PATH=$ORACLE_HOME/bin:$PATH
3.7 创建软连接
## 在/usr/bin目录下创建sqlplus的软连接
[root@26778a7f73a3 dbhome_2]# ln -s /home/oracle/app/oracle/product/11.2.0/dbhome_2/bin/sqlplus /usr/bin
[root@26778a7f73a3 dbhome_2]#
3.8 再次切换到Oracle用户
## 加 - 的目的是为了上一步创建的软连接能够生效
[root@26778a7f73a3 dbhome_2]# su - oracle
[oracle@26778a7f73a3 ~]$
3.9 操作Oracle
3.9.1 登录
[oracle@26778a7f73a3 ~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Sun Jan 9 13:00:11 2022
Copyright (c) 1982, 2009, Oracle. All rights reserved.
SQL>
3.9.2 连接
SQL> conn /as sysdba
Connected.
SQL>
3.9.3 修改账号密码
-- 修改system用户账号密码
SQL> alter user system identified by system;
User altered.
-- 修改sys用户账号密码
SQL> alter user sys identified by system;
User altered.
3.9.4 创建用户
-- 创建内部管理员账号密码
SQL> create user zxy identified by zxy;
User created.
3.9.5 授权
-- 为内部管理元分配dba权限
SQL> grant connect,resource,dba to zxy;
Grant succeeded.
SQL>
3.9.6 修改密码规则
-- 设置密码永不过期
SQL> alter profile default limit password_life_time unlimited;
Profile altered.
SQL>
3.9.7 修改密码最大连接数
SQL> alter system set processes=1000 scope=spfile;
System altered.
SQL>
3.9.8 重启数据库
-- 关闭数据库
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
-- 启动数据库
SQL> startup;
ORACLE instance started.
Total System Global Area 1603411968 bytes
Fixed Size 2213776 bytes
Variable Size 402655344 bytes
Database Buffers 1191182336 bytes
Redo Buffers 7360512 bytes
Database mounted.
Database opened.
SQL>
3.9.9 退出
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@26778a7f73a3 ~]$
3.10 Navicate连接
主要注意的是这里的服务名字需要写helowin,如果安装默认的ORCL会报错
3.11 退出容器
[oracle@26778a7f73a3 /]$ exit
exit
[root@hadoop_zxy docker]# ls或者 ctrl + d
二、Oracle常用函数
1.基础函数
- 1.ASCALL
返回与对应字符串对应的十进制数
select ascaii(A),ascaii(a),ascaii(0),ascaii() from dual; 65 97 48 32
- 2.TO_CHAR
将时间按照指定格式转为char类型
SELECT TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss') FROM dual; 2021-10-21 13:06:32
- 3.TO_DATE
将字符串转化为Oracle中的一个日期
select to_date(2021-10-21,yyyy-mm) from dual;
- 4.TO_NUMBER
转数字
select to_number(2021) from dual;
- 5.USER
返回当前数据库的一个用户
SELECT USER FROM dual;
- 6.MAX(DISTINCT|ALL)
求最大值,ALL表示对所有值求最大值,DISTINCT对相同值去重
select max(distinct sal) from zxy;
- 7.MIN(DISTINCT|ALL)
求最小值,ALL表示对所有值求最小值,DISTINCT对相同值去重
select min(distinct sal) from zxy;
- 8.GROUP BY
主要用来对一组数进行统计
select deptno,count(*),sum(sal) from zxy group by deptno;
- 9.HAVING
对分组统计加限制条件
select deptno,count(*),sum(sal) from zxy group by deptno having count(*) >= 5;
- 10.ORDER BY
用于对查询到的结果进行排序输出
select deptno,ename,sal from zxy order by deptno,sal desc
- 11.CASE…WHEN…END
-- 方法一 select ( case when DUMMY='X' then 0 else 1 end) as flag from dual; -- 方法二 select ( case col when 'a' then 1 when 'b' then 2 else 0 end) from dual;
2.字符串函数
- 1.CHR
给出整数,返回对应的字符串
select chr(54740),chr(65) from dual 赵 A
- 2.CONCAT
连接字符串
select concat(010-,8888) from dual 010-8888
- 3.INITCAP
返回字符串并将字符串的第一个字母变为大写
select initcap(smith) from dual; Smith
- 4.INSTR
在一个字符串中搜索指定的字符,返回发现指定的字符的位置。
select instr(oracle traning,ra,1,2) from dual; 9
- 5.LENGTH
返回字符串的长度
select name,length(name),sal,length(to_char(sal)) from text; zxy 3 999.9 5
- 6.LOWER
将所有字符转成小写
select lower(AbcD) from dual; abcd
- 7.UPPER
将所有字符转成大写
select upper(AbcD) from dual; ABCD
- 8.SUBSTR
取字符串从第几个开始取,取几个
select substr(abcdefg,1,2) from dual; cdefg
- 9.REPLACE
替换字符串中的指定字符
select replace(abcd efd , ef, A) from dual abcd Ad
- 10.TRIM
3.数值函数
- 1.ABS
取绝对值
select abs(-100) from dual 10
- 2.CEIL
返回大于或者等于给出数字的最小整数
selct ceil(2.232) from dual; 3
- 3.FLOOR
对于给定数字给整数
select floor(2.232) from dual; 2
- 4.SQRT
开根号
select sort(64) from dual; 8
4.日期函数
- 1.ADD_MONTHS
增加或减去月份
-- 加上月份 select to_char(add_month(to_date(2021-10-21,yyyy-mm),2),yyyy-mm ) from dual; 2021-12 --减去月份 select to_char(add_month(to_date(2021-10-21,yyyy-mm),2),yyyy-mm) from dual; 2021-10
- 2.SYSDATE
sysdate和current_date效果一样
select sysdate from dual; select current_date from dual;
- 3.LAST_DAY
取当月的最后一天
SELECT LAST_DAY(SYSDATE) FROM dual; 2021-10-31 12:13:43
- 4.MONTHS_BETWEEN
取两个日期时间段之间有几个月
select months_between(TO_DATE('2021-10-21','yyyy-mm-dd') ,TO_DATE('2021-01-21','yyyy-mm-dd')) months from dual;
- 5.NEXT_DAY
设置当前时间,以及星期一,自动求出当前日期的下一个星期一
SELECT NEXT_DAY(SYSDATE ,'星期一') FROM dual; 2021-10-25 12:38:55
5.高级函数
- 1.HEXTORAW
将一个十六机制构成的字符串转换为二进制
- 2.RAWTOHEXT
将一个二进制构成的字符串转换为十六进制
- 3.ROWIDTOCHAR
将ROWID数据类型转为字符类型
- 4.GREATEST
返回一组表达式的最大值,即比较字符的编码大小
select greatest(早,安,周) from dual;
- 5.LEAST
返回一组表达式的最小值,即比较字符的编码大小
select least(早,安,周) from dual;
- 6.NVL
NVL(expr1,expr2) NVL(expr1,expr2)-> expr1为NULL,返回expr2;否则返回expr1.注意两者的类型要一致。 NVL2(expr1,expr2,expr3) -> expr1不为NULL,返回expr2.为NULL,返回expr3;expr2和expr3类型不同的情况下,expr3会转换为expr2的类型 NULLIF(expr1,expr2) -> 相等返回NULL,不等返回expr1
- 7.分析函数
row_number() over(partition by ... order by ...)
rank() over(partition by ... order by ...)
dense_rank() over(partition by ... order by ...)
count() over(partition by ... order by ...)
max() over(partition by ... order by ...)
min() over(partition by ... order by ...)
sum() over(partition by ... order by ...)
avg() over(partition by ... order by ...)
first_value() over(partition by ... order by ...)
last_value() over(partition by ... order by ...)
lag() over(partition by ... order by ...)
lead() over(partition by ... order by ...)
over(order by salary) 安装salary排序进行累计,order by是一个默认的开窗函数
over(partition by deptno) 按照部门分区
over(order by salary range between 50 preceding and 150 following)
--每行对应的数据窗口是之前行幅度值不超过50,之后行幅度值不超过150
over(order by salary rows between 50 preceding and 150 following)
--每行对应的数据窗口是之前50行,之后150行
over(order by salary rows between unbounded preceding and unbounded following)
三、Oracle触发器
需求:使用触发器记录对某一表的insert,update,delete操作
1.创建测试数据库
-- 创建数据库测试表
CREATE TABLE "ZXY"."STUDY" (
"ID" NUMBER(*,0), --id
"NAME" VARCHAR2(20), --名称
"TESTTIME" VARCHAR2(50) --时间
)
2.创建Sequence
-- 创建Sequence
CREATE SEQUENCE seq_study --序列名即计算器的名称,自拟
INCREMENT BY 1 --每次增加幅度为1
START WITH 1 --起始序号设为1,视具体情况定
NOMAXvalue -- 不设置最大值,或设置最大值:maxvalue 9999
NOCYCLE --一直累加不循环,想循环的话设置cycle
CACHE 10; --设置缓存序列个数,如果系统down掉可能会导致序列不连续,可设置为nocache
3.创建存储触发器日志表
-- 创建存储触发器日志
CREATE TABLE "ZXY"."TRIGGERLOG_STUDY" (
"NUM" NUMBER(16,0), --存储数据库操作的唯一序列号
"SQLTEXT" VARCHAR2(2000), --存储数据库详细操作
"OPTFLG" VARCHAR2(2000) --存储操作类型
)
4.创建触发器
-- 创建触发器
create or replace trigger triggerlog_study --创建触发器
after
insert or update or delete --监控insert\update\delete操作
on STUDY
referencing old as old new as new
for each row
declare
NUM int; -- 记录操作唯一序列号
wk_values varchar2(2000); --详细操作
ID VARCHAR2(25); -- 测试数据库字段
NAME VARCHAR2(25);
TESTTIME VARCHAR2(25);
BEGIN
SELECT seq_study.nextval INTO NUM FROM dual; --查询当前的序列号,并赋值给NUM变量
IF :NEW.ID IS null THEN ID := 'null'; ELSE ID := :NEW.ID; END IF;
IF :NEW.NAME IS null THEN NAME := 'null'; ELSE NAME := ''''||:NEW.NAME||''''; END IF;
IF :NEW.TESTTIME IS null THEN TESTTIME := 'null'; ELSE TESTTIME := ''''||:NEW.TESTTIME||''''; END IF;
if inserting THEN -- insert事件
wk_values := 'insert into zxy values('
||ID||','
||NAME||','
||TESTTIME||
');';
INSERT INTO triggerlog_study(num,sqltext,optflg) VALUES(NUM,wk_values,'insert');
elsif updating THEN -- update事件
wk_values := 'delete from zxy where ID = ' || :OLD.ID;
insert into triggerlog_study(num,sqltext,optflg) VALUES(NUM,wk_values,'updating'); -- update先执行delete
SELECT seq_study.nextval INTO NUM FROM dual;
wk_values := '';
wk_values := 'insert into zxy values('
||ID||','
||NAME||','
||TESTTIME||
');';
INSERT INTO triggerlog_study(num,sqltext,optflg) VALUES(NUM,wk_values,'updated'); -- update后执行insert
elsif deleting THEN -- delete事件
wk_values := 'delete from zxy where id = '|| :OLD.ID;
insert into triggerlog_study(num,sqltext,optflg) VALUES(NUM,wk_values,'delete');
end if;
end;
5.测试
5.1 insert测试
--insert测试
insert into study values (1,'zxy','2021-02-02')
5.2 update测试
--update测试
update study set name = 'zhou' where name = 'zxy'
5.3 delete测试
--delete测试
delete from study where name = 'zhou'
5.4 查看触发器存储日志
-- 查看触发器表中存储日志
select * from TRIGGERLOG_STUDY