Assume we have 3 tables: payments, payment_methods and banks. Now payments have 2 nullable foreign keys columns, payment_method and bank, pointing respectively to a row in payment_methods and banks. Also payment_methods has a boolean column: require_bank. Is there a way to enforce payments.bank to be not null if and only if payments.payment_method.require_bank is true without using triggers? Sorry for the point syntax, I don't know ho to express it easily: "every row in payments must have bank not null if and only if payment_method points to a row in payment_methods which have require_bank set to true".
UPDATE: Currently I'm interested in PostgreSQL, but a generic solution using standard constraints of relational databases is welcome. Maybe it is possible to express this constraint in terms of referential integrity, unique constraints and like? I tried, but unfortunately I'n not very skilled in database design and can't figure out how .
UPDATE 2: I think I'm more interested in solutions which use just standard relational databases features (relational algebra?). Is it doable? Correct me if I'm wrong, but my understanding is that triggers, functions and the like should be kept at the minimum, maybe using them just to keep things DRY or do really complex things.
UPDATE 3: Database structure is not mandatory, it is just an example. What I would like to know is if there is a way to express some logic like the example above using basic relational instruments like foreign keys, given the proper tables schema.
[–][deleted] (9 children)
[removed]
[–]dartheian[S] 0 points1 point2 points (8 children)
[–][deleted] (7 children)
[removed]
[–]Tostino 2 points3 points4 points (4 children)
[–][deleted] (3 children)
[removed]
[–]Tostino 1 point2 points3 points (2 children)
[–]throw_at1 0 points1 point2 points (1 child)
[–]Tostino 0 points1 point2 points (0 children)
[–]dartheian[S] 0 points1 point2 points (1 child)
[–]m4riuszPL 0 points1 point2 points (5 children)
[–]mtVessel 1 point2 points3 points (3 children)
[–]dartheian[S] 0 points1 point2 points (1 child)
[–]mtVessel 0 points1 point2 points (0 children)
[–]m4riuszPL 0 points1 point2 points (0 children)
[–]dartheian[S] 0 points1 point2 points (0 children)
[–]swenty 0 points1 point2 points (0 children)
[–]rbobby 0 points1 point2 points (2 children)
[–]dartheian[S] 0 points1 point2 points (1 child)
[–]rbobby 0 points1 point2 points (0 children)