[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [SID] MySQL et FOREIGN KEY



Bon ça y est ça marche, je m'explique:

Il fallait commenter dans le fichier /etc/mysql/my.cnf
la ligne skip-innodb pour activer le support InnoDB.

Ensuite, j'ai lu dans la doc qu'il fallait créer un index pour pouvoir
exploiter les FOREIGN KEY, j'ai donc modifié mon script en conséquence:

CREATE TABLE Periodicite (
Num_Periode INT(255) NOT NULL AUTO_INCREMENT,
Nom_Periode VARCHAR(30) NOT NULL,
PRIMARY KEY (Num_Periode)
) TYPE=InnoDB ;
 
CREATE TABLE Type (
Num_Type INT(255) NOT NULL AUTO_INCREMENT,
Nom_Type VARCHAR(30) NOT NULL,
PRIMARY KEY (Num_Type)
) TYPE=InnoDB ;
 
CREATE TABLE Cmd_gcos (
Num_Commande INT(255) NOT NULL AUTO_INCREMENT,
Nom_Commande VARCHAR(100) NOT NULL,
Description TEXT NOT NULL,
Num_Type INT(255),
Num_Periode INT(255),
PRIMARY KEY (Num_Commande,Num_Type),
INDEX N_Per (Num_Periode),
INDEX N_Type (Num_Type),
FOREIGN KEY (Num_Periode) REFERENCES Periodicite(Num_Periode),
FOREIGN KEY (Num_Type) REFERENCES Type(Num_Type)
) TYPE=InnoDB ;

Ensuite on peut vérifier par un 
SHOW TABLE STATUS FROM Gcos LIKE 'Cmd_gcos';

+----------+--------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+-------------+-------------+------------+----------------+---------------------------------------------------------------------------------------------------------------+
| Name     | Type   | Row_format | Rows | Avg_row_length | Data_length |
Max_data_length | Index_length | Data_free | Auto_increment |
Create_time | Update_time | Check_time | Create_options |
Comment                                                                                                       |
+----------+--------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+-------------+-------------+------------+----------------+---------------------------------------------------------------------------------------------------------------+
| Cmd_gcos | InnoDB | Dynamic    |    0 |              0 |       16384
|            NULL |        32768 |         0 |              1 |
NULL        | NULL       | NULL       |                | InnoDB free:
4096 kB; (Num_Periode) REFER Gcos/Periodicite(Num_Periode); (Num_Type)
REFER Gcos/Type(Num_Type) |
+----------+--------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+-------------+-------------+------------+----------------+---------------------------------------------------------------------------------------------------------------+

ma table est donc bien de type InnoDB.

On peut ensuite regarder le mysqldump ci-dessous pour bien voir que
MySQL a bien pris en compte les ordres de création:

-- MySQL dump 9.10
--
-- Host: localhost    Database: Gcos
-- ------------------------------------------------------
-- Server version       4.0.17-log
 
--
-- Table structure for table `Cmd_gcos`
--
 
CREATE TABLE Cmd_gcos (
  Num_Commande int(255) NOT NULL auto_increment,
  Nom_Commande varchar(100) NOT NULL default '',
  Description text NOT NULL,
  Num_Type int(255) NOT NULL default '0',
  Num_Periode int(255) default NULL,
  PRIMARY KEY  (Num_Commande,Num_Type),
  KEY N_Per (Num_Periode),
  KEY N_Type (Num_Type),
  CONSTRAINT `0_56` FOREIGN KEY (`Num_Periode`) REFERENCES `Periodicite`
(`Num_Periode`),
  CONSTRAINT `0_57` FOREIGN KEY (`Num_Type`) REFERENCES `Type`
(`Num_Type`)
) TYPE=InnoDB;
 
--
-- Table structure for table `Periodicite`
--
 
CREATE TABLE Periodicite (
  Num_Periode int(255) NOT NULL auto_increment,
  Nom_Periode varchar(30) NOT NULL default '',
  PRIMARY KEY  (Num_Periode)
) TYPE=InnoDB;

--
-- Table structure for table `Type`
--
 
CREATE TABLE Type (
  Num_Type int(255) NOT NULL auto_increment,
  Nom_Type varchar(30) NOT NULL default '',
  PRIMARY KEY  (Num_Type)
) TYPE=InnoDB;

Voilà, donc maintenant ça marche encore merci à tous !

Laurent Oliva




Reply to: