外键无效

时间:2013-10-16 23:53:57

标签: php mysql key workbench

之前我做过这样的研究,但不确定我是否做得很好。

我正在使用MySQL Workbench来创建和管理我的数据库,本周,我创建了一个新数据库。

这是一个屏幕:http://puu.sh/4RSNV.png

我认为不好的是我的表t_games和t_articles / t_news之间的关系。

我希望能够创建包含或不包含链接游戏的新闻或文章。

不在PHP方面,而只在MySQL中。

http://puu.sh/4RSUC.png

我从来没有这样做过,现在我需要它。

这是表格中的MySQL:

  

-- phpMyAdmin SQL Dump
-- version 3.5.1
-- http://www.phpmyadmin.net
--
-- Client: localhost
-- Généré le: Mer 16 Octobre 2013 à 23:49
-- Version du serveur: 5.5.24-log
-- Version de PHP: 5.4.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Base de données: `bifrost`
--

-- --------------------------------------------------------

--
-- Structure de la table `t_articles`
--

CREATE TABLE IF NOT EXISTS `t_articles` (
  `id_article` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  `validate` tinyint(1) DEFAULT NULL,
  `title` varchar(45) DEFAULT NULL,
  `introduction` text,
  `tag` text,
  `t_categories_id_category` int(11) NOT NULL,
  `t_users_id_user` int(11) NOT NULL,
  `t_users_t_status_id_status` int(11) NOT NULL,
  `t_games_id_games` int(11) DEFAULT NULL,
  PRIMARY KEY (`id_article`,`t_categories_id_category`,`t_users_id_user`,`t_users_t_status_id_status`),
  KEY `fk_t_articles_t_categories1` (`t_categories_id_category`),
  KEY `fk_t_articles_t_users1` (`t_users_id_user`,`t_users_t_status_id_status`),
  KEY `fk_t_articles_t_games1` (`t_games_id_games`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_categories`
--

CREATE TABLE IF NOT EXISTS `t_categories` (
  `id_category` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `description` text,
  `ico` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id_category`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_games`
--

CREATE TABLE IF NOT EXISTS `t_games` (
  `id_games` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  `ico` varchar(45) DEFAULT NULL,
  `banner` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id_games`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_news`
--

CREATE TABLE IF NOT EXISTS `t_news` (
  `id_news` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  `title` varchar(45) DEFAULT NULL,
  `content` text,
  `tag` text,
  `t_users_id_user` int(11) NOT NULL,
  `t_games_id_games` int(11) DEFAULT NULL,
  PRIMARY KEY (`id_news`,`t_users_id_user`),
  KEY `fk_t_news_t_users1` (`t_users_id_user`),
  KEY `fk_t_news_t_games1` (`t_games_id_games`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=47 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_news_comment`
--

CREATE TABLE IF NOT EXISTS `t_news_comment` (
  `id_comment` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `content` text,
  `created` datetime DEFAULT NULL,
  `t_news_id_news` int(11) NOT NULL,
  PRIMARY KEY (`id_comment`),
  KEY `fk_t_news_comment_t_news1` (`t_news_id_news`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_pages`
--

CREATE TABLE IF NOT EXISTS `t_pages` (
  `id_page` int(11) NOT NULL AUTO_INCREMENT,
  `t_articles_id_article` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  `content` text,
  PRIMARY KEY (`id_page`),
  KEY `fk_t_pages_t_articles1` (`t_articles_id_article`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_sliders`
--

CREATE TABLE IF NOT EXISTS `t_sliders` (
  `id_slider` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  `title` varchar(80) DEFAULT NULL,
  `image` varchar(45) DEFAULT NULL,
  `link` varchar(125) DEFAULT NULL,
  PRIMARY KEY (`id_slider`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_status`
--

CREATE TABLE IF NOT EXISTS `t_status` (
  `id_status` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `description` text,
  PRIMARY KEY (`id_status`),
  UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_streaming`
--

CREATE TABLE IF NOT EXISTS `t_streaming` (
  `id_streaming` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `title` varchar(45) DEFAULT NULL,
  `description` text,
  `avatar` varchar(45) DEFAULT NULL,
  `twitch` varchar(45) DEFAULT NULL,
  `facebook` varchar(45) DEFAULT NULL,
  `twitter` varchar(45) DEFAULT NULL,
  `youtube` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id_streaming`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `t_users`
--

CREATE TABLE IF NOT EXISTS `t_users` (
  `id_user` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `udpated` datetime DEFAULT NULL,
  `login` varchar(45) DEFAULT NULL,
  `password` varchar(256) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `mail` varchar(256) DEFAULT NULL,
  `avatar` varchar(256) DEFAULT NULL,
  `presentation` text,
  `t_status_id_status` int(11) NOT NULL,
  PRIMARY KEY (`id_user`,`t_status_id_status`),
  KEY `fk_t_users_t_status1` (`t_status_id_status`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='Stockage des utilisateurs spécifique à l''administration du s' AUTO_INCREMENT=3 ;

--
-- Contraintes pour les tables exportées
--

--
-- Contraintes pour la table `t_articles`
--
ALTER TABLE `t_articles`
  ADD CONSTRAINT `fk_t_articles_t_users1` FOREIGN KEY (`t_users_id_user`, `t_users_t_status_id_status`) REFERENCES `t_users` (`id_user`, `t_status_id_status`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_t_articles_t_games1` FOREIGN KEY (`t_games_id_games`) REFERENCES `t_games` (`id_games`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_t_articles_t_categories1` FOREIGN KEY (`t_categories_id_category`) REFERENCES `t_categories` (`id_category`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Contraintes pour la table `t_news`
--
ALTER TABLE `t_news`
  ADD CONSTRAINT `fk_t_news_t_games1` FOREIGN KEY (`t_games_id_games`) REFERENCES `t_games` (`id_games`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_t_news_t_users1` FOREIGN KEY (`t_users_id_user`) REFERENCES `t_users` (`id_user`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Contraintes pour la table `t_news_comment`
--
ALTER TABLE `t_news_comment`
  ADD CONSTRAINT `fk_t_news_comment_t_news1` FOREIGN KEY (`t_news_id_news`) REFERENCES `t_news` (`id_news`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Contraintes pour la table `t_pages`
--
ALTER TABLE `t_pages`
  ADD CONSTRAINT `fk_t_pages_t_articles1` FOREIGN KEY (`t_articles_id_article`) REFERENCES `t_articles` (`id_article`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Contraintes pour la table `t_users`
--
ALTER TABLE `t_users`
  ADD CONSTRAINT `fk_t_users_t_status1` FOREIGN KEY (`t_status_id_status`) REFERENCES `t_status` (`id_status`) ON DELETE NO ACTION ON UPDATE NO ACTION;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

一切都好不好?

1 个答案:

答案 0 :(得分:0)

不要将t_games_id_games放在t_articles中,而是使用单独的关系表:

CREATE TABLE t_article_game (
    t_games_id_games INT NOT NULL,
    t_articles_id_article INT NOT NULL,
    PRIMARY KEY (t_games_id_games, t_articles_id_article),
    FOREIGN KEY (t_games_id_games) REFERENCES t_games (id_games),
    FOREIGN KEY (t_articles_id_articles) REFERENCS t_articles (id_articles)
);

如果您愿意,这可以让您拥有多对多的关系,并允许您根本不参考任何游戏的文章。但它确保当您创建关系时,会保留外键约束 - 您无法将文章链接到不存在的游戏。