MySQL中的查找表

时间:2014-03-25 02:10:52

标签: mysql foreign-keys

我正在使用PHP / MySQL& amp;我需要帮助。我有一张主表:

CREATE TABLE `m4l_movies` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `Title` varchar(250) NOT NULL,
    `Rating` int(11) NOT NULL,
    `Genre` varchar(250) NOT NULL,
    `Actors` varchar(250) NOT NULL,
    `UserID` int(11) NOT NULL DEFAULT '1',
    PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=115 DEFAULT CHARSET=utf8;

接收具有来自这些查找表的值的表单的输入:

CREATE TABLE `m4l_actors` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `Actor` varchar(255) NOT NULL,
    PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=232 DEFAULT CHARSET=utf8;

CREATE TABLE `m4l_genre` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `Genre` varchar(255) NOT NULL,
    PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=202 DEFAULT CHARSET=utf8;

CREATE TABLE `m4l_movierating` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `Movie_Rating` varchar(250) NOT NULL,
    PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

&安培;我创建了一个视图:

SELECT m4l_movies.ID AS ID,
  m4l_movies.Title AS Title,
  m4l_movierating.Movie_Rating AS Rating,
  m4l_movies.Actors AS Actors,
  m4l_movies.Genre AS Genre
FROM m4l_movies
  JOIN m4l_movierating ON m4l_movierating.ID = m4l_movies.Rating
  INNER JOIN m4l_genre ON m4l_movies.Genre = m4l_genre.ID
  INNER JOIN m4l_actors ON m4l_movies.Actors = m4l_actors.ID
ORDER BY m4l_movies.Title

这是我得到的输出:

----------------------------------------------------------------|
  ID      Title       Rating    Actor         Genre             |
  10      Summer      G         (10,15,25)    (45,115,123)      |
   1      About You   G-1       (63,163,405)  (3,16,51)         |
   5      Dog Years   P         (45,65,95)    (98,163,357)      |
----------------------------------------------------------------|

首先,此视图应返回超过200条记录。其次,我需要知道如何创建查找或其他一些方法来转换NAME& GENRE返回其相应的文本值。一些RATING值如何正确地执行它但我无法让NAME或GENRE正确地执行它。我确定它与我加入桌子的方式有关,但我无法弄清楚我哪里出错了。有人请帮助我。

好的尝试跟随Phil的建议我已经删除了演员,电影中的类型故事&创建了movies_genre& movies_actors

DROP TABLE IF EXISTS `m4l_movies`;
CREATE TABLE `m4l_movies` (
  `ID` int(11) NOT NULL auto_increment,
  `Title` varchar(250) NOT NULL,
  `Year` float NOT NULL,
  `Review` varchar(250) NOT NULL,
  `Rating` int(11) NOT NULL,
  `Image` varchar(250) NOT NULL,
  `Storyline` longtext NOT NULL,
  `Director` varchar(250) NOT NULL,
  `UserID` int(11) NOT NULL default '1',
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


 CREATE TABLE `m4l_movie_actor` (
    movie_id INT(11),
    actor_id INT(11),
    PRIMARY KEY (movie_id, actor_id),
    FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
    FOREIGN KEY (actor_id) REFERENCES m4l_actors (ID)
 );


CREATE TABLE `m4l_movie_genre` (
    movie_id INT(11),
    genre_id INT(11),
    PRIMARY KEY (movie_id, genre_id),
    FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
    FOREIGN KEY (genre_id) REFERENCES m4l_genre (ID)
);


DROP TABLE IF EXISTS m4l_genre;
CREATE TABLE m4l_genre (
  ID int(11) NOT NULL auto_increment,
  Genre varchar(250) NOT NULL,
  PRIMARY KEY  (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS m4l_actors;
CREATE TABLE m4l_actors (
  ID int(11) NOT NULL auto_increment,
  Actor varchar(255) NOT NULL,
  PRIMARY KEY  (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

但是当我尝试创建movie_actor或movie_genre时,我得到了

  

9时29分45秒
  CREATE TABLE`m4l_movie_actor`(movie_id INT(11),actor_id INT(11),
  PRIMARY KEY(movie_id,actor_id),
  FOREIGN KEY(movie_id)REFERENCES m4l_movies(ID),
   FOREIGN KEY(actor_id)参考文献m4l_actors(ID))
  错误代码:1215。无法添加外键约束
  0.000秒

     

09:40:56 CREATE TABLE m4l_movie_genre(movie_id INT(11),genre_id INT(11),
  PRIMARY KEY(movie_id,genre_id),
  FOREIGN KEY(movie_id)REFERENCES m4l_movies(ID),
  FOREIGN KEY(genre_id)参考文献m4l_genre(ID))
  错误代码:1215。无法添加外键约束
  0.016秒

从我可以收集的内容中,只有当数据类型不匹配时才会出现这种情况,但我认为我有所有INT数据类型,为什么我会收到此错误?

1 个答案:

答案 0 :(得分:3)

您似乎已经犯了以不可关联的方式存储关系数据的经典错误。

您应该使用junction tables而不是逗号分隔值。例如......

CREATE TABLE `m4l_movies` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `Title` varchar(250) NOT NULL,
    `Rating` int(11) NOT NULL,
    -- removed Actors and Genre
    `UserID` int(11) NOT NULL DEFAULT '1',
    PRIMARY KEY (`ID`)
);

CREATE TABLE `m4l_movie_actor` (
    movie_id INT(11) NOT NULL,
    actor_id INT(11) NOT NULL,
    PRIMARY KEY (movie_id, actor_id),
    FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
    FOREIGN KEY (actor_id) REFERENCES m4l_actors (ID)
);

-- repeat for genres 

然后,您可以加入联结表,然后加入Actors / Genres表,例如

SELECT ... m4l_actors.Actor ...
FROM m4l_movies
INNER JOIN m4l_movie_actor ON m4l_movies.ID = m4l_movie_actor.movie_id
INNER JOIN m4l_actors ON m4l_movie_actor.actor_id = m4l_actors.ID

如果您希望将结果放在以逗号分隔的列表中,请查看GROUP_CONCAT()