一、下是oracle中的一些基本定义:
1、数据库:oracle数据库就是数据的物理存储,这就包括(数据文件ORA或者DBF、控制文件、联机日志、参数文件)。其实Oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库。可以看作是Oracle就只有一个大数据库;
2、Oracle Instance(实例):由一系列的后台进程(Backguound Processes)和内存结构(Memory Structures)组成。一个数据库可以有n个实例;
3、用户:用户是在数据实例下面建立的,不同实例可以创建相同名称的用户;
4、表空间:是一个用来管理数据存储的逻辑概念,表空间知识和数据文件(ORA或者DBF)发生关系,数据库文件是物理的是物理的,一个表空间可以包含多个数据文件,而一个数据文件只能属于一个表空间;
5、数据文件(dbf、ora):数据文件是数据库的物理存储单位,数据库的数据是存在表空间里面的,而实际上是存放在一个或者多个数据文件当中,数据文件一旦创立就不能删除,如果需要删除,只能删除数据文件对应的表空间;
关系示意图:
二、数据库操作
1、创建数据库
create database databasename
2、删除数据库
drop database dbname
3、备份数据库
完全备份
exp demo/demo@orcl buffer=1024 file=d:\back.dmp full=y
demo:用户名、密码
buffer: 缓存大小
file: 具体的备份文件地址
full: 是否导出全部文件
ignore: 忽略错误,如果表已经存在,则也是覆盖
将数据库中system用户与sys用户的表导出
exp demo/demo@orcl file=d:\backup\1.dmp owner=(system,sys)
导出指定的表
exp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)
按过滤条件,导出
exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=\" where filed1 like 'fg%'\"
导出时可以进行压缩;命令后面 加上 compress=y ;如果需要日志,后面: log=d:\log.txt
备份远程服务器的数据库
exp 用户名/密码@远程的IP:端口/实例 file=存放的位置:\文件名称.dmp full=y
4、数据库还原
打开cmd直接执行如下命令,不用再登陆sqlplus。
完整还原
imp demo/demo@orcl file=d:\back.dmp full=y ignore=y log=D:\implog.txt
指定log很重要,便于分析错误进行补救。
导入指定表
imp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)
还原到远程服务器
imp 用户名/密码@远程的IP:端口/实例 file=存放的位置:\文件名称.dmp full=y
三、Oracle表操作
1、创建表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:select * into table_new from table_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle>
2、删除表
drop table tabname
3、重命名表
说明:alter table 表名 rename to 新表名
eg:alter table tablename rename to newtablename
4、增加字段
说明:alter table 表名 add (字段名 字段类型 默认值 是否为空);
例:alter table tablename add (ID int);
eg:alter table tablename add (ID varchar2(30) default '空' not null);
5、修改字段
说明:alter table 表名 modify (字段名 字段类型 默认值 是否为空);
eg:alter table tablename modify (ID number(4));
6、重名字段
说明:alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)
eg:alter table tablename rename column ID to newID;
7、删除字段
说明:alter table 表名 drop column 字段名;
eg:alter table tablename drop column ID;
8、添加主键
alter table 表名 add constraint 约束名 primary key(id);
9、删除主键
alter table 表名 drop constraint 约束名;
10、创建索引
创建一般索引
create index 索引名 on user(创建索引的字段);
创建唯一索引(主键oracle默认为其创建唯一索引)
create unique 索引名 on user(创建索引的字段);
11、删除索引
drop index 索引名
注:索引是不可更改的,想更改必须删除重新建。
12、表名注释
comment on table user is ‘用户’;
13、字段注释
comment on column user.name is ‘用户名’;
12、创建视图
create view viewname as select statement
13、删除视图
drop view viewname
四、Oracle操作数据
1、数据查询
select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]
2、插入数据
insert into 表名 values(所有列的值);
insert into test values(1,'zhangsan',20);
insert into 表名(列) values(对应的值);
insert into test(id,name) values(2,'lisi');
3、更新数据
update 表 set 列=新的值 [where 条件] -->更新满足条件的记录
update test set name='zhangsan2' where name='zhangsan'
update 表 set 列=新的值 -->更新所有的数据
update test set age =20;
4、删除数据
delete from 表名 where 条件 -->删除满足条件的记录
delete from test where id = 1;
delete from test -->删除所有
commit; -->提交数据
rollback; -->回滚数据
delete方式可以恢复删除的数据,但是提交了,就没办法了 delete删除的时候,会记录日志 -->删除会很慢很慢
truncate table 表名
删除所有数据,不会影响表结构,不会记录日志,数据不能恢复 -->删除很快
drop table 表名
删除所有数据,包括表结构一并删除,不会记录日志,数据不能恢复-->删除很快
5、数据复制
表数据复制
insert into table1 (select * from table2);
复制表结构
create table table1 select * from table2 where 1>1;
复制表结构和数据
create table table1 select * from table2;
复制指定字段
create table table1 as select id, name from table2 where 1>1;
6、删除所有数据
truncate table user;
delete from user;
7、增加字段
alter table user add (createdate date);
8、修改字段类型
alter table user modify(createdate varchar2(20));
9、删除字段
alter table user drop column createdate;
10、修改字段名称
alter table user rename column name to username;
11、创建序列
create sequence s_user_id start with 1 increment by 1
nomaxvalue nominvalue nocycle nocache;–自增序列
12、删除序列
drop sequence s_user_id;
13、查询序列
—直接用会报为在当前会话中定义
select s_user_id.currval from dual;
/*
currval 表示序列的当前值,新序列必须使用一次nextval 才能获取到值,否则会报错
nextval 表示序列的下一个值。新序列首次使用时获取的是该序列的初始值,
从第二次使用时 开始按照设置的步进递增
*/
14、视图
视图就是封装了一条复杂查询的语句
语法 1.:CREATE VIEW 视图名称 AS 子查询
语法 2:CREATE OR REPLACE VIEW 视图名称 AS 子查询
语法 3:CREATE OR REPLACE VIEW 视图名称 AS 子查询 WITH READ ONLY
demo:
---查询语句创建表
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;
---视图的作用?
---第一:视图可以屏蔽掉一些敏感字段。
---第二:保证总部和分部数据及时统一。
物化视图:会在数据库中真正找到一张表,而普通视图不是
15、索引
索引是用于加速数据存取的数据对象。合理的使用索引可以大大降低 i/o 次数,从而提高数据访问性能。
demo:
---索引
--索引的概念:索引就是在表的列上构建一个二叉树
----达到大幅度提高查询效率的目的,但是索引会影响增删改的效率。
---单列索引
---创建单列索引
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';---触发单列索引
五、PL/SQL
1、pl/sql
---pl/sql编程语言
---pl/sql编程语言是对sql语言的扩展,使得sql语言具有过程化编程的特性。
---pl/sql编程语言比一般的过程化编程语言,更加灵活高效。
---pl/sql编程语言主要用来编写存储过程和存储函数等。
---声明方法
---赋值操作可以使用:=也可以使用into查询语句赋值
declare
i number(2) := 10;
s varchar2(10) := '小明';
ena emp.ename%type;---引用型变量
emprow emp%rowtype;---记录型变量
begin
dbms_output.put_line(i);
dbms_output.put_line(s);
select ename into ena from emp where empno = 7788;
dbms_output.put_line(ena);
select * into emprow from emp where empno = 7788;
dbms_output.put_line(emprow.ename || '的工作为:' || emprow.job);
end;
---pl/sql中的if判断
---输入小于18的数字,输出未成年
---输入大于18小于40的数字,输出中年人
---输入大于40的数字,输出老年人
declare
i number(3) := ⅈ
begin
if i<18 then
dbms_output.put_line('未成年');
elsif i<40 then
dbms_output.put_line('中年人');
else
dbms_output.put_line('老年人');
end if;
end;
---pl/sql中的loop循环
---用三种方式输出1到10是个数字
---while循环
declare
i number(2) := 1;
begin
while i<11 loop
dbms_output.put_line(i);
i := i+1;
end loop;
end;
---exit循环
declare
i number(2) := 1;
begin
loop
exit when i>10;
dbms_output.put_line(i);
i := i+1;
end loop;
end;
---for循环
declare
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
---游标:可以存放多个对象,多行记录。
---输出emp表中所有员工的姓名
declare
cursor c1 is select * from emp;
emprow emp%rowtype;
begin
open c1;
loop
fetch c1 into emprow;
exit when c1%notfound;
dbms_output.put_line(emprow.ename);
end loop;
close c1;
end;
-----给指定部门员工涨工资
declare
cursor c2(eno emp.deptno%type)
is select empno from emp where deptno = eno;
en emp.empno%type;
begin
open c2(10);
loop
fetch c2 into en;
exit when c2%notfound;
update emp set sal=sal+100 where empno=en;
commit;
end loop;
close c2;
end;
----查询10号部门员工信息
select * from emp where deptno = 10;
2、存储过程与存储函数
--存储过程
--存储过程:存储过程就是提前已经编译好的一段pl/sql语言,放置在数据库端
--------可以直接被调用。这一段pl/sql一般都是固定步骤的业务。
----给指定员工涨100块钱
create or replace procedure p1(eno emp.empno%type)
is
begin
update emp set sal=sal+100 where empno = eno;
commit;
end;
select * from emp where empno = 7788;
----测试p1
declare
begin
p1(7788);
end;
----通过存储函数实现计算指定员工的年薪
----存储过程和存储函数的参数都不能带长度
----存储函数的返回值类型不能带长度
create or replace function f_yearsal(eno emp.empno%type) return number
is
s number(10);
begin
select sal*12+nvl(comm, 0) into s from emp where empno = eno;
return s;
end;
----测试f_yearsal
----存储函数在调用的时候,返回值需要接收。
declare
s number(10);
begin
s := f_yearsal(7788);
dbms_output.put_line(s);
end;
---out类型参数如何使用
---使用存储过程来算年薪
create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
is
s number(10);
c emp.comm%type;
begin
select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
yearsal := s+c;
end;
---测试p_yearsal
declare
yearsal number(10);
begin
p_yearsal(7788, yearsal);
dbms_output.put_line(yearsal);
end;
----in和out类型参数的区别是什么?
---凡是涉及到into查询语句赋值或者:=赋值操作的参数,都必须使用out来修饰。
---存储过程和存储函数的区别
---语法区别:关键字不一样,
------------存储函数比存储过程多了两个return。
---本质区别:存储函数有返回值,而存储过程没有返回值。
----------如果存储过程想实现有返回值的业务,我们就必须使用out类型的参数。
----------即便是存储过程使用了out类型的参数,起本质也不是真的有了返回值,
----------而是在存储过程内部给out类型参数赋值,在执行完毕后,我们直接拿到输出类型参数的值。
----我们可以使用存储函数有返回值的特性,来自定义函数。
----而存储过程不能用来自定义函数。
----案例需求:查询出员工姓名,员工所在部门名称。
----案例准备工作:把scott用户下的dept表复制到当前用户下。
create table dept as select * from scott.dept;
----使用传统方式来实现案例需求
select e.ename, d.dname
from emp e, dept d
where e.deptno=d.deptno;
----使用存储函数来实现提供一个部门编号,输出一个部门名称。
create or replace function fdna(dno dept.deptno%type) return dept.dname%type
is
dna dept.dname%type;
begin
select dname into dna from dept where deptno = dno;
return dna;
end;
---使用fdna存储函数来实现案例需求:查询出员工姓名,员工所在部门名称。
select e.ename, fdna(e.deptno)
from emp e;
3、触发器
---触发器,就是制定一个规则,在我们做增删改操作的时候,
----只要满足该规则,自动触发,无需调用。
----语句级触发器:不包含有for each row的触发器。
----行级触发器:包含有for each row的就是行级触发器。
-----------加for each row是为了使用:old或者:new对象或者一行记录。
---语句级触发器
----插入一条记录,输出一个新员工入职
create or replace trigger t1
after
insert
on person
declare
begin
dbms_output.put_line('一个新员工入职');
end;
---触发t1
insert into person values (1, '小红');
commit;
select * from person;
---行级别触发器
---不能给员工降薪
---raise_application_error(-20001~-20999之间, '错误提示信息');
create or replace trigger t2
before
update
on emp
for each row
declare
begin
if :old.sal>:new.sal then
raise_application_error(-20001, '不能给员工降薪');
end if;
end;
----触发t2
select * from emp where empno = 7788;
update emp set sal=sal-1 where empno = 7788;
commit;
----触发器实现主键自增。【行级触发器】
---分析:在用户做插入操作的之前,拿到即将插入的数据,
------给该数据中的主键列赋值。
create or replace trigger auid
before
insert
on person
for each row
declare
begin
select s_person.nextval into :new.pid from dual;
end;
--查询person表数据
select * from person;
---使用auid实现主键自增
insert into person (pname) values ('a');
commit;
insert into person values (1, 'b');
commit;
4、java调用oracle
jar包对应关系
----oracle10g ojdbc14.jar
----oracle11g ojdbc6.jar
简单的操作实例
import oracle.jdbc.driver.OracleTypes;
import org.junit.Test;
import java.sql.*;
/**
* Created by SunYuqin in 2018/11/3
* Code without comments is soulless
**/
public class OracleTest {
//简单测试连接
@Test
public void test() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.10:1521:orcl", "sun", "root");
PreparedStatement pstmt = conn.prepareStatement("select * from person where id=?");
pstmt.setObject(1,1);
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
System.out.println(rs.getString("name"));
}
rs.close();
pstmt.close();
conn.close();
}
//测试存储函数
@Test
public void testfunction() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.10:1521:orcl", "sun", "root");
CallableStatement pstmt = conn.prepareCall("{?=call f_yearsal(?)}");
pstmt.setObject(2,7788);
pstmt.registerOutParameter(1,OracleTypes.NUMBER);
pstmt.execute();
System.out.println(pstmt.getObject(1));
pstmt.close();
conn.close();
}
//测试存储过程
@Test
public void testprocedure() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.10:1521:orcl", "sun", "root");
CallableStatement pstmt = conn.prepareCall("{call p_yearsal(?, ?)}");
pstmt.setObject(1,7788);
pstmt.registerOutParameter(2, OracleTypes.NUMBER);
pstmt.execute();
System.out.println(pstmt.getObject(2));
pstmt.close();
conn.close();
}
}