I am using PostgreSQL db. I have two tables as shown below
I would like to add the missing person_ids in Table_2 by referring records from Table_1.
If you see the above tables, you can notice that person_id = 2,4 from table_1 is missing in table_2.
Though I am able to do this with help from forum, the problem is with the not null constraint.
Table 2 create table definition is like as shown below
CREATE TABLE Table_2(
SNO INTEGER (20) UNIQUE NOT NULL,
PERSON_ID INTERGER (20) NOT NULL,
date_1 DATE NOT NULL,
date_2 DATE NOT NULL,
DEPT VARCHAR (10) NOT NULL
);
This is what I tried
INSERT into Table_2 (person_id,date_1,date_2,Dept) (select distinct person_id,TO_DATE('1900-01-01', 'YYYY-MM-DD'),TO_DATE('2900-12-30', 'YYYY-MM-DD'),'F' from Table_1 where person_id not in (select distinct person_id from Table_2))
This results in error as shown below
`ERROR: null value in column "SNO" violates not-null constraint
DETAIL: Failing row contains (null, 2, 1900-01-01, 2900-12-30, F).
SQL state: 23502`
I expect my output to be like as shown below. Please note that my real data has more than 50K records and the newly added records should continue the sequence as is.



CREATE TABLEfor table 1. What is the difference betweenSNOandperson_idin table 1?snocolumn isn't there intable_1table_1have asnocolumn? Doessnointable_2have a sequence or is it a serial column?CREATE TABLEcommand, and that's not table 1. That's not table 2 either, does it have a 'gender' and/or a 'dept'? And do not post images, post code someone can copy-paste.