A simple equity database has 3 tables: a ticker table, an exchange table, and a currency table.
The ticker table has two columns: a symbol column (ex: IBM), and a foreign key to a row in the exchange table.
The exchange table has two columns: a name column (ex: NYSE), and a foreign key to a row in the currency table.
The currency table has one column: a symbol (ex: USD).
(I’ve left out the primary keys column for each table).
How do I enforce a constraint that there should not be the same ticker symbol twice for the same currency? i.e., I only want to allow one (IBM + USD). It is not enough to create a unique constraint on (ticker.symbol + ticker.exchange); (IBM + NASDAQ) is invalid if there is already (IBM + NYSE). I thought I could create a view that joined the ticker table and the currency table and create a unique index on (view.ticker + view.currency); however, as far as I can now tell, one cannot create an index on a view.
I don’t think it’s possible to specify this constraint in MySQL, but you could use triggers to check that such combinations don’t already exist (raising an error if they do); note however that you’d need to have triggers before insert on the ticker table and before update on all three tables.