Mnesia is a distributed database management system, suitable for telecommunications and other applications requiring continuous operation and with the soft real-time characteristics of the Erlang, more and more attention and use, but the current Mnesia data is not much, many only the official user guide. The following will focus on how the Mnesia database implements SQL queries to implement SQL operations such as Select/insert/update/where/order By/join/limit/delete.
The definition of the table structure in the example:
[Plain]View Plaincopy
- Percent of Account table structure
- -record (y_account,{ID, account, password}).
- Data table structure
- -record (Y_info, {ID, nickname, birthday, sex}).
1. Create Table/delete Table operation
[Plain]View Plaincopy
- %%===============================================
- Percent CREATE TABLE y_account (ID int, account varchar (50),
- Percent Password varchar (+), primary key (ID));
- %%===============================================
- Percent use Mnesia:create_table
- Mnesia:create_table (Y_account,[{attributes, Record_info (Fields, Y_account)},
- {Type,set}, {disc_copies, [node ()]}]).
- %%===============================================
- Percent drop table y_account;
- %%===============================================
- Percent use Mnesia:delete_table
- Mnesia:delete_table (Y_account).
Note: The parameter meaning can be seen in the document, {Type,set} represents the ID as the primary key, the ID is not allowed to repeat, if {Type,bag},id can be repeated, but the entire record cannot be repeated
2. Select Query
Query all records
[Plain]View Plaincopy
- %%===============================================
- Percent SELECT * from Y_account
- %%===============================================
- Percent use Mnesia:select
- F = Fun ()
- Matchhead = #y_account {_ = ' _ '},
- Guard = [],
- Result = [' $_ '],
- Mnesia:select (Y_account, [{matchhead, Guard, Result}])
- End
- Mnesia:transaction (F).
- Percent use QLC
- F = Fun ()
- Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
- Qlc:e (Q)
- End
- Mnesia:transaction (F).
Querying records for part of a field
[Plain]View Plaincopy
- %%===============================================
- Percent of select Id,account from Y_account
- %%===============================================
- Percent use Mnesia:select
- F = Fun ()
- Matchhead = #y_account {id = ' $ ', account = ' $ $ ', _ = ' _ '},
- Guard = [],
- Result = [' $$ '],
- Mnesia:select (Y_account, [{matchhead, Guard, Result}])
- End
- Mnesia:transaction (F).
- Percent use QLC
- F = Fun ()
- Q = Qlc:q ([[E#y_account.id, E#y_account.account] | | E <-mnesia:table (Y_account)]),
- Qlc:e (Q)
- End
- Mnesia:transaction (F).
3. Insert/update operation
Mnesia is based on the primary key to update the record, if the primary key does not exist insert
[Plain]View Plaincopy
- %%===============================================
- Percent INSERT into y_account (Id,account,password) VALUES (5, "Xiaohong", "123")
- Percent on duplicate key update account= "Xiaohong", password= "123";
- %%===============================================
- Percent use Mnesia:write
- F = Fun ()
- ACC = #y_account {id = 5, account= "Xiaohong", password= "123"},
- Mnesia:write (ACC)
- End
- Mnesia:transaction (F).
4. Where Query
[Plain]View Plaincopy
- %%===============================================
- Percent of select account from Y_account where id>5
- %%===============================================
- Percent use Mnesia:select
- F = Fun ()
- Matchhead = #y_account {id = ' $ ', account = ' $ $ ', _ = ' _ '},
- Guard = [{' > ', ' $ ', 5}],
- Result = [' $ $ '],
- Mnesia:select (Y_account, [{matchhead, Guard, Result}])
- End
- Mnesia:transaction (F).
- Percent use QLC
- F = Fun ()
- Q = Qlc:q ([E#y_account.account | | E <-mnesia:table (Y_account), e#y_account.id>5]),
- Qlc:e (Q)
- End
- Mnesia:transaction (F).
If you look for a record of the primary key key=x, you can also do this subquery:
[Plain]View Plaincopy
- %%===============================================
- Percent SELECT * from Y_account where id=5
- %%===============================================
- F = Fun ()
- Mnesia:read ({y_account,5})
- End
- Mnesia:transaction (F).
If you are looking for records for non-primary key field=x, you can query as follows:
[Plain]View Plaincopy
- %%===============================================
- Percent SELECT * from Y_account where account= ' Xiaomin '
- %%===============================================
- F = Fun ()
- Matchhead = #y_account {id = ' _ ', account = "Xiaomin", Password = ' _ '},
- Guard = [],
- Result = [' $_ '],
- Mnesia:select (Y_account, [{matchhead, Guard, Result}])
- End
- Mnesia:transaction (F).
5. Order by Query
[Plain]View Plaincopy
- %%===============================================
- Percent SELECT * from Y_account ORDER by ID ASC
- %%===============================================
- Percent use QLC
- F = Fun ()
- Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
- Qlc:e (Qlc:keysort (2, Q, [{Order, Ascending}]))
- End
- Mnesia:transaction (F).
- The second way to use QLC
- F = Fun ()
- Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
- Order = Fun (A, B)
- B#y_account.id > A#y_account.id
- End
- Qlc:e (Qlc:sort (Q, [{Order, Order}]))
- End
- Mnesia:transaction (F).
6. Join Association Table Query
[Plain]View Plaincopy
- %%===============================================
- Percent select y_info.* from Y_account join Y_info on (y_account.id = y_info.id)
- Percent of where y_account.account = ' xiaomin '
- %%===============================================
- Percent use QLC
- F = Fun ()
- Q = Qlc:q ([Y | | X <-mnesia:table (Y_account),
- X#y_account.account =:= "Xiaomin",
- Y <-mnesia:table (y_info),
- X#y_account.id =:= Y#y_info.id
- ]),
- Qlc:e (Q)
- End
- Mnesia:transaction (F).
7. Limit Query
[Plain]View Plaincopy
- %%===============================================
- Percent SELECT * from Y_account limit 2
- %%===============================================
- Percent use Mnesia:select
- F = Fun ()
- Matchhead = #y_account {_ = ' _ '},
- Mnesia:select (Y_account, [{matchhead, [], [' $_ ']}], 2, none)
- End
- Mnesia:transaction (F).
- Percent use QLC
- F = Fun ()
- Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
- QC = Qlc:cursor (Q),
- Qlc:next_answers (QC, 2)
- End
- Mnesia:transaction (F).
8. Select count (*) query
[Plain]View Plaincopy
- %%===============================================
- Percent of SELECT count (*) from Y_account
- %%===============================================
- Percent use Mnesia:table_info
- F = Fun ()
- Mnesia:table_info (y_account, size)
- End
- Mnesia:transaction (F).
9. Delete Query
[Plain]View Plaincopy
- %%===============================================
- Percent Delete from Y_account where id=5
- %%===============================================
- Percent use Mnesia:delete
- F = Fun ()
- Mnesia:delete ({y_account, 5})
- End
- Mnesia:transaction (F).
Note: Using the QLC module query, you need to declare "-include_lib (" Stdlib/include/qlc.hrl ") at the top of the file.", otherwise the compilation will produce "WARNING:QLC:Q/1 called, but" QLC.HRL "not Included "warning.
Update Description:
2013/11/20 added limit query for Mnesia:select mode
Erlang Mnesia database for SQL queries