您的位置:首页 > 数据库 > Oracle

Day59-Oracle03 - 创建表空间、创建表(子查询创建表)、表的约束、事务、数据库对象(视图、序列、索引、同义词)、PLSQL编程、(if,循环)、数据的导入导出备份

2017-08-30 21:42 1261 查看

DDL语句管理表 DML管理表数据

1)语法: 创建 和 删除 表空间 / 创建用户

create tablespace 表空间的名称
datafile '文件路径'   //这个文件是存储在服务器端的电脑上的。
size 初始大小
autoextend on  --打开自动扩容
next 每次扩容大小


例子:

– 创建表空间,前提是需要切换到管理员帐号,比如system,ps:root

create tablespace xiongan
datafile 'c:\xiongan.dbf'
size 100m
autoextend on
next 10m;


创建完表空间之后,是需要创建用户来管理和操作这个表空间的。

语法: 创建用户
create user 用户名
identified by  密码
default tablespace 表空间的名称


例子:

create user zhangsan
identified by zhangsan
default tablespace xiongan


创建完用户之后,现在还是不能够登录的,因为现在的用户是没有权限的,需要对用户进行授权

Oracle中已经存在三个重要的角色,connect角色,resource角色,dba角色

grant 角色 to 用户名


例子:

grant dba to zhangsan


因为我们的授予zhangsan用户的是dba的权限,所以可以访问和修改其他角色
select * from scott.emp;




删除表空间 ,在逻辑上断开连接

drop tablespace xiongan;


2)创建表

create table 表名(
列名1 列的类型 [列的约束],
列名2 列的类型 [列的约束]
);


2.1)列的类型

number(总长度,小数长度)  :数字 小数长度不能大于总长度number(2,3)
varchar2(长度)  :可变长度字符      varchar2(10) hello , 只存5个字符
char(长度)   :   固定长度字符  char(10)     hello , 不够长度,用空格
varchar2(5)  hellooooo
date : 年月日时分秒
timestamp : 时间戳, 更加精确,表示的时间更丰富
Long : 存放大文本 2G
Clob : 存放大文本 4G character large object ,大文本数据
Blob : binary large object 二进制大对象 ,存放音频视频


下面这两个可以查询时间和事件戳:

select sysdate from dual;
select current_date from dual;  -- 2017/8/26 15:18:37
select current_timestamp from dual; -- 26-AUG-17 03.19.53.250000 PM +08:00


–前面两种的结果是一样的,current_timestamp显示的信息更加丰富

2.2)使用子查询创建表 / 修改表

-- 使用子查询创建表
create table emp as select * from scott.emp;

-- 复制表,只要结构,不需要数据
create table emp1 as select * from scott.emp where 1=2;

-- 添加列, add,注意添加列的时候,要求表是空表,否则会报错
alter table person add sex varchar2(20) not null;

-- 同时添加多列
alter table person add (
mobile varchar2(11),
address varchar2(20)
);

-- 重定义列的类型 -  modify
alter table person modify sex number;

-- 删除列
alter table person drop column mobile;

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

-- 修改表名
rename person to p1;

-- 删除表
drop table p1;

--删除表中所有数据  -- 先删除整张表,然后再创建一个一模一样表结构的表出来
truncate table person;


删除数据: delete 和 truncate 区别?

delete : 逐条删除数据
DML: 操作表中数据, 支持事务,可以回滚
truncate: 先删除表,再创建表
DDL: 操作结构 , 不支持事务, 不可以回滚
效率高


3)表的约束

表的约束(五大约束):

*
*           主键约束: primary key  非空并且唯一
*           唯一约束: unique 唯一可以为空
*           非空约束: not null 不能为空
*           检查约束: check(条件) 检查插入的数据是否满足条件check(条件)
*
* mysql可以写检查约束,但是无效

*           外键约束: 约束从表中记录必须是参考主表中的记录  product category

约束的优劣势:
好: 可以帮助我们约束插入的数据
不好: 增大数据库的工作压力,效率低,


例子:

-- 创建person表
create table person(
pid number primary key,
pname varchar2(20) unique,
age number not null,
sex varchar2(6) check(sex in('男','女','妖'))
);


外键约束: 约束从表中记录必须是参考主表中的记录 product

category
-- 商品表
create table product(
pid number primary key,
pname varchar2(20),
cno number
);
-- 分类表
create table category(
cid number primary key,
cname varchar2(20)
);

-- 添加外键约束,防止插入的数据没有意义
alter table product add foreign key(cno) references category(cid);

-- 删除category表, 无法删除,存在外界约束的时候是不能够直接删除主表的。
drop table category;
-- 强制删除表: 先删除从表关联的外键约束,再删除自己
drop table category cascade constraint;

