插入没有空值

Insert into without null values

我不明白为什么即使我设置了非空 select 来执行插入,我仍然得到我试图输入空值的错误。作为上下文,我试图将另一个 table 中的两列插入到新的 table.

CREATE TABLE t.tb_c    
(
    id  INT NOT NULL,
    itm CHARACTER(5) NOT NULL,
    des CHARACTER(120) NOT NULL,

    CONSTRAINT pk_item PRIMARY KEY (item_id)
);

INSERT INTO t.tb_c (itm)
    SELECT itm 
    FROM erp.tb_d
    WHERE itm IS NOT NULL;



 INSERT INTO t.tb_c (des)
        SELECT des
        FROM erp.tb_d
        WHERE des IS NOT NULL;

我收到这个错误:

SQLState: 23502
Short Description: AN UPDATE INSERT OR SET VALUE IS NULL BUT THE OBJECT COLUMN CANNOT CONTAIN NULL VALUES

ERROR: the null value for the «des» violates the restriction not null
DETAIL: The row is (90002, AA111, null).
SQL state: 23502

试试这个插件:

INSERT INTO t.tb_c (itm, des)
SELECT itm, des
        FROM erp.tb_d
        WHERE des IS NOT NULL AND itm IS NOT NULL;
alter table t.tb_c  alter column itm  drop not null;
alter table t.tb_c  alter column des  drop not null;

将 id 从 int 更改为 serial follow=> Changing primary key int type to serial


ALTER TABLE t.tb_c ALTER COLUMN itm SET DEFAULT '';
ALTER TABLE t.tb_c ALTER COLUMN des  SET DEFAULT '';

将 id 从 int 更改为 serial follow=> Changing primary key int type to serial