Multiple Column PK

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blyxx86
    Contributor
    • Nov 2006
    • 258

    Multiple Column PK

    Hey there everyone,
    I am somewhat new to MySQL and have some past experience with various databases.

    What I am trying to accomplish seems simple, but I know the method I am going about it is wrong. Some database engines allow what I do, the more accurate ones do not.

    I am attempting to create a table as shown below:
    [code=text]
    CREATE TABLE `mydb`.`INV_Mas terItemList` (
    `MasterItemList _ID` INTEGER UNSIGNED NOT NULL DEFAULT NULL AUTO_INCREMENT,
    `ModelID` INTEGER UNSIGNED NOT NULL,
    `CustomerID` INTEGER UNSIGNED NOT NULL,
    `Serial` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`ModelID`, `CustomerID`, `Serial`)
    )
    ENGINE = InnoDB;
    [/code]

    However, when I do this I get the error that I expect to see (Cannot create an autonumber field that is not a primary key). I want to have a Unique ID that correlates to the line, but not allow duplicate values in the table between those 3 columns.

    Is there another way to do this? Or do I just have to do this through programming to verify the integrity of these 3 columns to have the single Unique ID?
  • blyxx86
    Contributor
    • Nov 2006
    • 258

    #2
    I discovered the "UNIQUE" index portion of MySQL...

    This should perform what I need now:
    [code=text]
    CREATE TABLE `synthesis`.`IN V_MasterItemLis t` (
    `MasterItemList _ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    `Customer_ID` INTEGER UNSIGNED NOT NULL,
    `Model_ID` INTEGER UNSIGNED NOT NULL,
    `Serial` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`MasterItemLis t_ID`),
    UNIQUE INDEX `INDEX_UNIQUE`( `Customer_ID`, `Model_ID`, `Serial`)
    )
    ENGINE = InnoDB;
    [/code]

    Comment

    Working...