how do i insert multiple values in mysql and avoid duplicates

后端 未结 3 864
我在风中等你
我在风中等你 2020-12-19 17:54

How would I insert multiple rows or values and avoid duplicates in the following schema.

table schema is

id,subject1,subject2,subject3
相关标签:
3条回答
  • 2020-12-19 18:09

    To add the constraint in now:

    ALTER TABLE {tablename}
    ADD CONSTRAINT {constraintname} UNIQUE (subject1, subject2, subject3)
    
    0 讨论(0)
  • 2020-12-19 18:18

    You want to add the UNIQUE constraint to your table. If you write the UNIQUE constraint out separately, it becomes clearer how to apply it to arbitrary combinations of columns.

    CREATE TABLE table_name (
        subject1 VARCHAR(30),
        subject2 VARCHAR(30),
        subject3 VARCHAR(30),
        UNIQUE (subject1, subject2, subject3)
    );
    
    0 讨论(0)
  • 2020-12-19 18:26

    You need to get around with the unique key on three columns.

    Example of table definition

    CREATE TABLE `table_name` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'PK',
      `subject1` varchar(64) NOT NULL,
      `subject2` varchar(64) NOT NULL,
      `subject3` varchar(64) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `subjects` (`subject1`,`subject1`, `subject3`)
    ) ENGINE=InnoDB
    
    0 讨论(0)
提交回复
热议问题