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 に答える