Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » PostgreSQL »

consulta y consejos con foregn key

Estas en el tema de consulta y consejos con foregn key en el foro de PostgreSQL en Foros del Web. buenas, espero me ayuden con esto, tengo las siguientes tablas productos (id_prod, nombre) detalle_venta(id_prod, id_venta, cantidad) venta(id_venta, fecha) el problema es cuando quiero eliminar un ...
  #1 (permalink)  
Antiguo 16/03/2009, 13:55
 
Fecha de Ingreso: noviembre-2007
Mensajes: 237
Antigüedad: 16 años, 5 meses
Puntos: 4
consulta y consejos con foregn key

buenas, espero me ayuden con esto, tengo las siguientes tablas

productos (id_prod, nombre)
detalle_venta(id_prod, id_venta, cantidad)
venta(id_venta, fecha)

el problema es cuando quiero eliminar un producto por ejemplo (todo esto con php), pero si este ya esta asignado a un detalle_venta, no me dejara elominarlo, aqui el problema como lo elimino, si es que el usuario desea eliminarlo, o modificar el producto, y que este se modifique en detalle_venta, que me recomiendan que haga?? existe alguna manera de hacer esto?, gracias


PD: si no va en este foro, muevanlo, no supe donde colocarlo, ya que trabajo con php y postgres, saludos
  #2 (permalink)  
Antiguo 18/03/2009, 17:37
Avatar de Sergestux  
Fecha de Ingreso: agosto-2007
Ubicación: Tapachula
Mensajes: 1.218
Antigüedad: 16 años, 8 meses
Puntos: 20
Respuesta: consulta y consejos con foregn key

Tienes que especificar al momento de generar la llave foranea que quieres que se haga cuando se elimine un dato de una tabla padre, por ejemplo si pones ON DELETE CASCADE estaras indicando que cuando se borre un archivo de la tabla padre, se va a eliminar en cascada el campo de la tabla hijo y todos los que referencien a ella
Código:
CREATE TABLE order_items (
    product_no integer REFERENCES products ON DELETE RESTRICT,
    order_id integer REFERENCES orders ON DELETE CASCADE,
    quantity integer,
    PRIMARY KEY (product_no, order_id)
);

Si ya esta creada la tabla es cosa de hacerlo con ALTER TABLE
  #3 (permalink)  
Antiguo 18/03/2009, 17:41
Avatar de Sergestux  
Fecha de Ingreso: agosto-2007
Ubicación: Tapachula
Mensajes: 1.218
Antigüedad: 16 años, 8 meses
Puntos: 20
Respuesta: consulta y consejos con foregn key

Cita:
In addition, when the data in the referenced columns is changed, certain actions are performed on the data in this table's columns. The ON DELETE clause specifies the action to perform when a referenced row in the referenced table is being deleted. Likewise, the ON UPDATE clause specifies the action to perform when a referenced column in the referenced table is being updated to a new value. If the row is updated, but the referenced column is not actually changed, no action is done. Referential actions other than the NO ACTION check cannot be deferred, even if the constraint is declared deferrable. There are the following possible actions for each clause:

NO ACTION

Produce an error indicating that the deletion or update would create a foreign key constraint violation. If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows. This is the default action.
RESTRICT

Produce an error indicating that the deletion or update would create a foreign key constraint violation. This is the same as NO ACTION except that the check is not deferrable.
CASCADE

Delete any rows referencing the deleted row, or update the value of the referencing column to the new value of the referenced column, respectively.
SET NULL

Set the referencing column(s) to null.
SET DEFAULT

Set the referencing column(s) to their default values.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:49.