a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 數據庫 > Oracle > 正文

oracle constraints

2024-08-29 13:53:56
字體:
來源:轉載
供稿:網友
 Oracle 的約束主要是在業務邏輯層面維護數據的完整性。主要通過程序員在應用程序中規定約束或者通過定義觸發器來維護數據完整性,最后是通過使用oracle自帶的約束來維護數據完整性。能使用oracle自帶的約束達到要求就盡量使用oracle自帶的約束,因為使用觸發器等用戶自定義約束都會影響數據庫的性能。例如:使用觸發器時會對表進行鎖定并進行表掃描或者索引掃描,這些都會降低數據庫性能和并發性。

  oracle 約束主要分為以下幾種:

not null 非空約束, unique 唯一約束, PRimary key 主鍵約束, foreign key 外鍵約束, check 約束。

  

not null 非空約束:

  創建方式:1, 建表時在列級別定義(也就是只能在定義表時將約束的定義寫在該列后面),2種方式。一種使用自定義約束名稱,一種是使用系統默認名稱。

view plaincopy to clipboardprint?
create table t   
(   
  tid     number(8) constraint NK_t1 not null,   
  tname   varchar2(10) not null  
)  
create table t
(
  tid     number(8) constraint NK_t1 not null,
  tname   varchar2(10) not null
)

view plaincopy to clipboardprint?
SQL> select t.constraint_name, t.table_name, t.status, t.deferrable from user_constraints t;   
    
CONSTRAINT_NAME TABLE_NAME                     STATUS   DEFERRABLE   
--------------- ------------------------------ -------- --------------   
NK_T1           T                              ENABLED  NOT DEFERRABLE   
SYS_C003735     T                              ENABLED  NOT DEFERRABLE   
    
SQL>   
SQL> select t.constraint_name, t.table_name, t.status, t.deferrable from user_constraints t;

CONSTRAINT_NAME TABLE_NAME                     STATUS   DEFERRABLE
--------------- ------------------------------ -------- --------------
NK_T1           T                              ENABLED  NOT DEFERRABLE
SYS_C003735     T                              ENABLED  NOT DEFERRABLE

SQL>  

             2,在表建立后對表進行修改,但是要確保表中數據沒有違反約束。

view plaincopy to clipboardprint?
SQL> alter table t modify tid not null;   
    
Table altered   
    
SQL> select t.constraint_name, t.table_name, t.status, t.deferrable from user_constraints t;   
    
CONSTRAINT_NAME TABLE_NAME                     STATUS   DEFERRABLE   
--------------- ------------------------------ -------- --------------   
SYS_C003736     T                              ENABLED  NOT DEFERRABLE   
    
SQL>   
SQL> alter table t modify tid not null;

Table altered

SQL> select t.constraint_name, t.table_name, t.status, t.deferrable from user_constraints t;

CONSTRAINT_NAME TABLE_NAME                     STATUS   DEFERRABLE
--------------- ------------------------------ -------- --------------
SYS_C003736     T                              ENABLED  NOT DEFERRABLE

SQL>  

check 約束

  創建方式:可以在表級別和列級別進行定義(既可以在列后面定義也可以在列定義完后再定義)。也是2種定義方式。

view plaincopy to clipboardprint?
SQL> create table t   
  2  (   
  3    tid     number(8) ,   
  4    tname   varchar2(10),   
  5    constraint CK_T1 check ((tid is not null) and (tid > 0))   
  6  )   
  7  /   
    
Table created   
    
SQL> alter table t add constraint CK_T2 check(tname is not null);   
    
Table altered   
    
SQL> select t.constraint_name, t.table_name, t.status, t.deferrable from user_constraints t;   
    
CONSTRAINT_NAME TABLE_NAME                     STATUS   DEFERRABLE   
--------------- ------------------------------ -------- --------------   
CK_T1           T                              ENABLED  NOT DEFERRABLE   
CK_T2           T                              ENABLED  NOT DEFERRABLE   
    
SQL>   
SQL> create table t
  2  (
  3    tid     number(8) ,
  4    tname   varchar2(10),
  5    constraint CK_T1 check ((tid is not null) and (tid > 0))
  6  )
  7  /

Table created

SQL> alter table t add constraint CK_T2 check(tname is not null);

Table altered

SQL> select t.constraint_name, t.table_name, t.status, t.deferrable from user_constraints t;

