IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Requêtes MySQL Discussion :

Pourquoi MySQL n'accepte pas ma FOREIGN KEY


Sujet :

Requêtes MySQL

  1. #1
    Membre expérimenté
    Avatar de Gouyon
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2003
    Messages
    1 076
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2003
    Messages : 1 076
    Points : 1 521
    Points
    1 521
    Billets dans le blog
    5
    Par défaut Pourquoi MySQL n'accepte pas ma FOREIGN KEY
    Salut à tous

    J'ai une base avec plusieurs table et parmi ces tables j'en ai 2 que je veux lier par une FOREIGN KEY. Je pense avoir tout fait comme il faut mais MySQL me jette avec le message d'erreur suivant:
    1005 Can't create table ca.#sqL-364_3 (errno:150)

    Voici un bout de ma base
    Les deux table liées sont la table t_participants et le table t_partie
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
     
    DROP DATABASE IF EXISTS `ca`;
    CREATE DATABASE `ca` DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;
    USE `ca`;
     
    -- ----------------------------
    -- Table structure for `t_cartes`
    -- ----------------------------
    DROP TABLE IF EXISTS `t_cartes`;
    CREATE TABLE `t_cartes` (
      `RefCarte` int(11) NOT NULL AUTO_INCREMENT,
      `Nom` varchar(80) NOT NULL,
      `Largeur` smallint(2) NOT NULL,
      `Hauteur` smallint(2) NOT NULL,
      PRIMARY KEY (`RefCarte`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
     
    -- ----------------------------
    -- Records of t_cartes
    -- ----------------------------
    INSERT INTO `t_cartes` VALUES ('1', 'CarteA', '50', '70');
     
    -- ----------------------------
    -- Table structure for `t_participants`
    -- ----------------------------
    DROP TABLE IF EXISTS `t_participants`;
    CREATE TABLE `t_participants` (
      `CodeID` int(10) NOT NULL,
      `Nom` varchar(32) NOT NULL,
      `Prenom` varchar(32) NOT NULL,
      `Email` varchar(64) NOT NULL,
      `Status` varchar(3) CHARACTER SET armscii8 NOT NULL,
      `Camps` int(10) NOT NULL,
      `RefNavLeader` int(10) NOT NULL,
      PRIMARY KEY (`CodeID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
     
    -- ----------------------------
    -- Records of t_participants
    -- ----------------------------
     
    -- ----------------------------
    -- Table structure for `t_partie`
    -- ----------------------------
    DROP TABLE IF EXISTS `t_partie`;
    CREATE TABLE `t_partie` (
      `SitInitiale` tinyint(1) NOT NULL DEFAULT '0',
      `RefNavire` int(10) NOT NULL,
      `IndexListe` smallint(2) unsigned NOT NULL DEFAULT '0',
      `CodeGroupe` int(10) unsigned DEFAULT '1',
      `QualiteEquipage` tinyint(1) unsigned NOT NULL DEFAULT '2',
      `Greement1` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Greement2` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Greement3` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Greement4` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Coque1` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Coque2` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Coque3` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Coque4` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `Sec1` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `ActSec1` tinyint(1) NOT NULL DEFAULT '0',
      `S1LibreDans` tinyint(1) DEFAULT NULL,
      `Sec2` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `ActSec2` tinyint(1) NOT NULL,
      `S2LibreDans` tinyint(1) DEFAULT NULL,
      `Sec3` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `ActSec3` tinyint(1) NOT NULL,
      `S3LibreDans` tinyint(1) DEFAULT NULL,
      `Sec4` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `ActSec4` tinyint(1) NOT NULL,
      `S4LibreDans` tinyint(1) DEFAULT NULL,
      `SecM` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `ActSecM` tinyint(1) NOT NULL,
      `SMLibreDans` tinyint(1) DEFAULT NULL,
      `GunDismBa` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `GunDismTr` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `BrdBaMasquee` tinyint(1) NOT NULL DEFAULT '0',
      `BrdTrMasquee` tinyint(1) NOT NULL DEFAULT '0',
      `BrdInitBa` tinyint(1) NOT NULL DEFAULT '1',
      `BrdInitTr` tinyint(1) NOT NULL DEFAULT '1',
      `PosX` smallint(2) NOT NULL,
      `PosY` smallint(2) NOT NULL,
      `Cap` tinyint(1) NOT NULL,
      `OrdresMvtExec` varchar(32) NOT NULL,
      `EtatVoilure` tinyint(1) NOT NULL DEFAULT '1',
      `EstCapture` tinyint(1) NOT NULL DEFAULT '0',
      `NouvCode` smallint(2) NOT NULL DEFAULT '0',
      `VaExploser` tinyint(1) NOT NULL DEFAULT '0',
      `VaCouler` tinyint(1) NOT NULL DEFAULT '0',
      `SeRend` tinyint(1) NOT NULL DEFAULT '0',
      `Detruit` tinyint(1) NOT NULL DEFAULT '0',
      `Fatigue` tinyint(1) NOT NULL DEFAULT '0',
      `QualEquipPrise` tinyint(1) NOT NULL DEFAULT '0',
      `NbEquipPrise` tinyint(1) NOT NULL DEFAULT '0',
      PRIMARY KEY (`SitInitiale`,`RefNavire`),
      INDEX `indGroupe` (`CodeGroupe`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    et la requete d'ajout de clef

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ALTER TABLE `t_partie` ADD FOREIGN KEY (`CodeGroupe`) REFERENCES `t_participants` (`CodeID`) ON DELETE SET NULL ON UPDATE CASCADE;
    Si quelqu'un a une idée parce que là ça fait un moment que je tourne en rond.
    Il y a des jours où j'éprouve une haine profonde envers microsoft et Apple c'est pas mieux
    Mon modeste site et mes modestes oeuvres sont
    Rémi

  2. #2
    Membre habitué
    Femme Profil pro
    Database
    Inscrit en
    Juin 2010
    Messages
    125
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Database

    Informations forums :
    Inscription : Juin 2010
    Messages : 125
    Points : 135
    Points
    135
    Par défaut
    Bonjour,

    Les 2 champs que tu essayes de lier ne correspondent pas (au niveau des types...)

    Dans la table "t_participants" tu déclares le champs `CodeID`comme un entier et dans la table "t_partie" tu déclares le champs `CodeGroupe` comme un entier non signé ce qui n'est pas la même chose

    Remplace la déclaration du champs `CodeGroupe` dans ton code par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    `CodeGroupe` int(10) DEFAULT '1',
    et ca devrait fonctionner....

    Bonne continuation

  3. #3
    Membre expérimenté
    Avatar de Gouyon
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2003
    Messages
    1 076
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Novembre 2003
    Messages : 1 076
    Points : 1 521
    Points
    1 521
    Billets dans le blog
    5
    Par défaut
    Arrrgh je me doutais que c'était une c..rie de ce type mais impossible de mettre la main dessus.

    Bon ça marche merci bien
    Il y a des jours où j'éprouve une haine profonde envers microsoft et Apple c'est pas mieux
    Mon modeste site et mes modestes oeuvres sont
    Rémi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Create table : erreur sur foreign key
    Par Naksh-i dans le forum Langage SQL
    Réponses: 1
    Dernier message: 26/10/2006, 23h01
  2. Foreign Key ou pas?
    Par mona dans le forum Oracle
    Réponses: 1
    Dernier message: 12/10/2005, 17h53
  3. Foreign Key sur Mysql
    Par lemagicien dans le forum Outils
    Réponses: 1
    Dernier message: 23/09/2005, 13h39
  4. mysql et foreign key
    Par skoukni_brahim dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 02/06/2005, 18h14
  5. foreign key dans mysql
    Par guillevin dans le forum SQL Procédural
    Réponses: 4
    Dernier message: 08/04/2004, 15h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo