当前位置:网站首页>Oracle -- 视图与序列
Oracle -- 视图与序列
2022-07-06 22:12:00 【攀登程序猿】
视图定义
是从一个或者多个数据表中导出的虚表(虚拟表),视图所对应的数据并不真正的存储在数据库中,比如将一个查询的结果定义为视图,则直接查询视图即可
优点:
(1)简化数据的操作:视图可以简化用户处理数据的方式
(2)着重与特定数据:不必要的数据或敏感数据可以不出现在视图中
(3)安全机制,不同的用户对数据访问权限不一样
(4)提供向后监控性,表改变,视图可以不变
视图操作
特点:
(1)视图不会实际存储,当视图允许修改时,修改视图中的数据,则表中的数据也被修改
(2)只读视图不允许修改数据
(3)带约束的视图
(4)多表关联也可以修改数据,基表的数据会跟随修改
create VIEW classstudent AS SELECT * FROM TBL_STUDENTS
// or replace是视图存在时,替换成新的,with check option是带检查约束的视图
create OR REPLACE VIEW classstudent AS SELECT * FROM TBL_STUDENTS WITH CHECK OPTION
// 创建只读视图
create OR REPLACE VIEW classstudent AS SELECT * FROM TBL_STUDENTS WITH READ ONLY
// 强制视图,即表不存在时创建的视图,用于动态生成表的场景
CREATE force VIEW view_test as SELECT * from tbl_test
// 复杂视图 多表查询,允许修改数据,会键保留表
CREATE VIEW view_student_class AS
SELECT * FROM TBL_STUDENTS JOIN TBL_CLASSES ON TBL_STUDENTS.STU_CID = TBL_CLASSES.CLASS_ID
// 聚合视图,聚合函数必须有列名
CREATE VIEW view_student as
SELECT COUNT(*) as count,STU_AGE as age FROM TBL_STUDENTS GROUP BY STU_AGE
注:
(1)键保留表,对于可更新的视图,在视图中的行和基表中的行之间必须具有一对一的关系,因为这样才能够反向映射到原基础表
(2)聚合函数做视图必须有列名
(3)聚合函数的视图无法修改
物化视图
物化视图是将查询的结果建立副本,占用存储空间,查询效率会高,但是会占用空间,未解决权限问题,后续学习。
更新方式:
COMPLETE 完全更新
FAST 增量更新,根据基表的更新日志,判断视图是否需要更新
ON COMMIT是在基表变化时刷新物化视图
ON COMMON是手动刷新
BUILD IMMEDIATE 创建视图时生成数据
ORA-01031: insufficient privileges
序列
创建一系列数字,mysql的主键自增是直接写的,oracle是需要创建序列的
--创建简单序列
CREATE sequence seq1;
--查询序列的下一个值,执行一次递增一个
SELECT seq1.NEXTval from dual;
--查询序列的当前值
select seq1.currval from dual;
CREATE sequence seq_withMax
maxvalue 20; --创建带有最大值的
SELECT seq_withMax.nextval FROM dual; -- 从一开始,每次递增1,最大到20
-- 跳跃的序列
CREATE sequence seq3
INCREMENT BY 2 -- 默认是1
START WITH 1000 --递增默认是MaxValue,递减是minvalue
maxvalue 1002
minvalue 100
cycle --是否循环 nocycle
cache 10; -- 每次放到内存中10个值,默认使用cache 20,不使用cache时配置为nocache,磁盘中保存到10了,缓存到20了,此时重启服务会从21开始,造成断号,有缓存必须小于一次的循环,即cache默认20*INCREMENT by = 200个,得比cache是10,小于200
select seq3.nextval from dual; -- 从1000开始,每次递增2,到1002后,重新从100开始
CREATE sequence seq4
INCREMENT BY -2
START WITH 1000
maxvalue 1001;
SELECT seq4.nextval FROM dual; -- 从1000开始,每次递减2
alter sequenct可以修改序列,不能修改start with
边栏推荐
- Win11 control panel shortcut key win11 multiple methods to open the control panel
- UltraEdit-32 warm prompt: right association, cancel bak file [easy to understand]
- What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
- SSM+jsp实现仓库管理系统,界面那叫一个优雅
- Win11远程桌面连接怎么打开?Win11远程桌面连接的五种方法
- Fiance donated 500million dollars to female PI, so that she didn't need to apply for projects, recruited 150 scientists, and did scientific research at ease!
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
- A series of shortcut keys for jetbrain pychar
- Win11截图键无法使用怎么办?Win11截图键无法使用的解决方法
- Ssm+jsp realizes the warehouse management system, and the interface is called an elegant interface
猜你喜欢
How to solve the problem of adding RTSP device to easycvr cluster version and prompting server ID error?
Video fusion cloud platform easycvr video Plaza left column list style optimization
[team learning] [34 issues] scratch (Level 2)
[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
程序员上班摸鱼,这么玩才高端!
[team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
Five years of automated testing, and finally into the ByteDance, the annual salary of 30W is not out of reach
MySQL forgot how to change the password
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
随机推荐
The request request is encapsulated in uni app, which is easy to understand
Network Security Learning - Information Collection
DAB-DETR: DYNAMIC ANCHOR BOXES ARE BETTER QUERIES FOR DETR翻译
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
主设备号和次设备号均为0
Why does WordPress open so slowly?
[system management] clear the icon cache of deleted programs in the taskbar
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
sscanf,sscanf_ S and its related usage "suggested collection"
接口自动化测试实践指导(中):接口测试场景有哪些
kivy教程之设置窗体大小和背景(教程含源码)
Zero knowledge private application platform aleo (1) what is aleo
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
JetBrain Pycharm的一系列快捷键
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
buildroot的根文件系统提示“depmod:applt not found”
C#使用西门子S7 协议读写PLC DB块
acwing 843. n-皇后问题
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展