Ver Mensaje Individual
  #7 (permalink)  
Antiguo 04/01/2012, 14:43
nosek159
 
Fecha de Ingreso: octubre-2008
Mensajes: 118
Antigüedad: 15 años, 7 meses
Puntos: 2
Respuesta: duda alter foreign key

estas son las tablas en cuestion:

CREATE TABLE `facturas` (
`id_factura` int(10) unsigned NOT NULL,
`anio` int(4) NOT NULL,
`descripcion` text NOT NULL,
`observaciones` text NOT NULL,
`fecha_factura` date NOT NULL,
`precio` float NOT NULL,
`importe_descuento` float NOT NULL,
`descuento_global` float NOT NULL,
`iva` float NOT NULL,
`franquicia` float NOT NULL default '0',
`tipo_pago` varchar(15) NOT NULL default 'Al contado',
`anio_presupuesto` int(4) NOT NULL,
`id_presupuesto` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_factura`,`anio`),
KEY `id_presupuesto` (`id_presupuesto`),
KEY `anio_presupuesto` (`anio_presupuesto`),
CONSTRAINT `facturas_ibfk_1` FOREIGN KEY (`id_presupuesto`) REFERENCES `presupuestos` (`id_presupuesto`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




CREATE TABLE `presupuestos` (
`id_presupuesto` int(10) unsigned NOT NULL,
`anio` int(4) NOT NULL,
`descripcion` text NOT NULL,
`observaciones` text NOT NULL,
`fecha_presupuesto` date NOT NULL,
`precio` float NOT NULL,
`importe_descuento` float NOT NULL default '0',
`descuento_global` float NOT NULL,
`iva` float NOT NULL,
`franquicia` float NOT NULL default '0',
`tipo` varchar(50) NOT NULL,
`id_averia` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_presupuesto`,`anio`),
KEY `id_averia` (`id_averia`),
KEY `tipo` (`tipo`),
CONSTRAINT `presupuestos_ibfk_1` FOREIGN KEY (`id_averia`) REFERENCES `resguardo_deposito` (`id_averia`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



como se puede ver el foreing key de facturas esta mal, ademas de relacionar el id se deberia de relacionar el año.

He elimidado el foreign key con la siguiente instruccion: ALTER TABLE facturas DROP FOREIGN KEY facturas_ibfk_1;
Lo que ahora nose es añadr el foreign key correcto con el id y el año