CONSTRAINT_NAME TABLE_NAME                     STATUS   DEFERRABLE
--------------- ------------------------------ -------- --------------
CK_T1           T                              ENABLED  NOT DEFERRABLE
CK_T2           T                              ENABLED  NOT DEFERRABLE

SQL>  

約束CK_T1保證tid列不能為空并且要大于0,CK_T2保證iname不能為空。check約束也可以是同一行的不同列之間的規則。

unique 約束

  創建方式:單unique規定的列只有一列時可以在列級別定義,如果unique規定的列包含多列時只能在表級別定義。

view plaincopy to clipboardprint?
SQL> create table t2   
  2  (   
  3     vid   number constraint VK_T1 unique,   
  4     vname varchar2(10),   
  5     vsex  varchar2(10),   
  6     constraint VK_T2 unique(vname,vsex)   
  7  )   
  8  /   
    
Table created   
  
SQL> select t.constraint_name, t.table_name, t.status, t.validated, t.deferrable from user_constraints t;   
    
CONSTRAINT_NAME TABLE_NAME STATUS   VALIDATED  DEFERRABLE   
--------------- ---------- -------- ---------- --------------   
VK_T1           T2         ENABLED  VALIDATED  NOT DEFERRABLE   
VK_T2           T2         ENABLED  VALIDATED  NOT DEFERRABLE   
    
SQL>   
SQL> create table t2
  2  (
  3     vid   number constraint VK_T1 unique,
  4     vname varchar2(10),
  5     vsex  varchar2(10),
  6     constraint VK_T2 unique(vname,vsex)
  7  )
  8  /

Table created

SQL> select t.constraint_name, t.table_name, t.status, t.validated, t.deferrable from user_constraints t;

CONSTRAINT_NAME TABLE_NAME STATUS   VALIDATED  DEFERRABLE
--------------- ---------- -------- ---------- --------------
VK_T1           T2         ENABLED  VALIDATED  NOT DEFERRABLE
VK_T2           T2         ENABLED  VALIDATED  NOT DEFERRABLE

SQL>  

unique約束創建的同時會產生一條索引(可能是唯一性所以也可以是非唯一性索引,這要看建表時指定該表是否在數據插入時立即檢查數據的約束):

view plaincopy to clipboardprint?
SQL> select t.index_name, t.table_name, t.uniqueness from user_indexes t;   
    
INDEX_NAME  TABLE_NAME UNIQUENESS   
----------- ---------- ----------   
VK_T1       T2         UNIQUE   
VK_T2       T2         UNIQUE  
SQL> select t.index_name, t.table_name, t.uniqueness from user_indexes t;

INDEX_NAME  TABLE_NAME UNIQUENESS
----------- ---------- ----------
VK_T1       T2         UNIQUE
VK_T2       T2         UNIQUE

由于有索引所以在創建表時可以指定索引存儲的位置和一些存儲參數。

view plaincopy to clipboardprint?
SQL> create table t2   
  2  (   
  3     vid   number constraint VK_T1 unique,   
  4     vname varchar2(10),   
  5     vsex  varchar2(10),   
  6     constraint VK_T2 unique(vname,vsex) using index tablespace indx   
  7                                         storage(initial 100k next 100k pctincrease 0)   
  8                                         nologging   
  9  )   
10  /   
    
Table created  
SQL> create table t2
  2  (
  3     vid   number constraint VK_T1 unique,
  4     vname varchar2(10),
  5     vsex  varchar2(10),
  6     constraint VK_T2 unique(vname,vsex) using index tablespace indx
  7                                         storage(initial 100k next 100k pctincrease 0)
  8                                         nologging
  9  )
10  /

Table created

指定約束索引存儲的表空間是indx表空間,初始塊大小是100k,然后對dml操作不產生日志(但是由于其他原因也會產生日志,只是相對默認的logging要少)

primary key 主鍵約束

   創建方式:primary key主要是由非空和唯一性組合而成的。一個表只能包含一個主鍵,但一個主鍵可以含有多列。

view plaincopy to clipboardprint?
SQL> create table t2   
  2  (   
  3     vid   number constraint VK_T1 unique,   
  4     vname varchar2(10),   
  5     vsex  varchar2(10),   
  6     constraint VK_T2 primary key(vname,vsex) using index tablespace indx   
  7                                         storage(initial 100k next 100k pctincrease 0)   
  8                                         nologging   
  9  )   
