1. 程式人生 > >oracle(sql)基礎篇系列(四)——數字字典、索引、序列、三正規化

oracle(sql)基礎篇系列(四)——數字字典、索引、序列、三正規化

數字字典表

--檢視當前使用者下面有哪些張表

select * from user_tables;

select table_name from user_tables;

 

--檢視當前使用者下面有哪些檢視

select * from user_views;

select view_name from user_views;

 

--檢視當前使用者下面有哪些約束

select * from user_constraints;

select constraint_name from user_constraints;

 

--檢視當前使用者下面的約束在哪些表上面

select constraint_name, table_name from user_constraints;

 

--select * from dictionary;

 

像上面這樣的表被稱作資料字典表,存在dictionary中。

索引

索引的概念

索引是一個數據庫物件,索引就相當於我們字典裡那個索引;當為某個欄位建立索引,查詢這個欄位的時候效率就會更高。

需要注意的是,索引的建立只是提供了查詢效率,修改卻更慢了,因為需要附加修改索引表。

 

什麼時候建索引

查詢某個欄位,訪問量特別大的時候,而且效率比較低的時候,這個時候可以考慮建立索引。但不要輕易建立索引,因為建立所以也會增加而外的維護開銷。

建立索引

--給stu表的email欄位建立索引

create index stu_email_index on stu(email);

 

刪除索引

--刪除stu表的email欄位的索引stu_email_index

drop index stu_email_index;

 

序列

序列的概念

create table stu

(

id number(10) ,

...

constraint stu_id_pk primary key(id),

...

);

在建立stu表的時候,我們制定了id作為主鍵,主鍵是唯一的,這就要求每次插入一條記錄,都要插入一個新的id,但是在多執行緒的環境下同時插入大量記錄,如何保證每次插入的id是不同的,而且這個id應該是有序遞增的?一般資料庫都會提供這樣的機制:專門產生一個獨一無二的數,然後每次自己往上遞增一個1或者指定增量。Oracle裡,這種機制稱為序列sequence。sequence是一個物件,可以使用裡面的屬性nextval作為主鍵id的值,這就能保證主鍵id是唯一的。

建立序列與檢視

--序列提供了兩個屬性,即NEXTVAL 和CURRVAL,用來訪問序列中的序號

--其中NEXTVAL代表下一個可用的序號, CURRYAL代表當前的序號。

drop sequence seq;

create sequence seq;

select seq.nextval ,seq.currval from dual;

SELECT * FROM USER_SEQUENCES;

SELECT * FROM ALL_SEQUENCES;

--sys使用者檢視

SELECT * FROM DBA_SEQUENCES;

 

--INCREMENT BY選項指定了序列中序號遞增的幅度

drop sequence seq2;

create sequence seq2 increment by 2;

select seq2.nextval ,seq2.currval from dual;

 

--START WITH選項指定序列中的序號從哪個數字開始

drop sequence seq3;

create sequence seq3 start with 20;

select seq3.nextval ,seq3.currval from dual;

 

--MAXVALUE用來指定序列中序號的最大值

drop sequence seq4;

create sequence seq4 maxvalue 3;

select seq4.nextval ,seq4.currval from dual;

 

--MINVALUE用來指定序列中序號的最小值

drop sequence seq5;

create sequence seq5 minvalue 5;

select seq5.nextval ,seq5.currval from dual;

 

--選項CYCLE使得序列中的序號可以迴圈使用

drop sequence seq6;

--必須指定nocache 或者cache,否則報ORA-04013: number to CACHE must be less than one cycle

create sequence seq6 minvalue 1 maxvalue 5 cycle nocache;

select seq6.nextval ,seq6.currval from dual;

 

--如果把序列中的序號放在記憶體中進行緩衝,那麼獲得序號的速度將大大加快。

--將序列中接下來的n個序號在記憶體中進行緩衝,最小值為2

drop sequence seq7;

create sequence seq7 minvalue 1 maxvalue 30 cache 3;

select seq7.nextval ,seq7.currval from dual;

 

序列的修改

--序列在建立之後,可以對其進行修改。

--比如修改它的最大值、最小值、增幅等,但不能修改初始值。

drop sequence seq8;

create sequence seq8 increment by 2;

select seq8.nextval ,seq8.currval from dual;

 

alter sequence seq8 increment by 3;

刪除序列

drop sequence seq;

 

序列作為主鍵使用

insert into stu(id,name,email) values(seq.nextval,'lisi','[email protected]');

insert into stu(id,name,email) values(seq.nextval,'wangwu','[email protected]');

insert into stu(id,name,email) values(seq.nextval,'chenliu','[email protected]');

 

 

 

三正規化

正規化的由來

資料庫設計時的一些規則,而這些規則是由一個姓範的人規定的,所以叫正規化。

正規化的目標

不存在冗餘資料(同樣的資料不存第二遍)。

第一正規化

第一正規化的要求:1.要有主鍵(設計任何表都要有主鍵) 2.列不可分

第二正規化

第二正規化的要求:當一張表裡面有多個欄位作為主鍵的時候,非主鍵的這些欄位,不能依賴於部分主鍵。簡單的說:不能存在部分依賴。

第三正規化

第三正規化的要求:不能存在傳遞依賴(除了主鍵之外的其他欄位必須直接依賴於主鍵)。