I'm not too sure if there is a special term for this kind of data, but the concept is similar to a C++ std::variant
(but maybe in PostgreSQL it wouldn't be ideal to use a variant-like structure).
As an example, suppose I have a table purchases
that has some columns:
CREATE TABLE purchases (
purchaseid bigserial,
buyerid bigint,
name text,
...
);
Each purchase would be paid via one of a small number of possibilities, e.g. cheque or bitcoin. Each possibility has different associated fields that would need to be saved.
Payment by cheque would require saving the cheque number and the bank name, and payment by bitcoin would require saving the sender's bitcoin address and a return bitcoin address.
It would seem wise (with my little knowledge of PostgreSQL) to have cheque payments and bitcoin payments reside in separate tables due to the differing fields.
What is the ideal way to store such data (and relate them to each row in purchases
) if I know that each purchase can only be paid by either cheque or bitcoin, but not both?