Question

I want to make 3 Tables like this :

wc_groups Table

CREATE TABLE IF NOT EXISTS `wc_groups` (
  `id` int(2) unsigned NOT NULL AUTO_INCREMENT,
  `idgroup` int(7) NOT NULL,
  `title` varchar(10) NOT NULL,
  `content` text,
  `status` smallint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idgroup` (`idgroup`),
  KEY `status` (`status`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

wc_matches Table

CREATE TABLE IF NOT EXISTS `wc_matches` (
  `id` int(4) unsigned NOT NULL AUTO_INCREMENT,
  `time` date NOT NULL,
  `group_id` int(2) unsigned NOT NULL,
  `place` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `team_id_1` int(10) unsigned NOT NULL,
  `team_id_2` int(10) unsigned NOT NULL,
  `edituser` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `group_id_foreign` (`group_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

wc_teams Table

CREATE TABLE IF NOT EXISTS `wc_teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `group_id` int(2) unsigned NOT NULL,
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

ALTER TABLE `wc_teams`
  ADD CONSTRAINT `group_id_foreign` FOREIGN KEY (`group_id`) REFERENCES `wc_groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Why i got an error when execution Code to create table wc_team ?

What's possible to use What Is Possible Use One Reference Field ( wc_groups (id) ) To Multiple Foreign Key Constraint ?

Was it helpful?

Solution

I don't understand what you mean with multiple foreign keys.

But the problem in your wc_teams create query is that you have a lost , behind

 `group_id` int(2) unsigned NOT NULL,

But you also miss your primary key so i guess you need this

CREATE TABLE IF NOT EXISTS `wc_teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `group_id` int(2) unsigned NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top