Database / Mnesia basics Interview questions
How do you write a record to a Mnesia table?
mnesia:write/1 inserts or updates a record inside a transaction — if a record with the
same primary key already exists, it's overwritten; otherwise a new one is added.
-record(person, {id, name, age}). mnesia:transaction(fun() -> mnesia:write(#person{id = 1, name = "Ada", age = 34}) end).
Because mnesia:write/1 must run inside a transaction context (typically via
mnesia:transaction/1 or mnesia:sync_transaction/1), Mnesia can guarantee the write is
applied consistently across every replicated copy of the table, or not at all if the transaction aborts. Trying
to call mnesia:write/1 outside of any transaction context raises an error, since there's no
transaction for it to participate in.
More Related questions...