SQL架构级别约束

时间:2015-12-01 14:00:37

标签: mysql sql constraints

我正在尝试实现的某些表之间存在约束,我想我应该在数据库级别上执行它,但我很难为谷歌提出正确的术语,也许你们可以救命! 如果我有桌子:

  • 产品[可乐,香蕉,巧克力等]
  • 产品类型[食品,饮料,化妆品等]
  • 位置[冰箱,架子,橱柜等]

如果 - >多对一,> - <多对多

  • 产品 - > ProductType
  • 产品 - >位置
  • ProductType> - <位置

鉴于冰箱,我们知道冰箱可以包含[食物,饮料],因此在应用程序级别,我们向用户提供仅分配给冰箱的食品和饮料产品。数据库级别有没有办法确保冰箱只包含其允许类型的产品?

下面是我为上面提到的SQL代码,在最后一部分我插入'coke',使用ProductType'drink'。如何确保它不能放在“橱柜”中

CREATE SCHEMA IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `test` ;

-- -----------------------------------------------------
-- Table `test`.`Location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`Location` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test`.`ProductType`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`ProductType` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test`.`Product`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`Product` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `Location_id` INT NULL,
  `ProductType_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_Product_Location_idx` (`Location_id` ASC),
  INDEX `fk_Product_ProductType1_idx` (`ProductType_id` ASC),
  CONSTRAINT `fk_Product_Location`
    FOREIGN KEY (`Location_id`)
    REFERENCES `test`.`Location` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Product_ProductType1`
    FOREIGN KEY (`ProductType_id`)
    REFERENCES `test`.`ProductType` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test`.`ProductType_has_Location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`ProductType_has_Location` (
  `ProductType_id` INT NOT NULL,
  `Location_id` INT NOT NULL,
  PRIMARY KEY (`ProductType_id`, `Location_id`),
  INDEX `fk_ProductType_has_Location_Location1_idx` (`Location_id` ASC),
  INDEX `fk_ProductType_has_Location_ProductType1_idx` (`ProductType_id` ASC),
  CONSTRAINT `fk_ProductType_has_Location_ProductType1`
    FOREIGN KEY (`ProductType_id`)
    REFERENCES `test`.`ProductType` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_ProductType_has_Location_Location1`
    FOREIGN KEY (`Location_id`)
    REFERENCES `test`.`Location` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

INSERT INTO `test`.`Location`
(`name`)
VALUES
('fridge'),('shelf'),('cupboard');

INSERT INTO `test`.`ProductType`
(`name`)
VALUES
('food'),('drink'),('cosmetics');

INSERT INTO `test`.`ProductType_has_Location`
(`ProductType_id`,`Location_id`)
VALUES
(1,1),(2,1),(2,2),(3,3);

INSERT INTO `test`.`Product`
(`name`,`ProductType_id`,`Location_id`)
VALUES
('coke',2,NULL);

1 个答案:

答案 0 :(得分:0)

Product中的另一个约束:

CONSTRAINT `fk_Product_Location_XRef`
FOREIGN KEY (`ProductType_id`,`Location_id`)
REFERENCES `test`.`ProductType_has_Location` (`ProductType_id`,`Location_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION

然后检查类型和位置是否与ProductType_has_Location表中的内容一致。