-- 级联删除 : 删除主表中的记录的时候,先删除从表中对应的记录,然后再删除主表中的记录
在创建外键的时候就指定外键能够级联删除  -- on delete cascade
alter table product add foreign key(cno) references category(cid) on delete cascade;

-- 删除category中的记录
delete from category where cid = 1;

-- 使用子查询插入数据
insert into emp1 select * from emp where deptno=10;   -- 没有as

-- 修改数据
update product set pname='华为';


4)Oracle中的事务:

事务:  一组操作,要么都成功,要么失败
事务特型ACID: 原子性, 一致性,隔离性,持久性
不考虑事务隔离级别,出现的问题: 脏读,虚读/幻读,不可重复读

MYSQL隔离级别:
READ UNCOMMITTED : 读取未提交, 脏读,虚读/幻读,不可重复读 都会发生
READ COMMITTED   : 读取已提交,虚读/幻读,不可重复读 都会发生

100c9
REPEATABLE READ  : 可重复读 , 虚读/幻读 会发生
SERIALIZABLE     : 串行化, 一个事务在执行的时候,另外一个事务处于等待

Oracle隔离级别:
READ COMMITTED : 默认隔离级别
SERIALIZABLE
READ ONLY : 只读

事务的保存点:
savepoint 保存点

回滚到保存点:
rollback to 保存点

insert into product(aaa);
insert into product(aaa);
insert into product(aaa);
savepoint 保存点名称
insert into product(aaa);
insert into product(aaa);
insert into product(aaa);
rollback to 保存点名称;


例子:

create table stair(
step number primary key
);

declare
begin
insert into stair values(1);
insert into stair values(2);
insert into stair values(3);
savepoint point1;
insert into stair values(4);
insert into stair values(4);
insert into stair values(6);
commit; -- 没有异常就提交
exception
when others then  -- 当发生了其它所有异常
rollback to point1;
commit;
end;

select * from stair;


其他数据库对象 - 视图 、同义词、序列 、 索引

1)视图:

实际上是对查询语句的封装,视图不存储任何数据,所有的数据存放在原来的表中

作用:

1.简化查询语句,封装复杂的查询语句

2.屏蔽表中的细节

语法:

create [or replace] view 视图名称 as 查询语句 [with read only]
or replace:表示如果视图存在,就用新创建的视图替换掉原来的旧视图;
with read only:因为视图不存储数据,如果再视图进行修改,原数据也会修改;
视图的命名一般格式为:view_名称


例子:

create or replace view view_emp1 as select * from emp where deptno =10;


修改:能够修改的前提是在定义视图的时候没有写with read only;

不然会报错:virtual column not allowed here

update view_emp1 set ename='clark' where ename='CLARK';


删除视图:

drop view view_demo02;


– 作用一:对复杂查询语句进行封装

create or replace view  view_year as select sum(cc) "TOTAL",
sum(case yy when '1987' then cc end) "1987",
sum(case yy when '1980' then cc end) "1980",
sum(case yy when '1981' then cc end) "1981",
sum(case yy when '1982' then cc end) "1982"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;

select * from view_year;


– 作用二:屏蔽表中细节,不然表中的部分数据被人看到,提供一个经过筛选的视图即可。

create or replace view view_yuangong as select ename,job,mgr from emp with read only;

select * from view_yuangong;


2)synonym 同义词

-- 给表/视图取一个别名
-- 增大代码被破解的难度
create synonym employee for view_yuangong;
select * from employee;


3)序列

序列: 一组有规律的数, 1,2,3,4,5,6,7,8,9,10..

类似于mysql auto_increment执行结果

完整语法:

create sequence 序列的名称
start with 从几开始
increment by 每次增长多少
minvalue n | nominvalue
maxvalue n | nomaxvalue
cycle | nocycle 循环的
cache n ; 缓存


简便写法:其余参数使用默认值代替:

默认从1开始,每次增加1,没有缓存,没有最大值;

create sequence 序列的名称;


注意:

序列是永不回头向下递增,无论发生异常还是回滚,都是向下递增的。

nextval : 序列中的下一个值
currval : 序列中当前的值, 注意,必须是调用了一次nextval才能使用


例子:

-- 生成一个序列: 1,3,5,7,9,0,2,4,6,8,10,0,2,4,6,8,10......
create sequence seq_test1
start with 1
increment by 2
minvalue 0
maxvalue 10
cycle
cache 5;


使用:

select seq_test1.nextval from dual;
select seq_test1.currval from dual;//要先执行一次 序列.nextval,才能够执行currval

-- 通常情况下创建的序列的写法:
默认从1开始,每次增加1,没有缓存,没有最大值;
create sequence seq_test2;
select seq_test2.nextval from dual;


4) 索引

1.相当于是一本书的目录,能够提高查询效率

2.索引是一种排好序的数据结构