10  /   
    
Table created  
SQL> create table t2
  2  (
  3     vid   number constraint VK_T1 unique,
  4     vname varchar2(10),
  5     vsex  varchar2(10),
  6     constraint VK_T2 primary key(vname,vsex) using index tablespace indx
  7                                         storage(initial 100k next 100k pctincrease 0)
  8                                         nologging
  9  )
10  /

Table created

foreign key 外鍵

  創建方式:外鍵涉及的表可以有2張也可以是1張,2張的情況下一張child表中的一個字段引用的鍵必須fater表中的主鍵。約束是建立在child表中的,標示該表中的該字段中的值必須在父表中存在或者是NULL值。

view plaincopy to clipboardprint?
SQL> create table dept   
  2  (   
  3    did   number(8),   
  4    dname varchar2(20),   
  5    constraint PK_DEPT primary key (did)   
  6  )   
  7  /   
    
Table created   
    
SQL>    
SQL> create table emp   
  2  (   
  3    eid   number(8) primary key,   
  4    ename varchar2(20),   
  5    did   number(8) /*references dept(did)*/,   
  6    dname varchar2(20),   
  7    constraint FK_EMP2 foreign key(did) references dept(did)   
  8  )   
  9  /   
    
Table created  
SQL> create table dept
  2  (
  3    did   number(8),
  4    dname varchar2(20),
  5    constraint PK_DEPT primary key (did)
  6  )
  7  /

Table created

SQL>
SQL> create table emp
  2  (
  3    eid   number(8) primary key,
  4    ename varchar2(20),
  5    did   number(8) /*references dept(did)*/,
  6    dname varchar2(20),
  7    constraint FK_EMP2 foreign key(did) references dept(did)
  8  )
  9  /

Table created

由于外鍵(foreign key)由reference key(引用鍵)決定所以在對fater table進行update或者drop,delete等操作時會受到限制。具體有外鍵約束的狀態決定。

  delete on action, delete cascade, delete set null

delete on action 默認設置,如果刪除fater table表中的數據,這時oracle會鎖定字表然后進行掃描表(如果外鍵列有所以則掃描索引)然后child表中的外鍵列存在該數據則不允許刪除。

delete cascade 則把字表中的相應對應的行也刪除。

delete set null 則吧子表中的外鍵列對應的數據變成NULL.

view plaincopy to clipboardprint?
create table emp   
(   
  eid   number(8) primary key,   
  ename varchar2(20),   
  did   number(8) /*references dept(did)*/,   
  dname varchar2(20),   
  constraint FK_EMP2 foreign key(did) references dept(did) on delete set null  
)  
create table emp
(
  eid   number(8) primary key,
  ename varchar2(20),
  did   number(8) /*references dept(did)*/,
  dname varchar2(20),
  constraint FK_EMP2 foreign key(did) references dept(did) on delete set null
)  

由于對父表的reference key進行delete,update操作時候都會對child表進行鎖定然后進行掃描,所以可以在child的foreign key上建立索引。這樣就不需要對表進行鎖定了而且也減少了掃描的時間。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色小电影网址 | 91中文字幕 | 在线观看三级视频 | 亚洲高清在线 | 精品久久久久久国产 | 成人亚洲视频 | 黄av在线| 一区二区三区回区在观看免费视频 | 久久成人激情视频 | 国产欧美精品区一区二区三区 | 国产精品久久久久蜜臀 | 久久久精品视频在线观看 | 国产综合一区二区 | www.男人天堂 | 日本不卡免费新一二三区 | 日韩精品小视频 | 欧美综合在线观看 | 精品久久久久久亚洲精品 | 久久国产亚洲 | av网址大全在线观看 | 精品九九 | 99热福利 | 一区二区三区精品视频 | 欧美成人一区二区三区 | 国产四区 | 日本欧美中文字幕 | 91视频免费看网站 | 日韩一区二区不卡 | 成人午夜电影网 | 欧美性网 | 亚洲三级网站 | 中文字幕在线视频免费观看 | 国产精品国产三级国产aⅴ无密码 | 午夜视频在线观看网站 | 亚洲影院成人 | 一级大片免费观看 | 欧美一及黄色片 | 毛片在线免费 | 日韩在线观看精品 | 亚洲福利一区 | 亚洲精品视频一区二区三区 |