Problema nella creazione di una tavola MySQL

Chiuso
medaro - 11 giu 2009 alle 18:16
 Walt - 12 giu 2009 alle 10:42
Ciao,
Qualcuno può spiegarmi perché mysql ritorna un messaggio di errore quando creo un table BATIMENT :
"ERROR 1072 (42000): Key column 'CATEGORY_ID' doesn't exist in table"

ecco il codice MySQL per la creazione di due table:

create table CATEGORY
(
CATEGORY_ID integer not null auto_increment,
Category_Name varchar(100) binary not null,
Category_Description varchar(250) binary,
primary key (CATEGORY_ID),
unique (CATEGORY_ID,Category_Name)
) ENGINE = InnoDB;

create table BATIMENT
(
BATIMENT_ID integer not null auto_increment,
Batiment_Name varchar(100) binary not null,
Batiment_Description varchar(200) binary,
primary key (BATIMENT_ID),
unique (BATIMENT_ID,Batiment_Name),
INDEX (CATEGORY_ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY (CATEGORY_ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;

**************************************************
La creazione del table CATEGORY si esegue correttamente:

mysql> SHOW COLUMNS FROM CATEGORY ;
+----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+----------------+
| CATEGORY_ID | int(11) | NO | PRI | NULL | auto_increment |
| Category_Name | varchar(100) | NO | | | |
| Category_Description | varchar(250) | YES | | NULL | |
+----------------------+--------------+------+-----+---------+----------------+

1 risposta

ciao,
comincio prima con alcune note:
-inutile segnalare al livello della table CATEGORY che CATEGORY_ID è unico visto che è una primary key
-lo stesso per BATIMENT e primary key BATIMENT_ID
-lo stesso per not null che è implicita nel caso di primary key
-non ho capito l'interesse dell'uso di index(CATEGORY_ID) ???

Per il problema è dovuto alla dichiarazione di CATEGORY_ID nella table BATIMENT .
Prova cosi:

create table BATIMENT
(
BATIMENT_ID integer auto_increment,
Batiment_Name varchar(100) binary not null unique,
Batiment_Description varchar(200) binary,
CATEGORY_ID integer,
Constraint pk_batiment primary key (BATIMENT_ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY (CATEGORY_ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
0
Unisciti ALLA COMMUNITY