Oracle主鍵Primary Key包含非空約束及唯一約束。
石泉網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司成立于2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
添加主鍵的語(yǔ)句
alter table table_name add constraint cons_name primary key(col_name);
查看主鍵是否被創(chuàng)建成功
select dbms_metadata.get_ddl('OBJECT_TYPE','NAME','SCHEMA') from dual;
OBJECT_TYPE (TABLE,PARTITION,INDEX......)
NAME (OBJECT_NAME)
SCHEMA (INSTANCE)
對(duì)應(yīng)object表中的相應(yīng)內(nèi)容:
select object_type,object_name,owner from dba_objects
獲取到的DDL語(yǔ)句中表示已經(jīng)創(chuàng)建了主鍵列,該列位于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);
創(chuàng)建主鍵后發(fā)現(xiàn)該表增加了唯一索引及主鍵字段
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);
驗(yàn)證:
反復(fù)向表中插入數(shù)據(jù) insert into userlevelbytel values('10010','3',' ');
出現(xiàn)如下錯(cuò)誤提示(唯一約束錯(cuò)誤):
ORA-00001: unique constraint (NEWCCS.PK_PRIMARY) violated
插入空值 insert into userlevelbytel values('','3',' ');
出現(xiàn)如下錯(cuò)誤提示(唯一約束錯(cuò)誤):
ORA-01400: cannot insert NULL into ("NEWCCS"."USERLEVELBYTEL_BAK"."CUSTTEL")
單獨(dú)添加唯一約束對(duì)比區(qū)別
刪除該表主鍵:
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;
可以看到唯一約束被創(chuàng)建
測(cè)試:
反復(fù)插入非空記錄:
ORA-00001: unique constraint (NEWCCS.UNIQUE_CUSTTEL) violated
反復(fù)插入空記錄:
select * from userlevelbytel_bak where custtel is null;
查詢(xún)結(jié)果為多個(gè)重復(fù)的空記錄
這里面可以看出唯一約束僅對(duì)非空值進(jìn)行約束,而在Oracle中NULL值無(wú)法比較,因此即使反復(fù)插入,唯一約束也無(wú)法限制,但在組合約束條件下,比如(A,B,NULL)則是可以進(jìn)行比較的。
舉例:
alter table userlevelbytel_bak add constraint unique_custtel unique(custtel,userlevel);
反復(fù)插入空記錄:
ORA-00001: unique constraint (NEWCCS.UNIQUE_CUSTTEL) violated