当尝试构建一个由两个表组成的mysql数据库时,某些原因我得到错误1215并且我无法找到原因。两个字段都属于同一类型。我试图用MySQL工作台做到这一点。有谁知道这个错误的原因?
-- MySQL Workbench Forward Engineering
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,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Classified_tweets`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Classified_tweets` (
`QueryID` INT NULL,
`TweetID` BIGINT NOT NULL,
`TweetKeyword` VARCHAR(45) NULL,
`TweetUsername` VARCHAR(45) NULL,
`TweetDate` VARCHAR(45) NULL,
`TweetLocation` VARCHAR(45) NULL,
`TweetContent` VARCHAR(150) NULL,
`TweetLabel` TINYINT(1) NULL,
PRIMARY KEY (`TweetID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Words_frequency`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Words_frequency` (
`QueryID` INT NOT NULL,
`Word` VARCHAR(45) NOT NULL,
`Label` INT NULL,
`Frequency` VARCHAR(45) NULL,
PRIMARY KEY (`QueryID`, `Word`),
INDEX `fk_Words_frequency_Classified_tweets_idx` (`QueryID` ASC),
CONSTRAINT `fk_Words_frequency_Classified_tweets`
FOREIGN KEY (`QueryID`)
REFERENCES `mydb`.`Classified_tweets` (`QueryID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
答案 0 :(得分:0)
您已将Classified_tweets
上的主键定义为TweetID
。
您应该将此列用于外键关系。也许:
CREATE TABLE IF NOT EXISTS `mydb`.`Words_frequency` (
`TweetID` BIGINT NOT NULL,
`Word` VARCHAR(45) NOT NULL,
`Label` INT NULL,
`Frequency` VARCHAR(45) NULL,
PRIMARY KEY (`QueryID`, `Word`),
INDEX `fk_Words_frequency_Classified_tweets_idx` (`QueryID` ASC),
CONSTRAINT `fk_Words_frequency_Classified_tweets`
FOREIGN KEY (`TweetID`)
REFERENCES `mydb`.`Classified_tweets` (`TweetID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
注意:我将第一列从QueryId
更改为TweetId
。这应该可以解决问题,尽管你的意图可能是别的。
答案 1 :(得分:0)
您只能引用作为主键或唯一属性的列。如果要引用查询ID本身,请将其作为主键或唯一属性。或更改Words_frequency表以引用tweetID。