Question

I did so research before that but not sure if I did everything good.

I'm using MySQL Workbench to create and manage my databases and this week, I created a new one.

Here is a screen : http://puu.sh/4RSNV.png

The thing I think isn't good is the relations between my table t_games and t_articles/t_news.

I want to be able to create a news or article with or without a linked game.

Not in the PHP side but only in the MySQL.

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

I never did it and I need it now.

Here is the MySQL of the table :

-- 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 */;

Is everything good or not?

Was it helpful?

Solution

Instead of putting t_games_id_games in t_articles, use a separate relation table:

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)
);

This allows you to have many-to-many relationships if you want, as well as allowing articles that don't reference any game at all. But it ensures that when you do create a relationship, the foreign key constraints are maintained -- you can't link an article to a game that doesn't exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top