I need to insert dataset in postgresql.
INSERT INTO table_subject_topics_exams (name_of_subject, section, topic, subtopic) VALUES ('Algebra', 'Mathematics', 'Progressions', 'Number Sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Arithmetic Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Arithmetic Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Arithmetic Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Arithmetic Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Geometric Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Geometric Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Geometric Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Geometric Progression');
the problem is that there are many duplicate values in the request. As the result database should involve
('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Arithmetic Progression'), ('Algebra', 'Mathematics', 'Progressions', 'Geometric Progression');
Or nothing if this dataset is in database. How should I make my query?
P.S.
The table is:
CREATE TABLE public.table_subject_topics_exams ( ids_of_subject_section integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ), name_of_subject character varying(50) COLLATE pg_catalog."default" NOT NULL, section character varying(50) COLLATE pg_catalog."default", topic character varying(50) COLLATE pg_catalog."default" NOT NULL, subtopic character varying(50) COLLATE pg_catalog."default" NOT NULL, CONSTRAINT table_subject_topics_exams_pkey PRIMARY KEY (ids_of_subject_section) )
Advertisement
Answer
If you don’t want duplicates inserted, then add a unique index or constraint:
CREATE UNIQUE INDEX unq_table_subject_topics_exams_3 ON table_subject_topics_exams(name_of_subject, section, topic, subtopic);
If you want your insert
to succeed for the non-duplicated values, then add:
ON CONFLICT DO NOTHING
as the last line of the INSERT
.
Here is a db<>fiddle.