Oracle primary key和unique key的區別與聯絡

來源:互聯網
上載者:User

  • primary key與unique key都是唯一性限制式。但二者有很大的區別:   
  • 1.作為primary key的1個或多個列必須為NOT NULL,   
  •   如果建表時此列設為NULL,在增加PRIMARY KEY時,列自動更改為NOT NULL。   
  •   而unique key約束的列可以為null,這是primary key與unique key最大的區別。   
  • 2.一個表只能有一個primary key(單列或多列,多列主鍵叫聯合主鍵),但可以有多個unique key。   
  • 執行個體1:   
  • create table t(c1 number(2),c2 date,c3 varchar2(5),c4 int);   
  • desc t;   
  • Name Type        Nullable Default Comments    
  • ---- ----------- -------- ------- --------    
  • C1   NUMBER(2)   Y                            
  • C2   DATE        Y                            
  • C3   VARCHAR2(5) Y                            
  • C4   INTEGER     Y   
  • //   
  • 執行個體2:添加primary key   
  • alter table t add constraint t_pk primary key(c1,c2);   
  • desc t;   
  • Name Type        Nullable Default Comments    
  • ---- ----------- -------- ------- --------    
  • C1   NUMBER(2)                                
  • C2   DATE                                     
  • C3   VARCHAR2(5) Y                            
  • C4   INTEGER     Y    
  • 我們看到,將c1,c2列設為聯合主鍵後,他們變為not null;   
  • 如果在建表時就指定了主鍵的話,主鍵列將會預設為not null。   
  • //   
  • 如果我們在添加一個primary key,那麼我們將會得到一個錯誤:   
  • alter table t add constraint t_pk_2 primary key(c3,c4)   
  • ORA-02260: table can have only one primary key   
  • //   
  • 執行個體3:添加unique key   
  • alter table t add constraint unique_key_t unique(c3,c4);   
  • 執行個體4:添加資料   
  • insert into t(c1,c2,c3,c4)   
  • values(10,sysdate,'abc',3);   
  • 1 row inserted   
  • //   
  • insert into t(c1,c2,c3,c4)   
  • values(11,sysdate,'abc',3);   
  • ORA-00001: unique constraint (SCOTT.UNIQUE_KEY_T) violated   
  • 我們看到,添加的第二條資料違反了剛剛建立的唯一鍵約束;   
  • 將unique_key_t刪除,添加就能成功了。   
  • 執行個體5:刪除unique key   
  • alter table t drop constraint unique_key_t;   
  • insert into t(c1,c2,c3,c4)   
  • values(11,sysdate,'abc',3);   
  • 1 row inserted   
  • //   
  • 執行個體6:刪除primary key   
  • alter table t drop constraint t_pk;   
  • Table altered   
  • //   
  • desc t;   
  • Name Type        Nullable Default Comments    
  • ---- ----------- -------- ------- --------    
  • C1   NUMBER(2)   Y                            
  • C2   DATE        Y                            
  • C3   VARCHAR2(5) Y                            
  • C4   INTEGER     Y    
  • 刪除主鍵約束後,c1,c2列由恢複了原來的預設值null。   
  • //   
  • 3.其實primary key也是unique key,被primary key約束的列not null,並且不允許重複   
  • 執行個體7:   
  • truncate table t;   
  • Table truncated   
  • //   
  • alter table t add constraint t_pk primary key(c1,c2);   
  • Table altered   
  • //   
  • insert into t(c1,c2,c3,c4)   
  • values(1,to_date('2011-01-01','yyyy-mm-dd'),'csdn',10);   
  • 1 row inserted   
  • //   
  • insert into t(c3,c4)   
  • values('china',1);   
  • ORA-01400: cannot insert NULL into ("SCOTT"."T"."C1")   
  • //   
  • insert into t(c1,c2,c3,c4)   
  • values(1,to_date('2011-01-01','yyyy-mm-dd'),'csdn',10);   
  • ORA-00001: unique constraint (SCOTT.T_PK) violated   
  • 這裡,我們看到primary key t_pk變成了unique約束,證明了主鍵約束也是唯一約束。 
  • 相關文章

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.