This chapter introduces Mnesia. Following a brief discussion about the first initial setup, a Mnesia database example is demonstrated. This database example will be referenced in the following chapters, where this example is modified in order to illustrate various program constructs. In this chapter, the following mandatory procedures are illustrated by examples:
Following is a simplified demonstration of a Mnesia system startup. This is the dialogue from the Erlang shell:
erl -mnesia dir '"/tmp/funky"' Erlang (BEAM) emulator version 4.9 Eshell V4.9 (abort with ^G) 1> 1> mnesia:create_schema([node()]). ok 2> mnesia:start(). ok 3> mnesia:create_table(funky, []). {atomic,ok} 4> mnesia:info(). ---> Processes holding locks <--- ---> Processes waiting for locks <--- ---> Pending (remote) transactions <--- ---> Active (local) transactions <--- ---> Uncertain transactions <--- ---> Active tables <--- funky : with 0 records occupying 269 words of mem schema : with 2 records occupying 353 words of mem ===> System info in version "1.0", debug level = none <=== opt_disc. Directory "/tmp/funky" is used. use fall-back at restart = false running db nodes = [nonode@nohost] stopped db nodes = [] remote = [] ram_copies = [funky] disc_copies = [schema] disc_only_copies = [] [{nonode@nohost,disc_copies}] = [schema] [{nonode@nohost,ram_copies}] = [funky] 1 transactions committed, 0 aborted, 0 restarted, 1 logged to disc 0 held locks, 0 in queue; 0 local transactions, 0 remote 0 transactions waits for other nodes: [] ok ]]>
In the example above the following actions were performed:
A Mnesia database is organized as a set of tables. Each table is populated with instances (Erlang records). A table also has a number of properties, such as location and persistence.
In this example we shall:
In this database example, we will create the database and relationships depicted in the following diagram. We will call this database the Company database.
The database model looks as follows:
There are three relationships between these entities:
We first enter our record definitions into a text file
named
The structure defines six tables in our database. In Mnesia,
the function
For example, the table
for employees will be created with the function
The following shell interaction starts Mnesia and
initializes the schema for our
% erl -mnesia dir '"/ldisc/scratch/Mnesia.Company"' Erlang (BEAM) emulator version 4.9 Eshell V4.9 (abort with ^G) 1> mnesia:create_schema([node()]). ok 2> mnesia:start(). ok
The following program module creates and populates previously defined tables:
The following commands and functions were used to initiate the Company database:
Continuing the dialogue with the Erlang shell will produce the following:
company:init(). {atomic,ok} 4> mnesia:info(). ---> Processes holding locks <--- ---> Processes waiting for locks <--- ---> Pending (remote) transactions <--- ---> Active (local) transactions <--- ---> Uncertain transactions <--- ---> Active tables <--- in_proj : with 0 records occuping 269 words of mem at_dep : with 0 records occuping 269 words of mem manager : with 0 records occuping 269 words of mem project : with 0 records occuping 269 words of mem dept : with 0 records occuping 269 words of mem employee : with 0 records occuping 269 words of mem schema : with 7 records occuping 571 words of mem ===> System info in version "1.0", debug level = none <=== opt_disc. Directory "/ldisc/scratch/Mnesia.Company" is used. use fall-back at restart = false running db nodes = [nonode@nohost] stopped db nodes = [] remote = [] ram_copies = [at_dep,dept,employee,in_proj,manager,project] disc_copies = [schema] disc_only_copies = [] [{nonode@nohost,disc_copies}] = [schema] [{nonode@nohost,ram_copies}] = [employee,dept,project,manager,at_dep,in_proj] 6 transactions committed, 0 aborted, 0 restarted, 6 logged to disc 0 held locks, 0 in queue; 0 local transactions, 0 remote 0 transactions waits for other nodes: [] ok ]]>
A set of tables is created:
The
To write a function which inserts an employee record into the database, there must be an
The
The function can be used as:
Emp = #employee{emp_no= 104732,
name = klacke,
salary = 7,
sex = male,
phone = 98108,
room_no = {221, 015}},
insert_emp(Me, 'B/SFR', [Erlang, mnesia, otp]).
Functional Objects (Funs) are described in the Erlang Reference Manual, "Fun Expressions".
After the insertion of the employee named
An employee record has the following Erlang record/tuple
representation:
At_dep has the following Erlang tuple representation:
In_proj has the following Erlang tuple representation:
There is no difference between rows in a table and Mnesia records. Both concepts are the same and will be used interchangeably throughout this book.
A Mnesia table is populated by Mnesia records. For example,
the tuple
We were also able to insert the
After adding additional record to the Company database, we may end up with the following records:
Employees
{employee, 104465, "Johnson Torbjorn", 1, male, 99184, {242,038}}.
{employee, 107912, "Carlsson Tuula", 2, female,94556, {242,056}}.
{employee, 114872, "Dacker Bjarne", 3, male, 99415, {221,035}}.
{employee, 104531, "Nilsson Hans", 3, male, 99495, {222,026}}.
{employee, 104659, "Tornkvist Torbjorn", 2, male, 99514, {222,022}}.
{employee, 104732, "Wikstrom Claes", 2, male, 99586, {221,015}}.
{employee, 117716, "Fedoriw Anna", 1, female,99143, {221,031}}.
{employee, 115018, "Mattsson Hakan", 3, male, 99251, {203,348}}.
Dept
{dept, 'B/SF', "Open Telecom Platform"}.
{dept, 'B/SFP', "OTP - Product Development"}.
{dept, 'B/SFR', "Computer Science Laboratory"}.
Projects
%% projects
{project, erlang, 1}.
{project, otp, 2}.
{project, beam, 3}.
{project, mnesia, 5}.
{project, wolf, 6}.
{project, documentation, 7}.
{project, www, 8}.
The above three tables, titled
Manager
{manager, 104465, 'B/SF'}.
{manager, 104465, 'B/SFP'}.
{manager, 114872, 'B/SFR'}.
At_dep
{at_dep, 104465, 'B/SF'}.
{at_dep, 107912, 'B/SF'}.
{at_dep, 114872, 'B/SFR'}.
{at_dep, 104531, 'B/SFR'}.
{at_dep, 104659, 'B/SFR'}.
{at_dep, 104732, 'B/SFR'}.
{at_dep, 117716, 'B/SFP'}.
{at_dep, 115018, 'B/SFP'}.
In_proj
{in_proj, 104465, otp}.
{in_proj, 107912, otp}.
{in_proj, 114872, otp}.
{in_proj, 104531, otp}.
{in_proj, 104531, mnesia}.
{in_proj, 104545, wolf}.
{in_proj, 104659, otp}.
{in_proj, 104659, wolf}.
{in_proj, 104732, otp}.
{in_proj, 104732, mnesia}.
{in_proj, 104732, erlang}.
{in_proj, 117716, otp}.
{in_proj, 117716, documentation}.
{in_proj, 115018, otp}.
{in_proj, 115018, mnesia}.
The room number is an attribute of the employee
record. This is a structured attribute which consists of a
tuple. The first element of the tuple identifies a corridor,
and the second element identifies the actual room in the
corridor. We could have chosen to represent this as a record
The Company database is now initialized and contains data.
Retrieving data from DBMS should usually be done with
Since we want to update the record using
It is not always the case that we can directly read the values from the table,
we might need to search the table or several tables to get the data we want, this
is done by writing database queries. Queries are always more expensive operations
than direct lookups done with
There are two methods for writing database queries:
The following function extracts the names of the female employees stored in the database:
\011 mnesia:select(employee, [{#employee{sex = female, name = '$1', _ = '_'},[], ['$1']}]).
Select must always run within an activity such as a transaction. To be able to call from the shell we might construct a function as:
The select expression matches all entries in table employee with the field sex set to female.
This function can be called from the shell as follows:
(klacke@gin)1> company:all_females(). {atomic, ["Carlsson Tuula", "Fedoriw Anna"]}
See also the
This section contains simple introductory examples only. Refer to QLC reference manual for a full description of the QLC query language. Using QLC might be more expensive than using Mnesia functions directly but offers a nice syntax.
The following function extracts a list of female employees from the database:
Q = qlc:q([E#employee.name || E mnesia:table(employee), \011 E#employee.sex == female]), \011 qlc:e(Q),
Accessing mnesia tables from a QLC list comprehension must always be done within a transaction. Consider the following function:
This function can be called from the shell as follows:
(klacke@gin)1> company:females(). {atomic, ["Carlsson Tuula", "Fedoriw Anna"]}
In traditional relational database terminology, the above operation would be called a selection, followed by a projection.
The list comprehension expression shown above contains a number of syntactical elements.
Hence, the above list comprehension demonstrates the
formation of the list
The whole list comprehension must be given to the
It is possible to combine list comprehensions with low level Mnesia functions in the same transaction. If we want to raise the salary of all female employees we execute:
The function
33>company:raise_females(33). {atomic,2}