您好,登錄后才能下訂單哦!
Oracle數(shù)據(jù)庫---對(duì)象中最基本的是表和視圖,其他還有約束、索引、序列、函數(shù)、存儲(chǔ)過程、甚至創(chuàng)建同義詞。對(duì)數(shù)據(jù)庫的操作可以基本歸結(jié)為對(duì)數(shù)據(jù)對(duì)象的操作,因此,在上篇博文講述了基本操作的基礎(chǔ)上,本篇博文將介紹其對(duì)象的基本操作。
- Oracle中表是數(shù)據(jù)存儲(chǔ)的基本結(jié)構(gòu)。隨之引入了分區(qū)表和對(duì)象表,后來又引入了臨時(shí)表,使表的功能更強(qiáng)大。
- 視圖是一個(gè)或多個(gè)表中數(shù)據(jù)的邏輯表達(dá)式。用戶可以將視圖看成一個(gè)存儲(chǔ)查詢(stored query)或一個(gè)虛擬表(virtual table).查詢僅僅存儲(chǔ)在oracle數(shù)據(jù)字典中,實(shí)際的數(shù)據(jù)沒有存放在任何其它地方,所以建立視圖不用消耗其他的空間。視圖也可以隱藏復(fù)雜查詢。
在上一篇博文已詳細(xì)闡述,創(chuàng)建表空間---創(chuàng)建用戶(c##jerry)---創(chuàng)建表(info),表環(huán)境如下:
SQL>create table info
2 (
3 id number(4) constraint PK_id primary key, #constraint :約束
4 name varchar2(10),
5 score number(5,2),
6 born date,
7 address varchar2(50)
8 );
SQL>insert into info values(1,'zhangsan',88,to_date('2018-10-9','yyyy-mm-dd'),'nanjing');
SQL>insert into info values(2,'lisi',77,null,null);
SQL>insert into info values(3,'lwangwu',77,null,null);
SQL>commit;
SQL>create view view_info as select * from info; #創(chuàng)建視圖
select view view_info as select * from info; #查看視圖
drop view view_info; #刪除視圖
物化視圖是包括一個(gè)查詢結(jié)果的數(shù)據(jù)庫對(duì)像,它是遠(yuǎn)程數(shù)據(jù)的的本地副本,或者用來生成基于數(shù)據(jù)表求和的匯總表。物化視圖存儲(chǔ)基于遠(yuǎn)程表的數(shù)據(jù),也可以稱為快照。
SQL>conn sys/abc123 as sysdba #切換到管理員
grant create materialized view to c##jerry; #創(chuàng)建物化視圖
grant query rewrite to c##jerry; #查詢重寫
grant create any table to c##jerry; #創(chuàng)建任何表
grant select any table to c##jerry; #查看任何表
SQL>create materialized view log on info;
SQL>create materialized view mtrlview_info #建立物化視圖名稱
build immediate #立馬生成數(shù)據(jù)
refresh fast #刷新(不開啟此功能=快照)
on commit #開啟提交功能
enable query rewrite #開啟查詢重寫
as
select語句;
SQL>drop materialized view mtrlview_info;
索引是一種可以提高查詢性能的數(shù)據(jù)結(jié)構(gòu),分為以下幾類:
SQL>create index score_index on info(score);
SQL>create unique index uni_index_info on info(id);
SQL>create index re_index_info on info(score) reverse;
SQL>create bitmap index bit_index_info on info(address);
SQL>create index upp_index_info on info(upper(name)); #大寫函數(shù)索引
SQL>select index_name,index_type,table_name,tablespace_name from user_indexes;
SQL>alter index 索引名稱 rebuild;
SQL>alter index 索引名稱 rebuild tablespace 表空間
SQL>alter index 索引名稱 coalesce;
SQL>drop index 索引名稱
Oracle序列是一個(gè)連續(xù)的數(shù)字生成器。序列常用于人為的關(guān)鍵字,或給數(shù)據(jù)行排序否則數(shù)據(jù)行是無序的。
SQL>create sequence toy_seq
start with 10 #初始值
increment by 1 #增量
maxvalue 2000 #最大值
nocycle #非循環(huán)(超過2000不重新開始)
cache 30; #緩存30個(gè)序列數(shù)字
SQL>create table toy
2 (
3 id number(4) constraint PK_id primary key,
4 name varchar2(10),
5 score number(5,2),
6 born date,
7 );
SQL>insert into toy values (toy_seq.nextval,'zhangsan',88); #nextval:指針(固定) .為調(diào)用
SQL>insert into toy values (toy_seq.nextval,'zhangsan',77);
SQL>select toy_seq.currval from dual;
drop sequence toy_seq;
SQL>create synonym pr_info for info;
SQL>select * from pr_info; #通過同義詞查看
SQL>create public synonym pub_info for info;
SQL> select * from pub_info; #查看
為解決海量數(shù)據(jù)存儲(chǔ)問題
SQL>create tablespace tmp01
datafile '/orc/app/oracle/oradata/tmp01.dbf'
size 100M;
SQL>create tablespace tmp02
datafile '/orc/app/oracle/oradata/tmp02.dbf'
size 100M;
SQL>create tablespace tmp03
datafile '/orc/app/oracle/oradata/tmp03.dbf'
size 100M;
SQL>create tablespace tmp04
datafile '/orc/app/oracle/oradata/tmp04.dbf'
size 100M;
create table sales
(
sales_id number,
product_id vachar2(5),
sales_date date
)
partition by range (sales_date)
(
partition p1 values less than (to_date('2018-04-03','yyyy-mm-dd')) tablespace tmp01,
partition p2 values less than (to_date('2018-05-03','yyyy-mm-dd')) tablespace tmp02,
partition p3 values less than (to_date('2018-06-03','yyyy-mm-dd')) tablespace tmp03,
partition p4 values less than (maxvalue) tablespace tmp04
);//插入數(shù)據(jù),查看是否實(shí)現(xiàn)分布式存儲(chǔ)
insert into sales values(1,'abc',to_date('2018-05-23','yyyy-mm-dd'));
select * from sales partition(p3);
#結(jié)果顯示:輸入數(shù)據(jù)日期為2018-05-23,應(yīng)該存儲(chǔ)在p3分區(qū)內(nèi),而其他分區(qū)沒有此條數(shù)據(jù)!
感謝大家的閱讀,希望共同進(jìn)步!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。