3.只有在数据量比较大的时候,才有必要建立索引。几百万条级

4.索引建立在那些经常被用来作为查询条件的列

语法:

create index 索引的名称 on 表名(列名,....);

drop index 索引名


扩展:
索引是一种排好序的数据结构  ,
Btree : balance Tree  平衡多路查找树

索引的优缺点:
优点: 能够提高查询效率
缺点: 反向影响增删改的效率
什么时候创建索引?
哪些列经常用在查询,或者用在分组,用在排序的时候
索引的创建不是一朝一夕的, 需要根据业务需求不断的更新重新

索引例子:


create table tt(
t1 varchar2(50),
t2 varchar2(50)
);
-- 创建一个序列
create sequence seq_t1;

-- 插入500万条记录

-- 使用PLSQL循环插入数据
declare
-- 声明
begin
-- 业务逻辑
for i in 1..5000000 loop
insert into tt values('t1_'||seq_t1.nextval,'t2_'||i);
end loop;
commit;
end;

-- 在没有创建索引: 查询 where t1 = 't1_3123456'  -- 2.625s
select * from tt where t1 = 't1_3123456';

-- 创建索引
create index tt_t1 on tt(t1);

--创建索引之后: 查询 where t1 = 't1_3123456'   -- 0.020
select * from tt where t1 = 't1_3123456';

-- 在没有创建复合/多列索引   where t1 = 't1_3123456' and t2 = 't2_3123456'  -- 0.022
select * from tt where t1 = 't1_3123456' and t2 = 't2_3123456';

-- 创建一个复合索引: 哪些列经常用来作为查询条件,就需要创建索引
create index tt_t12 on tt(t1,t2);

-- 创建复合/多列索引   where t1 = 't1_3123456' and t2 = 't2_3123456'   --0.021
select * from tt where t1 = 't2_3123456' and t2 = 't2_3123456';


PLSQL编程:

PLSQL编程: Procedure Language

ORACLE对象SQL语言的扩展,能够让开发人员在Sql中编写逻辑代码

类似于java中的 if 条件判断, for循环

语法:

declare
-- 声明部分
变量名 变量类型 := 初始值;
vsal emp.sal%type; 引用型变量//引用emp表中sal的变量类型,在sql中用into来赋值
vrow emp%rowtype;  记录型变量//将查询出来一行记录封装到变量,也在sql用into来赋值
begin
-- 业务逻辑
end;
输出:
dbms_output.put_line('hello PLSQL!'); 相当于是java中的syso




if语句:



declare
i number:=3;
begin
if i=1 then dbms_output.put_line('hello world === 1');
elsif i=2 then dbms_output.put_line('hello world === 2');
else dbms_output.put_line('hello world === 3');
end if;
end;


循环:



declare
i number:=1;
begin
while i<10 loop
dbms_output.put_line(i);
i:= i+1;
end loop;
end;

declare
i number:=1;
begin
for i in 1..20 loop
dbms_output.put_line(i);
end loop;
end;
//注意,for循环这种方式不用写循环参数的自增 i:=i+1,循环体会自动增加的。

declare
i number:=2;
begin
loop
exit when i>15;
dbms_output.put_line(i);
i:=i+1;
end loop;
end;


-- 查询员工工资
declare
vsal emp.sal%type;
begin
select sal into vsal from emp where empno=7369;
dbms_output.put_line('工资'||vsal);
end;

-- 查询7369的所有信息
declare
vrow emp%rowtype;
begin
select * into vrow from emp where empno=7369;
dbms_output.put_line('姓名:'||vrow.ename||' 工资: '||vrow.sal);
end;


数据的导入和导出  以及备份


一)使用cmd命令导入和导出

1)全库导出 、导入

在安装了oracle数据库的电脑上,使用cmd命令整库导出和导入:

打开命令提示符:直接输入如下指令:

full=y 表示全库导出

exp 管理员帐号/密码 full=y

//这种方式会默认在当前目录下面生成一个expdat.dmp的备份文件

exp 管理员帐号/密码 file=’C:\beifen.dmp’ full=y

//在指定的路径下生成指定名称的备份文件;

例如:exp zhangsan/zhangsan full=y

导入:

imp 管理员帐号/密码 file=’C:\beifen.dmp’ full=y

2)按用户导出和导入:

导出:

exp 用户名/密码 owner=用户 file=”

exp 管理员帐号/密码 file=’c:\beifen2.dmp’ owner=帐号

导入:

imp 管理员帐号/密码 file=’c:\beifen2.dmp’ fromuer=帐号

3)按表导出和导入



二 )使用PLSQL Developer导出数据

2.1)导出建表语句



2.2)导出表结构和数据

Tools –>Export Tables





2.3)导如数据



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 事务 索引
相关文章推荐