Que tal Allam: 
 
 
Del manual de mysql [1] saqu� �ste ejemplo: 
 
 
CREATE TABLE parent(id INT NOT NULL, PRIMARY KEY (id)) TYPE=INNODB; 
CREATE TABLE child(id INT, parent_id INT, INDEX par_ind (parent_id), 
             FOREIGN KEY (parent_id) REFERENCES parent(id) 
             ON DELETE SET NULL 
) TYPE=INNODB; 
 
El cual prob� y funcion� de forma correcta. 
 
Cuando intent� hacer un insert en la tabla child sin tener datos en la tabla 
parent me tir� el siguiente error: 
 
mysql> insert into child values (1,1); 
ERROR 1216: Cannot add or update a child row: a foreign key constraint fails 
 
Por otro lado, �Qu� versi�n de mysql est�s utilizando?, En el link de la 
documentaci�n dice: 
 
Starting from version 3.23.50, InnoDB allows you to add a new foreign key 
constraint to a table through 
 
ALTER TABLE yourtablename 
ADD [CONSTRAINT [symbol]] FOREIGN KEY (...) REFERENCES anothertablename(...) 
[on_delete_and_on_update_actions] 
 
No vaya ser que tienes una versi�n algo vieja de mysql. :P 
 
Hasta aqu� llegaba mi respuesta, pero por curiosidad quise ver si realmente 
las sentencias sql funcionban (al menos las que pusiste en este correo) y no 
me funcionaron, tons hay que sentarse un ratico a ver que hiciste. 
 
 
Mensaje citado por Allam Chaves <[EMAIL PROTECTED]>: 
 
> create table paises (codigo char(2) not null, nombre varchar(50), 
> primary key (codigo))TYPE=InnoDb; 
 
Ok, ejecut� esta sentencia para crear la tabla y no me indic� error alguno. 
 
> create table clientes (id varchar(15), nombre varchar(50), pais char(2) 
> not null, email varchar(60), direccion varchar(100), fecha date, empresa 
> varchar(50), primary key(id), index fkpais (pais), foreign key (fkpais) 
> references pais (codigo))TYPE=InnoDB; 
 
 
Ejecut� esta sentencia y me indic� el siguiente error: 
 
 
ERROR 1005: Can't create table './mysqlrefint/clientes.frm' (errno: 150) 
 
Busqu� en la documentaci�n que significaba este error: 
 
Error Code: 1005 
Error Info: 
Occurs when your SQL Statement is formed incorrectly when trying to create a 
table. 
 
Arregl� la sentencia de la siguiente manera: 
 
create table clientes 
( 
   id varchar(15), 
   nombre varchar(50), 
   pais char(2) not null, 
   email varchar(60), 
   direccion varchar(100), 
   fecha date, empresa varchar(50), 
   primary key(id), index pais_index (pais),      <-- Aqu� 
   foreign key (pais) references paises (codigo)  <-- y Aqu� 
) TYPE=InnoDB; 
 
insert into clientes (id,nombre,pais) values (1,'MyCliente',1); 
ERROR 1216: Cannot add or update a child row: a foreign key constraint fails 
 
 
Funcion� de la forma esperada ! 
 
 
Espero que entend�s c�al fu� el problema, sino, pued�s leer los siguientes 
links: 
 
http://www.mysql.com/doc/en/CREATE_INDEX.html 
http://www.mysql.com/doc/en/InnoDB_foreign_key_constraints.html 
 
saludos, 
roche 
 
 
[1] http://www.mysql.com/doc/en/InnoDB_foreign_key_constraints.html 

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/


-- 
Desuscripci�n: escriba a [EMAIL PROTECTED], tema 'unsubscribe'
Problemas a: [EMAIL PROTECTED]  http://www.linux.or.cr/listas

Responder a