I find it useful to use a composite PK when I want to get the database
to enforce business rules.  For example, to take Anru's example above
of the [order]>--<[line] many-to-many relationship, you are left with
the problem of how to manage adding product lines to the order.

You could do this:

  query table WHERE order_id=order AND product_id=product
  no rows returned?
    INSERT row and set order_id=order AND product_id=product
  else
    UPDATE existing row

But I prefer to use the database engine to do my dirty work.  Put the
PK on order_id + product_id, then:

  INSERT row and set order_id=order AND product_id=product
  Trap for "Would create duplicate values and violate Primary Key"
error
  In errorhandler:
    UPDATE existing row

Hope this example makes sense.  Perhaps other members could comment
whether this is a favoured practice or not?

-stephen/.

-- 
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]

Reply via email to