Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

sql - Insert data into Postgresql with duplicate values

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)
)
question from:https://stackoverflow.com/questions/65642402/insert-data-into-postgresql-with-duplicate-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...