Mnesia是一套轻量级的软实时分布式数据存储系统,支持冗余复制和事务,特别适合于存储离散的erlang数据块,尤其擅长RAM中的数据存储。
初始化数据库步骤:
1.启动节点,erl -Mensa dir ‘“DIR"' -name mynode
2.建立数据库模式,mnesia:create_schema([node()]).
3.启动Mnesia,mnesia:start().
建表
建表操作需调用函数mnesia:create_table(Name,Options)来完成。Options是一张{Key,Value}选项列表,key既可以是attributes,也可以是{type,Type},其中Type可以是set/ordered_set/bag.使用记录进行建表:
-record(user,{id,name}).
-record(project,{title,description}).
-record(contributor,{user_id,project_title}).
-module(create_tables).
-export([init_tables/0]).
-include("mnesia_record.hrl").
init_tables() ->
mnesia:create_table(user,[{attributes,record_info(fields,user)}]),
mnesia:create_table(project,[{attributes,record_info(fields,project)}]),
mnesia:create_table(contributor,[{type,bag},{attributes,record_info(
fields,
contributor)}] ).
本文介绍了Erlang轻量级分布式数据库Mnesia的使用,包括初始化步骤和建表操作。通过启动节点、创建数据库模式和启动Mnesia进行初始化。建表时,利用mnesia:create_table函数及Erlang记录定义数据结构,如user、project和contributor表。
337

被折叠的 条评论
为什么被折叠?



