ho un problema con l'aggiunta di una foreign key.
codice:
mysql> desc book;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| book_id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | | NULL | |
| author | int(11) | NO | MUL | NULL | |
| editor | int(11) | NO | MUL | NULL | |
| price | decimal(7,2) | NO | | NULL | |
| position | int(11) | NO | MUL | NULL | |
+----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> desc position;
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| position_id | int(11) | NO | PRI | NULL | auto_increment |
| position_name | varchar(20) | YES | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
ho provato da terminale, da mysql-query-browser e phpmyadmin.
vi riporto l'errore di quest'ultimo:
codice:
ALTER TABLE `book` ADD FOREIGN KEY ( `position` ) REFERENCES `book`.`position` (
`position_id`
) ON DELETE NO ACTION ON UPDATE NO ACTION ;
Messaggio di MySQL: Documentazione
#1452 - Cannot add or update a child row: a foreign key constraint fails (`book`.<result 2 when explaining filename '#sql-f66_95'>, CONSTRAINT `#sql-f66_95_ibfk_3` FOREIGN KEY (`position`) REFERENCES `position` (`position_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
ERRORE
ALTER TABLE `book` ADD FOREIGN KEY ( `position` ) REFERENCES `book`.`position` (
`position_id`
) ON DELETE NO ACTION ON UPDATE NO ACTION ;
nn riesco a capire perchè le altre due foreign key le ha aggiunte senza problemi e le strutture delle tabella sono identiche.
ps: la tabella principale si chiama come il db, poca fantasia......