mnesia foreign keys

Hakan Mattsson hakan@REDACTED
Mon Jan 19 15:42:07 CET 2004

Rudolph> I am thinking of how to get mnesia to enforce foreign key
Rudolph> relationships in our database. I.e. to get some key-violation when you
Rudolph> delete a record on which another record in the same or another table
Rudolph> depends.
Rudolph> Looking at the Mnesia user's guide, this section seems to imply that
Rudolph> such functionality already exists. Can someone explain to me how to
Rudolph> use this? Is it possible to use this foreign key mechanism for what we
Rudolph> need? I think I once read that Ulf Wiger had written some Relational
Rudolph> DB library for mnesia?

The current foreign key mechanism in Mnesia is NOT what you are looking for. 

It is merely a way of obtaining co-location of records in two fragmented
tables. The typical scenario is that you have a subscriber table which you
access frequently. If you put some more seldomly accessed attributes in another
table and have a secondary key containing the subscriber key, Mnesia will
co-locate the two subscriber records by feeding the hash function with the
(secondary) subscriber key. This mechanism scales extremely well. Try the
bench example (mnesia/examples/bench) if you want to explore the

As Mnesia does not support traditional foreign keys, you need to do it
yourself. Either you could take a look into Uffes RDBMS contribution.
Or you could do it by the ruling your own Mnesia access callback
module. Instead of hardcoding the integrity rules in the callback module,
you can store this schema info as "user_properties" for the table.


Håkan Mattsson
High Availability Software, DBMS Internals

More information about the erlang-questions mailing list