您好,登錄后才能下訂單哦!
Oracle主鍵Primary Key包含非空約束及唯一約束。
添加主鍵的語句
alter table table_name add constraint cons_name primary key(col_name);
查看主鍵是否被創建成功
select dbms_metadata.get_ddl('OBJECT_TYPE','NAME','SCHEMA') from dual;
OBJECT_TYPE (TABLE,PARTITION,INDEX......)
NAME (OBJECT_NAME)
SCHEMA (INSTANCE)
對應object表中的相應內容:
select object_type,object_name,owner from dba_objects
獲取到的DDL語句中表示已經創建了主鍵列,該列位于CUSTTEL上
CREATE TABLE "NEWCCS"."USERLEVELBYTEL_BAK" ( "CUSTTEL" VARCHAR2(20), "USERLEVEL" NUMBER(*,0) NOT NULL ENABLE, "CUSTID" VARCHAR2(20) ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ; ALTER TABLE "NEWCCS"."USERLEVELBYTEL_BAK" MODIFY ("USERLEVEL" NOT NULL ENABLE);
創建主鍵后發現該表增加了唯一索引及主鍵字段
CREATE TABLE "NEWCCS"."USERLEVELBYTEL_BAK" ( "CUSTTEL" VARCHAR2(20), "USERLEVEL" NUMBER(*,0) NOT NULL ENABLE, "CUSTID" VARCHAR2(20), CONSTRAINT "PK_PRIMARY" PRIMARY KEY ("CUSTTEL") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ; CREATE UNIQUE INDEX "NEWCCS"."PK_PRIMARY" ON "NEWCCS"."USERLEVELBYTEL_BAK" ("CUSTTEL") PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ; ALTER TABLE "NEWCCS"."USERLEVELBYTEL_BAK" ADD CONSTRAINT "PK_PRIMARY" PRIMARY KEY ("CUSTTEL") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ENABLE; ALTER TABLE "NEWCCS"."USERLEVELBYTEL_BAK" MODIFY ("USERLEVEL" NOT NULL ENABLE);
驗證:
反復向表中插入數據 insert into userlevelbytel values('10010','3',' ');
出現如下錯誤提示(唯一約束錯誤):
ORA-00001: unique constraint (NEWCCS.PK_PRIMARY) violated
插入空值 insert into userlevelbytel values('','3',' ');
出現如下錯誤提示(唯一約束錯誤):
ORA-01400: cannot insert NULL into ("NEWCCS"."USERLEVELBYTEL_BAK"."CUSTTEL")
單獨添加唯一約束對比區別
刪除該表主鍵:
alter table userlevelbytel_bak drop constraint pk_primary;
CREATE TABLE "NEWCCS"."USERLEVELBYTEL_BAK" ( "CUSTTEL" VARCHAR2(20), "USERLEVEL" NUMBER(*,0) NOT NULL ENABLE, "CUSTID" VARCHAR2(20), CONSTRAINT "UNIQUE_CUSTTEL" UNIQUE ("CUSTTEL") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ; CREATE UNIQUE INDEX "NEWCCS"."UNIQUE_CUSTTEL" ON "NEWCCS"."USERLEVELBYTEL_BAK" ("CUSTTEL") PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ; ALTER TABLE "NEWCCS"."USERLEVELBYTEL_BAK" MODIFY ("USERLEVEL" NOT NULL ENABLE); ALTER TABLE "NEWCCS"."USERLEVELBYTEL_BAK" ADD CONSTRAINT "UNIQUE_CUSTTEL" UNIQUE ("CUSTTEL") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "NEWCCS" ENABLE;
可以看到唯一約束被創建
測試:
反復插入非空記錄:
ORA-00001: unique constraint (NEWCCS.UNIQUE_CUSTTEL) violated
反復插入空記錄:
select * from userlevelbytel_bak where custtel is null;
查詢結果為多個重復的空記錄
這里面可以看出唯一約束僅對非空值進行約束,而在Oracle中NULL值無法比較,因此即使反復插入,唯一約束也無法限制,但在組合約束條件下,比如(A,B,NULL)則是可以進行比較的。
舉例:
alter table userlevelbytel_bak add constraint unique_custtel unique(custtel,userlevel);
反復插入空記錄:
ORA-00001: unique constraint (NEWCCS.UNIQUE_CUSTTEL) violated
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。