1、视图 1.1、什么是视图 视图就是一个虚拟表,实体表的映射。 什么时候使用视图 在开发中,有一些表结构是不希望过多的人去接触,就把实体表映射为一个视图。(表结构简化) 在项目过程中,程序员主要关注编码的性能、业务分析这方面。对
-- 语法
create view 视图名称 as 查询语句;
-- 语法
drop view 视图名称;
-- 0.准备数据,查询业主表
select * from t_owners;
-- 1 创建视图
create view view_owners
as select * from t_owners;
-- 2 删除视图
drop view view_owners;
-- 3 查询 -- 与查询表语法相同
select * from view_owners;
-- 4 增删改
insert into view_owners values(11,"美廉美超市666",5,"4-2","30423",to_date("2016-10-12","yyyy-MM-dd"),3 );
commit;
update view_owners set name = "大润发" where id = 11;
commit;
delete from view_owners where id = 11;
commit;
-- 含条件的视图
-- 0 准备工作
select * from t_owners where ownertypeid = 1
-- 1 创建视图
create or replace view view_owner1
as select * from t_owners where ownertypeid = 1
with check option
-- 2 查询
select * from view_owner1;
-- 3 添加数据
-- 执行不成功,提供违反约束
insert into view_owner1 values(11,"美廉美超市666",5,"4-2","30423",to_date("2016-10-12","yyyy-MM-dd"),3 );
commit;
-- 执行成功
insert into view_owner1 values(11,"美廉美超市666",5,"4-2","30423",to_date("2016-10-12","yyyy-MM-dd"),1 );
commit;
delete from t_owners where id = 11;
commit;
--- 强制创建
-- 0 准备工作,查询表数据,表不存在
select * from temp;
-- 1 创建视图
create force view view_owners2
as select * from temp;
-- 只读视图
-- 0.准备数据,查询业主表
select * from t_owners;
-- 1 创建只读视图
create view view_owners3
as select * from t_owners
with read only
-- 2 查询
select * from view_owners3
-- 3 添加 --不允许
insert into view_owners3 values(11,"美廉美超市666",5,"4-2","30423",to_date("2016-10-12","yyyy-MM-dd"),1 );
commit;
-- 关联查询(复杂视图)
-- 0 准备sql
select ow.id 业主编号,ow.name 业主名称,ot.name 类型名称 from t_owners ow
inner join t_ownertype ot on ow.ownertypeid = ot.id
-- 1 创建视图
create or replace view view_owners4
as select ow.id 业主编号,ow.name 业主名称,ot.name 类型名称 from t_owners ow
inner join t_ownertype ot on ow.ownertypeid = ot.id
-- 2 查询数据
select * from view_owners4
-- 3 更新数据
-- 3.1 键保留表
update view_owners4 set 业主名称 = "王小强" where 业主编号 = 2;
commit;
-- 3.2 非键保留表 (非键值保存表)--执行不成功
update view_owners4 set 类型名称 = "行政事业单位" where 业主编号 = 2;
commit;
create [or replace] [force] view 视图名称
as 查询语句
[with check option]
[with read only]
-- or replace 覆盖
-- force 强制
-- with check option 有效数据检查
-- with read only 只读视图
create sequence 序列号;
create sequence 序列号
start with 起始值
increment by 步长
maxvalue 最大值
minvalue 最小值
-- 简化版
create sequence seq_stuno;
-- 完整版
create sequence seq_stuno2
start with 2
increment by 3
maxvalue 20
minvalue 2
-- 查询 , nextval 获得下一个序列
select seq_stuno2.nextval from dual;
-- 表主键自动维护
create table t_demo(
id int primary key,
name varchar2(50)
);
insert into t_demo(id,name) values(seq_stuno.nextval , "张三");
insert into t_demo(id,name) values(seq_stuno.nextval , "李四");
insert into t_demo(id,name) values(seq_stuno.nextval , "王五");
commit;
创建
私有同义词
create synonym 同义词名称 for 表名|视图名
公有同义词
create public synonym 同义词名称 for 表名|视图名
删除
私有同义词
drop synonym 同义词名称
公有同义词
drop public synonym 同义词名称
--- 同义词
-- 创建私有同义词
create synonym owners for t_owners;
-- 创建完成后,可以查询到数据
select * from owners;
-- 创建共有的同义词
create public synonym owners2 for t_owners;
select * from owners2;
--- 删除
drop synonym owners;
drop public synonym owners2
create index 索引名称 on 表名(列名);
-- 1 准备数据
-- 1.1 创建表
drop table t_indextest;
create table t_indextest(
id number,
name varchar2(30)
);
-- 1.2 批量导入100万条数据
begin
for i in 1..1000000
loop
insert into t_indextest(id,name) values(i, "AA" || i);
end loop;
commit;
end;
-- 2 测试
-- 2.1 查询(id,name) -- 两个结果基本一样
select * from t_indextest where id = 765432;
select * from t_indextest where name = "AA765432";
-- 2.2 添加索引
create index t_indextest_name on t_indextest(name);
-- 2.3 再次查询 -- 有索引快
select * from t_indextest where id = 765432;
select * from t_indextest where name = "AA765432";
--- 执行计划
explain plan for select * from t_indextest where id = 765432;
select * from table(dbms_xplan.display());
explain plan for select * from t_indextest where name = "AA765432";
select * from table(dbms_xplan.display());
create unique index 索引名称 on 表名(列名);
create index 索引名称 on 表名(列名,列名2,.....);
--结束END--
本文标题: Oracle学习(五) --- 视图、序列、同义词、索引
本文链接: https://lsjlt.com/news/6415.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0