有人可以解释两个CREATE表语句之间的结果会有什么不同(有什么好处)?
选项#1
CREATE TABLE sch.address_type (
address_type_cd VARCHAR2(50 BYTE) NOT NULL CONSTRAINT sch_2002 CHECK ("address_type_cd" IS NOT NULL),
desc_txt VARCHAR2(100 BYTE) NOT NULL CONSTRAINT sch_2003 CHECK ("DESC_TXT" IS NOT NULL),
rec_creat_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL CONSTRAINT sch_2001 CHECK ("REC_CREAT_TS" IS NOT NULL),
CONSTRAINT pk_address_type PRIMARY KEY (address_type_cd)
);
选项#2
CREATE TABLE sch.address_type (
address_type_cd VARCHAR2(50 BYTE) NOT NULL,
desc_txt VARCHAR2(100 BYTE) NOT NULL,
rec_creat_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT pk_address_type PRIMARY KEY (address_type_cd)
);
由于
答案 0 :(得分:1)
两个语句都执行相同的功能,但在第一个查询中,您正在做不必要的工作。即address_type_cd VARCHAR2(50 BYTE) NOT NULL
与CONSTRAINT sch_2002 CHECK ("address_type_cd" IS NOT NULL),
相同。它们都意味着字段中应该有一个值,并且不能等于NULL
答案 1 :(得分:0)
查看使用查询创建的约束:
SELECT constraint_name, constraint_type, search_condition
FROM ALL_CONSTRAINTS
WHERE table_name = 'ADDRESS_TYPE';
选项1 :
CONSTRAINT_NAME C SEARCH_CONDITION
-------------------- - ------------------------------
PK_ADDRESS_TYPE P
SYS_C0010366 C "ADDRESS_TYPE_CD" IS NOT NULL
SYS_C0010367 C "DESC_TXT" IS NOT NULL
SYS_C0010368 C "REC_CREAT_TS" IS NOT NULL
SCH_2002 C address_type_cd IS NOT NULL
SCH_2003 C "DESC_TXT" IS NOT NULL
SCH_2001 C "REC_CREAT_TS" IS NOT NULL
选项2 :
CONSTRAINT_NAME C SEARCH_CONDITION
-------------------- - ------------------------------
PK_ADDRESS_TYPE P
SYS_C0010373 C "ADDRESS_TYPE_CD" IS NOT NULL
SYS_C0010374 C "DESC_TXT" IS NOT NULL
SYS_C0010375 C "REC_CREAT_TS" IS NOT NULL
选项3 :
CREATE TABLE address_type (
address_type_cd VARCHAR2(50 BYTE) CONSTRAINT address_type__cd__pk PRIMARY KEY,
desc_txt VARCHAR2(100 BYTE) CONSTRAINT address_type__desc__nn NOT NULL,
rec_creat_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
CONSTRAINT address_type__rct__nn NOT NULL
);
有约束:
CONSTRAINT_NAME C SEARCH_CONDITION
------------------------- - ------------------------------
ADDRESS_TYPE__CD__PK P
ADDRESS_TYPE__DESC__NN C "DESC_TXT" IS NOT NULL
ADDRESS_TYPE__RCT__NN C "REC_CREAT_TS" IS NOT NULL
所以:
NOT NULL
列上不需要PRIMARY KEY
约束,它还会命名NOT NULL
约束。所有选项都会给出相同的有效结果。在调试期间命名约束很有用(甚至是NOT NULL
约束)