Package
Package
-------
PackageID
PackageName
CityID
PRIMARY KEY (PackageID)
UNIQUE KEY (PackageName)
FOREIGN KEY (CityID)
REFERENCES City (CityID)
Package versions:
PackageVersion
--------------
PackageID
VersionNumber
PackageType
PRIMARY KEY (PackageID, VersionNumber)
UNIQUE KEY (PackageID, VersionNumber, PackageType)
FOREIGN KEY (PackageID)
REFERENCES Package (PackageID)
and subtypes (of versions):
LandPackageVersion
------------------
PackageID
VersionNumber
PackageType DEFAULT 'L'
PRIMARY KEY (PackageID, VersionNumber, PackageType)
FOREIGN KEY (PackageID, VersionNumber, PackageType)
REFERENCES PackageVersion (PackageID, VersionNumber, PackageType)
FlightPackageVersion
------------------
PackageID
VersionNumber
PackageType DEFAULT 'F'
OriginID
PRIMARY KEY (PackageID, VersionNumber, PackageType)
FOREIGN KEY (PackageID, VersionNumber, PackageType)
REFERENCES PackageVersion (PackageID, VersionNumber, PackageType)
FOREIGN KEY (OriginID)
REFERENCES City (CityID)
When I define PackageVersion (read PackageVariant
) table, I mark VersionNumber (aka idPackageVariant
) as Auto Increment and a part of Primary Key in the order defined above:
PRIMARY KEY (
idHolidayPackage
,idPackageVariant
)
CREATE TABLE IF NOT EXISTS `wah_schema`.`HolidayPackageVariant` (
`idHolidayPackage` INT UNSIGNED NOT NULL ,
`idPackageVariant` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`packageType` VARCHAR(45) NOT NULL ,
**PRIMARY KEY (`idHolidayPackage`, `idPackageVariant`) ,**
UNIQUE INDEX `idHolidayPackage_UNIQUE` (`idHolidayPackage` ASC, `idPackageVariant` ASC, `packageType` ASC) ,
INDEX `fk_HolidayPackageVariant_HolidayPackage` (`idHolidayPackage` ASC) ,
CONSTRAINT `fk_HolidayPackageVariant_HolidayPackage`
FOREIGN KEY (`idHolidayPackage` )
REFERENCES `wah_schema`.`HolidayPackage` (`idHolidayPackage` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
However, due to this bug in mySql, I need to have AutoIncrement Key
as the first part of the Primary Key in a compound key.
So, does the order of keys in compound Primary key make any difference ?
Just for reference, the above design comes from here.
VersionNumber
as auto_increment and PK and(PackageID, VersionNumber)
as Unique. Or vice-versa. But that's not what VersionNumber is supposed to be, isn't it?VersionNumber
be unique to identify a unique variant for a given package ? If I make VersionNumber auto_increment, the combination becomes unique. Also, the unique combination helps in identifying a unique variant for a given package also.One
:Many
relationship betweenPackage
:PackageVariant