解释用于外键/主键分配的模型图

时间:2019-03-24 14:58:20

标签: mysql sql

https://imgur.com/a/HAlSO7z

我对应该标识为Flatpack组件表的主键的内容完全感到困惑。对于flatpack组件,与其他两个表都存在多重关系,尽管直觉上我倾向于组件表,因为这似乎更相关,并且多重性是0到Many to 1,而不是关联的Many to 1带有扁平包装组件。

我尝试了FlatpackID和componentNo的复合键,但是在尝试用示例数据填充表时遇到了错误。

目前,我有以下表格:

CREATE TABLE `flatpack_apn` (
    `flatpackid` INT(11) NOT NULL AUTO_INCREMENT,
    `name` TINYTEXT NOT NULL,
    `colour` TEXT NULL DEFAULT NULL,
    `flatpacktype` ENUM('Office','Kitchen','Bedroom','General') NOT NULL,
    `unitprice` DECIMAL(5,2) NULL DEFAULT NULL,
    PRIMARY KEY (`flatpackid`)
)
COLLATE='hp8_english_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2217
;

CREATE TABLE `component_apn` (
    `componentno` INT(11) NOT NULL AUTO_INCREMENT,
    `description` VARCHAR(30) NOT NULL,
    PRIMARY KEY (`componentno`)
)
COLLATE='hp8_english_ci'
ENGINE=InnoDB
AUTO_INCREMENT=20
;

对于flatpackcomponents_apn表(争用中的表),我暂时放置了以下代码:

CREATE TABLE `flatpackcomponents_apn` (
    `flatpackid` INT(11) NOT NULL DEFAULT '12',
    `componentno` INT(11) NOT NULL DEFAULT '23',
    `quantity` INT(10) NOT NULL,
    PRIMARY KEY (`componentno`, `flatpackid`),
    INDEX `flatpackid` (`flatpackid`),
    INDEX `componentno` (`componentno`),
    CONSTRAINT `flatpackcomponents_apn_ibfk_1` FOREIGN KEY (`flatpackid`) REFERENCES `flatpack_apn` (`flatpackid`),
    CONSTRAINT `flatpackcomponents_apn_ibfk_2` FOREIGN KEY (`componentno`) REFERENCES `component_apn` (`componentno`)
)
COLLATE='hp8_english_ci'
ENGINE=InnoDB
;

0 个答案:

没有答案