SQL查询引发错误150

时间:2012-11-01 05:43:30

标签: mysql sql foreign-keys

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

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

-- -----------------------------------------------------
-- Table `rsmad`.`app_flashobjects`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`app_flashobjects` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `app_id` INT UNSIGNED NOT NULL ,
  `flashobject_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rsmad`.`app_html`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`app_html` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `app_id` INT UNSIGNED NOT NULL ,
  `html_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rsmad`.`app_iframes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`app_iframes` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `app_id` INT UNSIGNED NOT NULL ,
  `iframe_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rsmad`.`app_images`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`app_images` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `app_id` INT UNSIGNED NOT NULL ,
  `image_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rsmad`.`app_links`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`app_links` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `app_id` INT UNSIGNED NOT NULL ,
  `link_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rsmad`.`app_text`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`app_text` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `app_id` INT UNSIGNED NOT NULL ,
  `text_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `rsmad`.`apps`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `rsmad`.`apps` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `webpage_id` INT UNSIGNED NOT NULL ,
  `timestamp` DATETIME NULL ,
  `status` ENUM('ok','malicious','good') NOT NULL DEFAULT 'ok' ,
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_apps_app_flashobjects1`
    FOREIGN KEY (`id` )
    REFERENCES `rsmad`.`app_flashobjects` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_apps_app_html1`
    FOREIGN KEY (`id` )
    REFERENCES `rsmad`.`app_html` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_apps_app_iframes1`
    FOREIGN KEY (`id` )
    REFERENCES `rsmad`.`app_iframes` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_apps_app_images1`
    FOREIGN KEY (`id` )
    REFERENCES `rsmad`.`app_images` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_apps_app_links1`
    FOREIGN KEY (`id` )
    REFERENCES `rsmad`.`app_links` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_apps_app_text1`
    FOREIGN KEY (`id` )
    REFERENCES `rsmad`.`app_text` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

有人可以解释这些查询的问题是什么吗?

1 个答案:

答案 0 :(得分:1)

您正在使用polymorphic associations,遗憾的是无法使用约束,因为外键无法共享引用列。为了实现这一点,请在apps

中为每个外表引用它们自己的列
CREATE  TABLE IF NOT EXISTS `rsmad`.`apps` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `webpage_id` INT UNSIGNED NOT NULL ,
  `timestamp` DATETIME NULL ,
  `status` ENUM('ok','malicious','good') NOT NULL DEFAULT 'ok' ,
  `flashobjects_id` INT UNSIGNED NOT NULL,
  `html_id` INT UNSIGNED NOT NULL,
  -- etc
  PRIMARY KEY (`id`) ,
  CONSTRAINT `fk_apps_app_flashobjects1`
    FOREIGN KEY (`flashobjects_id` )
    REFERENCES `rsmad`.`app_flashobjects` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_apps_app_html1`
    FOREIGN KEY (`html_id` )
    REFERENCES `rsmad`.`app_html` (`app_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  -- etc

编辑:实际上,我认为您应该以相反的方式定义外键关系。外键在子表中定义,apps看起来非常像父表。现在,如果删除任何app_%表中的行,则apps中与其相关的条目将被级联删除。