PostgreSQL NOT NULL外键和带序列的默认值

时间:2016-11-11 14:26:50

标签: postgresql

我正在分析以下PostgreSQL架构:

CREATE SEQUENCE ref_email_type_ref_email_type_id_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE TABLE ref_email_type
(
  ref_email_type_id integer NOT NULL DEFAULT nextval('ref_email_type_ref_email_type_id_seq'::regclass),
  description character varying(50) NOT NULL,
  CONSTRAINT ref_email_type_pkey PRIMARY KEY (ref_email_type_id)
)

CREATE TABLE email
(
  email_id integer NOT NULL DEFAULT nextval('email_email_id_seq'::regclass),
  ref_email_type_id integer NOT NULL DEFAULT nextval('email_ref_email_type_id_seq'::regclass),
  email_address character varying(100),
  CONSTRAINT email_pkey PRIMARY KEY (email_id),
  CONSTRAINT email_ref_email_type_id_fkey FOREIGN KEY (ref_email_type_id)
      REFERENCES ref_email_type (ref_email_type_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

email.ref_email_type_id和现有约束的情况下,使用DEFAULT nextval('email_ref_email_type_id_seq'::regclass)声明NOT NULL是否有意义:

  CONSTRAINT email_ref_email_type_id_fkey FOREIGN KEY (ref_email_type_id)
      REFERENCES ref_email_type (ref_email_type_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION

1 个答案:

答案 0 :(得分:1)

不,这根本没有任何意义。

它也不会造成任何伤害,因为默认值可能永远不会被使用。