无法在Mysql中创建表

时间:2017-02-20 19:01:02

标签: mysql foreign-keys

我正在创建一个最伟大的电影数据库,并且我收到了错误:

第19行的错误1005(HY000):无法创建表格Greatest_Movies.genre' (错误:150)

我查过这篇文章: Error Code: 1005. Can't create table '...' (errno: 150)并尝试了许多事情,但没有成功。

这实际上是:

 DROP DATABASE `Greatest_Movies`;
 CREATE DATABASE  `Greatest_Movies`;
 USE `Greatest_Movies` ;

CREATE TABLE IF NOT EXISTS `movie` (
 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
 `film` VARCHAR(255) NOT NULL,
 `director` VARCHAR(255) NOT NULL,
 `release_year` VARCHAR(255) NOT NULL,
 `oscars` TINYINT NULL,
 `IMDB_link` VARCHAR(255) NOT NULL,
 `film_page` VARCHAR(255) NOT NULL,
 `country` VARCHAR(255) NOT NULL,
 `genre` TINYINT UNSIGNED NOT NULL,

 PRIMARY KEY (`id`) )
 ENGINE = InnoDB;

 CREATE TABLE IF NOT EXISTS `genre` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id`) REFERENCES `movie`(`genre`))
ENGINE = InnoDB;

我失踪了什么? 此致

3 个答案:

答案 0 :(得分:1)

此脚本工作并经过测试,请尝试:

DROP DATABASE `Greatest_Movies`;
 CREATE DATABASE  `Greatest_Movies`;
 USE `Greatest_Movies` ;

DROP TABLE IF EXISTS `genre`;
CREATE TABLE `genre` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `film` varchar(255) NOT NULL,
  `director` varchar(255) NOT NULL,
  `release_year` varchar(255) NOT NULL,
  `oscars` tinyint(4) DEFAULT NULL,
  `IMDB_link` varchar(255) NOT NULL,
  `film_page` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `genre` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `genre_fk` (`genre`),
  CONSTRAINT `genre_fk` FOREIGN KEY (`genre`) REFERENCES `genre` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

答案 1 :(得分:0)

我认为您需要从此行的末尾删除逗号:

`genre` TINYINT UNSIGNED NOT NULL,

以及之后:

`name` VARCHAR(255) NOT NULL,

答案 2 :(得分:0)

@Solarflare在评论中说,我做了这个改变:

DROP DATABASE `Greatest_Movies`;
CREATE DATABASE  `Greatest_Movies`;
USE `Greatest_Movies`;


CREATE TABLE IF NOT EXISTS `genre` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`))

ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `movie` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`film` VARCHAR(255) NOT NULL,
`director` VARCHAR(255) NOT NULL,
`release_year` VARCHAR(255) NOT NULL,
`oscars` TINYINT NULL,
`IMDB_link` VARCHAR(255) NOT NULL,
`film_page` VARCHAR(255) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`genre` TINYINT UNSIGNED NOT NULL,

 PRIMARY KEY (`id`),
 FOREIGN KEY (`genre`) REFERENCES `genre`(`id`))
ENGINE = InnoDB;