0
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;

谁能解释这些查询的问题是什么?

4

1 回答 1

1

您正在使用多态关联,遗憾的是无法通过约束来完成,因为外键不能共享引用列。要使其正常工作,请在以下位置为每个外部表引用它们自己的列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

于 2012-11-01T06:22:45.403 回答