Skip to content Skip to sidebar Skip to footer

Using Group Of Columns As A Unique Key In Mysql

I have a table called requests on which the columns are id, placeId, songId, userId id is the primary index of the table. Rest of the columns are only unsigned integers and no othe

Solution 1:

Another method is to add an unique constraint to the table :

ALTERTABLE requests ADDCONSTRAINT placeSong UNIQUE( placeId , songId );

Solution 2:

you can create a UNIQUE index on multiple columns like this

CREATEUNIQUE INDEX placeSong
ON requests (placeId, songId)

Solution 3:

CREATETABLE `tbl` (
    `ID` INT(11) NOTNULL AUTO_INCREMENT,
    `field1` VARCHAR(45) NOTNULLDEFAULT'',
    `field2` VARCHAR(20) NOTNULLDEFAULT'',
    PRIMARY KEY (`ID`),
    UNIQUE INDEX `Index 2` (`field1`, `field2`)
);

Post a Comment for "Using Group Of Columns As A Unique Key In Mysql"