如何解决错误“错误:错误1215:无法添加外键约束”?

时间:2019-10-28 22:55:41

标签: mysql database mysql-workbench

我在MySQL-Workbench中创建了一个图(EER),尝试执行“将工程师转发到数据库”给了我这个错误“错误1215:无法添加外键约束”,并且无法识别代码中的错误。 / p>

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////

代码:

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////

模式loja1182

CREATE SCHEMA IF NOT EXISTS `loja1182` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;
USE `loja1182` ;

loja1182distritos

CREATE TABLE IF NOT EXISTS `loja1182`.`distritos` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `distrito` VARCHAR(50) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

loja1182clientes

CREATE TABLE IF NOT EXISTS `loja1182`.`clientes` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `nome` VARCHAR(100) NULL,
  `morada` VARCHAR(100) NULL,
  `data_registo` DATE NOT NULL,
  `nrEncomendas` INT(11) NULL,
  `idDistrito` INT(11) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_clientes_table11_idx` (`idDistrito` ASC),
  CONSTRAINT `fk_clientes_table11`
    FOREIGN KEY (`idDistrito`)
    REFERENCES `loja1182`.`distritos` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

loja1182utilizadores

CREATE TABLE IF NOT EXISTS `loja1182`.`utilizadores` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `email` VARCHAR(100) NULL,
  `password` VARCHAR(20) NULL,
  `total_acessos` INT NULL,
  `idCliente` INT(11) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_utilizadores_clientes_idx` (`idCliente` ASC),
  CONSTRAINT `fk_utilizadores_clientes`
    FOREIGN KEY (`idCliente`)
    REFERENCES `loja1182`.`clientes` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

loja1182tiposencomenda

CREATE TABLE IF NOT EXISTS `loja1182`.`tiposencomenda` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `tipo` VARCHAR(20) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

loja1182estadosencomenda

CREATE TABLE IF NOT EXISTS `loja1182`.`estadosencomenda` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `estado` VARCHAR(20) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

loja1182encomendas

CREATE TABLE IF NOT EXISTS `loja1182`.`encomendas` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `idCliente` INT(11) UNSIGNED NOT NULL,
  `data` DATE NULL,
  `idEstado` INT(11) UNSIGNED NOT NULL,
  `idTipo` INT(11) UNSIGNED NOT NULL,
  `valor` FLOAT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_encomendas_clientes1_idx` (`idCliente` ASC),
  INDEX `fk_encomendas_tiposencomenda1_idx` (`idTipo` ASC),
  INDEX `fk_encomendas_estadosencomenda1_idx` (`idEstado` ASC),
  CONSTRAINT `fk_encomendas_clientes1`
    FOREIGN KEY (`idCliente`)
    REFERENCES `loja1182`.`clientes` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_encomendas_tiposencomenda1`
    FOREIGN KEY (`idTipo`)
    REFERENCES `loja1182`.`tiposencomenda` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_encomendas_estadosencomenda1`
    FOREIGN KEY (`idEstado`)
    REFERENCES `loja1182`.`estadosencomenda` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

loja1182categorias

CREATE TABLE IF NOT EXISTS `loja1182`.`categorias` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `categoria` VARCHAR(50) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

loja1182iva

CREATE TABLE IF NOT EXISTS `loja1182`.`iva` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `taxa` FLOAT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

loja1182artigos

CREATE TABLE IF NOT EXISTS `loja1182`.`artigos` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `descricao` VARCHAR(100) NULL,
  `precos` FLOAT NULL,
  `idIva` INT(11) UNSIGNED NOT NULL,
  `stock` INT(11) NOT NULL,
  `idCategoria` INT(11) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_artigos_categorias1_idx` (`idCategoria` ASC),
  INDEX `fk_artigos_iva1_idx` (`idIva` ASC),
  CONSTRAINT `fk_artigos_categorias1`
    FOREIGN KEY (`idCategoria`)
    REFERENCES `loja1182`.`categorias` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_artigos_iva1`
    FOREIGN KEY (`idIva`)
    REFERENCES `loja1182`.`iva` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

loja1182linhasencomenda

CREATE TABLE IF NOT EXISTS `loja1182`.`linhasencomenda` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `idEncomenda` INT(11) UNSIGNED NOT NULL,
  `idArtigo` INT(11) UNSIGNED NOT NULL,
  `quantidade` INT(11) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_linhasencomenda_encomendas1_idx` (`idEncomenda` ASC),
  INDEX `fk_linhasencomenda_artigos1_idx` (`idArtigo` ASC),
  CONSTRAINT `fk_linhasencomenda_encomendas1`
    FOREIGN KEY (`idEncomenda`)
    REFERENCES `loja1182`.`encomendas` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_linhasencomenda_artigos1`
    FOREIGN KEY (`idArtigo`)
    REFERENCES `loja1182`.`artigos` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////

错误:

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////

在服务器中执行SQL脚本 错误:错误1215:无法添加外键约束 SQL代码:

    Table `loja1182`.`encomendas`

    CREATE TABLE IF NOT EXISTS `loja1182`.`encomendas` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `idCliente` INT(11) UNSIGNED NOT NULL,
      `data` DATE NULL,
      `idEstado` INT(11) UNSIGNED NOT NULL,
      `idTipo` INT(11) UNSIGNED NOT NULL,
      `valor` FLOAT NULL,
      PRIMARY KEY (`id`),
      INDEX `fk_encomendas_clientes1_idx` (`idCliente` ASC),
      INDEX `fk_encomendas_tiposencomenda1_idx` (`idTipo` ASC),
      INDEX `fk_encomendas_estadosencomenda1_idx` (`idEstado` ASC),
      CONSTRAINT `fk_encomendas_clientes1`
        FOREIGN KEY (`idCliente`)
        REFERENCES `loja1182`.`clientes` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT `fk_encomendas_tiposencomenda1`
        FOREIGN KEY (`idTipo`)
        REFERENCES `loja1182`.`tiposencomenda` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT `fk_encomendas_estadosencomenda1`
        FOREIGN KEY (`idEstado`)
        REFERENCES `loja1182`.`estadosencomenda` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB

SQL脚本执行完成:语句:成功10次,失败1次

以最终形式获取后视图定义。 什么都没拿

0 个答案:

没有答案