ERROR: null value in column violates not-null constraint
ERROR: null value in column violates not-null constraint
CREATE TABLE medico
(num_cedula serial NOT NULL UNIQUE,
nome varchar(20) NOT NULL,
especialidade varchar(20) NOT NULL,
PRIMARY KEY(num_cedula));
CREATE TABLE consulta
(num_cedula serial NOT NULL,
num_doente serial NOT NULL,
data_consulta date NOT NULL,
nome varchar(20) NOT NULL,
CHECK(EXTRACT(DOW FROM data_consulta) NOT IN (6,0)),
UNIQUE(num_doente,data_consulta,nome),
FOREIGN KEY(nome) REFERENCES instituicao(nome) ON UPDATE CASCADE,
FOREIGN KEY(num_cedula) REFERENCES medico(num_cedula) ON UPDATE CASCADE,
PRIMARY KEY(num_cedula,num_doente,data_consulta));
CREATE TABLE analise
(num_analise serial NOT NULL UNIQUE,
especialidade varchar(20) NOT NULL,
num_cedula serial ,
num_doente serial ,
data_analise date ,
data_registo date NOT NULL,
nome varchar(20) NOT NULL,
quant integer NOT NULL,
inst varchar(20) NOT NULL,
CONSTRAINT fk_analise FOREIGN KEY(num_cedula,num_doente,data_analise) REFERENCES consulta(num_cedula,num_doente,data_consulta) ON UPDATE CASCADE,
FOREIGN KEY(inst) REFERENCES instituicao(nome) ON UPDATE CASCADE,
PRIMARY KEY(num_analise));
这是我的代码,但是当我尝试插入另一个带有 num_cedula、num_doente、data_analise 作为 NULL 的“分析”时,它给了我这个错误(“错误:空值在“num_cedula”列中违反了非空约束
详细信息:失败行包含 (32, Ortopedia, null, null, null, 2019-12-02, glicemia, 176, Instituicao1)。
SQL state: 23502") 我真的不知道为什么。
另外,这是我使用的插入代码:
INSERT INTO analise VALUES(32,'Ortopedia',NULL,NULL,NULL,'2019-12-02','glicemia',176,'Instituicao1');
serial
数据类型不允许 null
值。 documentation 解释了在声明 serial
时幕后发生的事情(重点是我的):
Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. A NOT NULL
constraint is applied to ensure that a null value cannot be inserted.
我不认为你真的想要 serial
这里。该数据类型的用例是创建一个自动递增的列。您可以改用 int
,这样可以避免您遇到的问题。
CREATE TABLE medico
(num_cedula serial NOT NULL UNIQUE,
nome varchar(20) NOT NULL,
especialidade varchar(20) NOT NULL,
PRIMARY KEY(num_cedula));
CREATE TABLE consulta
(num_cedula serial NOT NULL,
num_doente serial NOT NULL,
data_consulta date NOT NULL,
nome varchar(20) NOT NULL,
CHECK(EXTRACT(DOW FROM data_consulta) NOT IN (6,0)),
UNIQUE(num_doente,data_consulta,nome),
FOREIGN KEY(nome) REFERENCES instituicao(nome) ON UPDATE CASCADE,
FOREIGN KEY(num_cedula) REFERENCES medico(num_cedula) ON UPDATE CASCADE,
PRIMARY KEY(num_cedula,num_doente,data_consulta));
CREATE TABLE analise
(num_analise serial NOT NULL UNIQUE,
especialidade varchar(20) NOT NULL,
num_cedula serial ,
num_doente serial ,
data_analise date ,
data_registo date NOT NULL,
nome varchar(20) NOT NULL,
quant integer NOT NULL,
inst varchar(20) NOT NULL,
CONSTRAINT fk_analise FOREIGN KEY(num_cedula,num_doente,data_analise) REFERENCES consulta(num_cedula,num_doente,data_consulta) ON UPDATE CASCADE,
FOREIGN KEY(inst) REFERENCES instituicao(nome) ON UPDATE CASCADE,
PRIMARY KEY(num_analise));
这是我的代码,但是当我尝试插入另一个带有 num_cedula、num_doente、data_analise 作为 NULL 的“分析”时,它给了我这个错误(“错误:空值在“num_cedula”列中违反了非空约束 详细信息:失败行包含 (32, Ortopedia, null, null, null, 2019-12-02, glicemia, 176, Instituicao1)。 SQL state: 23502") 我真的不知道为什么。 另外,这是我使用的插入代码:
INSERT INTO analise VALUES(32,'Ortopedia',NULL,NULL,NULL,'2019-12-02','glicemia',176,'Instituicao1');
serial
数据类型不允许 null
值。 documentation 解释了在声明 serial
时幕后发生的事情(重点是我的):
Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. A
NOT NULL
constraint is applied to ensure that a null value cannot be inserted.
我不认为你真的想要 serial
这里。该数据类型的用例是创建一个自动递增的列。您可以改用 int
,这样可以避免您遇到的问题。