A simple database in Erlang


With Erlang is the principal creating of a database management system (DBMS) relatively easy to deal with.

This also speaks, that some databases in Erlang are designed:

  • CouchDB
  • SimpleDB

But other well-known projects.

Example database statements in Erlang

For a given amount of data called DB

DB = [{sebastian, jan, 26}, {daniel, jan, 10}, {susi, jan, 10},{thomas, dez, 19}].

1. All persons are extracted, have birthday in January:

[Name || {Name,Monat,_} <- DB, Monat == jan].                                     
[sebastian,daniel,susi]

2. Extracts all months and the name of the birthday boys and girls

[{Monat,[Name || {Name,MonatOuter,_} <- DB,  MonatOuter == Monat ]} || {_,Monat,_} <- DB].
[{jan,[sebastian,daniel,susi]},
 {jan,[sebastian,daniel,susi]},
 {jan,[sebastian,daniel,susi]},
 {dez,[thomas]}]

If you want to have no duplicate elements in the result set you can convert the list into a set and back again in a list, because a set has only contains unique keys:

sets:to_list(sets:from_list([{Monat,[Name || {Name,MonatOuter,_} <- DB,  MonatOuter == Monat ]}||{_,Monat,_} <- DB])).
[{jan,[sebastian,daniel,susi]},{dez,[thomas]}]

3. How many people have a birthday in January?

lists:sum([1 || {_,jan,_} <- DB]).  
3

4. How many people have on average (AVG) in December birthday?

lists:sum([1 || {_,dez,_} <- DB]) / length(DB).      
0.25