making a combination of two columns unique.

Discussion in 'MySQL' started by mahmood, May 20, 2006.

  1. #1
    How can I make a combination of 2 columns unique in mysql?

    for example I want to have only one business in one town in my table.
     
    mahmood, May 20, 2006 IP
  2. ajayr

    ajayr Active Member

    Messages:
    322
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You can use something like:

    
    CREATE TABLE business (
    businessname VARCHAR(100),
    town VARCHAR(100),
    .
    .
    .
    .
    PRIMARY KEY (businessname, town)
    );
    
    Code (markup):
     
    ajayr, May 20, 2006 IP
    mahmood likes this.
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Ajayr
    Do you happen to know the alter code of it so I can change my current table.
     
    mahmood, May 20, 2006 IP
  4. ajayr

    ajayr Active Member

    Messages:
    322
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Try this:

    
    ALTER TABLE business
    ADD PRIMARY KEY (businessname, town);
    
    Code (markup):
    If you have already defined a primary key for the table, then use this command first:

    
    ALTER TABLE business
    DROP PRIMARY KEY;
    
    Code (markup):
     
    ajayr, May 20, 2006 IP