Oracle數據庫---對象中最基本的是表和視圖,其餘還有約束、索引、序列、函數、存儲過程、甚至建立同義詞。對數據庫的操做能夠基本歸結爲對數據對象的操做,所以,在上篇博文講述了基本操做的基礎上,本篇博文將介紹其對象的基本操做。數據庫
- Oracle中表是數據存儲的基本結構。隨之引入了分區表和對象表,後來又引入了臨時表,使表的功能更強大。
- 視圖是一個或多個表中數據的邏輯表達式。用戶能夠將視圖當作一個存儲查詢(stored query)或一個虛擬表(virtual table).查詢僅僅存儲在oracle數據字典中,實際的數據沒有存放在任何其它地方,因此創建視圖不用消耗其餘的空間。視圖也能夠隱藏複雜查詢。
在上一篇博文已詳細闡述,建立表空間---建立用戶(c##jerry)---建立表(info),表環境以下:c#
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; #建立視圖 select view view_info as select * from info; #查看視圖 drop view view_info; #刪除視圖
物化視圖是包括一個查詢結果的數據庫對像,它是遠程數據的的本地副本,或者用來生成基於數據表求和的彙總表。物化視圖存儲基於遠程表的數據,也能夠稱爲快照。oracle
SQL>conn sys/abc123 as sysdba #切換到管理員 grant create materialized view to c##jerry; #建立物化視圖 grant query rewrite to c##jerry; #查詢重寫 grant create any table to c##jerry; #建立任何表 grant select any table to c##jerry; #查看任何表
SQL>create materialized view log on info;app
SQL>create materialized view mtrlview_info #創建物化視圖名稱 build immediate #立馬生成數據 refresh fast #刷新(不開啓此功能=快照) on commit #開啓提交功能 enable query rewrite #開啓查詢重寫 as select語句;
SQL>drop materialized view mtrlview_info;less
索引是一種能夠提升查詢性能的數據結構,分爲如下幾類:分佈式
SQL>create index score_index on info(score);ide
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)); #大寫函數索引
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序列是一個連續的數字生成器。序列經常使用於人爲的關鍵字,或給數據行排序不然數據行是無序的。
SQL>create sequence toy_seq start with 10 #初始值 increment by 1 #增量 maxvalue 2000 #最大值 nocycle #非循環(超過2000不從新開始) cache 30; #緩存30個序列數字
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:指針(固定) .爲調用
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; #查看
爲解決海量數據存儲問題
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
);//插入數據,查看是否實現分佈式存儲
insert into sales values(1,'abc',to_date('2018-05-23','yyyy-mm-dd'));
select * from sales partition(p3);
#結果顯示:輸入數據日期爲2018-05-23,應該存儲在p3分區內,而其餘分區沒有此條數據!
感謝你們的閱讀,但願共同進步!