MySQL indexes

Discussion in 'MySQL' started by xms, Aug 24, 2017.

  1. #1
    Hello,

    I have a table:

    CREATE TABLE types
    (
      id                INT(10) UNSIGNED NOT NULL AUTO_INCREMENT
      , info            VARCHAR(128) NOT NULL
      , name            VARCHAR(32) NOT NULL
    
      , PRIMARY KEY (id)
    
      , UNIQUE INDEX (info, name)
    ) ENGINE=InnoDB CHARACTER SET latin1 COLLATE latin1_swedish_ci;
    Code (SQL):
    If I want to have an unique index, should I add the following code?

    CREATE UNIQUE INDEX types_unique_index
        ON types (info, name);
    Code (SQL):
     
    xms, Aug 24, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    WHY do you need that unique index? You have an auto-increment primary key field - that will be unique, so why do you need the separate, unique index?
     
    PoPSiCLe, Aug 24, 2017 IP
  3. xms

    xms Active Member

    Messages:
    169
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    This is just an example.
     
    xms, Aug 24, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Yes, I understand that, but explaining what you want to do, instead of asking "should I do this" usually leads to better answers. Because most times, people asking "should I do this" are on the wrong track to begin with. You should read up on this: What is the XY problem
     
    PoPSiCLe, Aug 24, 2017 IP
  5. xms

    xms Active Member

    Messages:
    169
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #5
    I just do not know if...

    UNIQUE INDEX (info, name)
    Code (SQL):
    ...does the same job as the following code:

    CREATE UNIQUE INDEX types_unique_index
        ON types (info, name);
    Code (SQL):
     
    xms, Aug 24, 2017 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    They are functionally the same, yes, except the latter one has an arbitrary name for the unique multiple index.
     
    PoPSiCLe, Aug 25, 2017 IP