Erlang Mnesia database for SQL queries

Source: Internet
Author: User

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
    1. Percent of Account table structure
    2. -record (y_account,{ID, account, password}).
    3. Data table structure
    4. -record (Y_info, {ID, nickname, birthday, sex}).

1. Create Table/delete Table operation

[Plain]View Plaincopy
  1. %%===============================================
  2. Percent CREATE TABLE y_account (ID int, account varchar (50),
  3. Percent Password varchar (+), primary key (ID));
  4. %%===============================================
  5. Percent use Mnesia:create_table
  6. Mnesia:create_table (Y_account,[{attributes, Record_info (Fields, Y_account)},
  7. {Type,set}, {disc_copies, [node ()]}]).
  8. %%===============================================
  9. Percent drop table y_account;
  10. %%===============================================
  11. Percent use Mnesia:delete_table
  12. 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
  1. %%===============================================
  2. Percent SELECT * from Y_account
  3. %%===============================================
  4. Percent use Mnesia:select
  5. F = Fun ()
  6. Matchhead = #y_account {_ = ' _ '},
  7. Guard = [],
  8. Result = [' $_ '],
  9. Mnesia:select (Y_account, [{matchhead, Guard, Result}])
  10. End
  11. Mnesia:transaction (F).
  12. Percent use QLC
  13. F = Fun ()
  14. Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
  15. Qlc:e (Q)
  16. End
  17. Mnesia:transaction (F).

Querying records for part of a field

[Plain]View Plaincopy
  1. %%===============================================
  2. Percent of select Id,account from Y_account
  3. %%===============================================
  4. Percent use Mnesia:select
  5. F = Fun ()
  6. Matchhead = #y_account {id = ' $ ', account = ' $ $ ', _ = ' _ '},
  7. Guard = [],
  8. Result = [' $$ '],
  9. Mnesia:select (Y_account, [{matchhead, Guard, Result}])
  10. End
  11. Mnesia:transaction (F).
  12. Percent use QLC
  13. F = Fun ()
  14. Q = Qlc:q ([[E#y_account.id, E#y_account.account] | | E <-mnesia:table (Y_account)]),
  15. Qlc:e (Q)
  16. End
  17. 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
    1. %%===============================================
    2. Percent INSERT into y_account (Id,account,password) VALUES (5, "Xiaohong", "123")
    3. Percent on duplicate key update account= "Xiaohong", password= "123";
    4. %%===============================================
    5. Percent use Mnesia:write
    6. F = Fun ()
    7. ACC = #y_account {id = 5, account= "Xiaohong", password= "123"},
    8. Mnesia:write (ACC)
    9. End
    10. Mnesia:transaction (F).

4. Where Query

[Plain]View Plaincopy
  1. %%===============================================
  2. Percent of select account from Y_account where id>5
  3. %%===============================================
  4. Percent use Mnesia:select
  5. F = Fun ()
  6. Matchhead = #y_account {id = ' $ ', account = ' $ $ ', _ = ' _ '},
  7. Guard = [{' > ', ' $ ', 5}],
  8. Result = [' $ $ '],
  9. Mnesia:select (Y_account, [{matchhead, Guard, Result}])
  10. End
  11. Mnesia:transaction (F).
  12. Percent use QLC
  13. F = Fun ()
  14. Q = Qlc:q ([E#y_account.account | | E <-mnesia:table (Y_account), e#y_account.id>5]),
  15. Qlc:e (Q)
  16. End
  17. Mnesia:transaction (F).

If you look for a record of the primary key key=x, you can also do this subquery:

[Plain]View Plaincopy
    1. %%===============================================
    2. Percent SELECT * from Y_account where id=5
    3. %%===============================================
    4. F = Fun ()
    5. Mnesia:read ({y_account,5})
    6. End
    7. Mnesia:transaction (F).

If you are looking for records for non-primary key field=x, you can query as follows:

[Plain]View Plaincopy
    1. %%===============================================
    2. Percent SELECT * from Y_account where account= ' Xiaomin '
    3. %%===============================================
    4. F = Fun ()
    5. Matchhead = #y_account {id = ' _ ', account = "Xiaomin", Password = ' _ '},
    6. Guard = [],
    7. Result = [' $_ '],
    8. Mnesia:select (Y_account, [{matchhead, Guard, Result}])
    9. End
    10. Mnesia:transaction (F).

5. Order by Query

[Plain]View Plaincopy
  1. %%===============================================
  2. Percent SELECT * from Y_account ORDER by ID ASC
  3. %%===============================================
  4. Percent use QLC
  5. F = Fun ()
  6. Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
  7. Qlc:e (Qlc:keysort (2, Q, [{Order, Ascending}]))
  8. End
  9. Mnesia:transaction (F).
  10. The second way to use QLC
  11. F = Fun ()
  12. Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
  13. Order = Fun (A, B)
  14. B#y_account.id > A#y_account.id
  15. End
  16. Qlc:e (Qlc:sort (Q, [{Order, Order}]))
  17. End
  18. Mnesia:transaction (F).

6. Join Association Table Query

[Plain]View Plaincopy
  1. %%===============================================
  2. Percent select y_info.* from Y_account join Y_info on (y_account.id = y_info.id)
  3. Percent of where y_account.account = ' xiaomin '
  4. %%===============================================
  5. Percent use QLC
  6. F = Fun ()
  7. Q = Qlc:q ([Y | | X <-mnesia:table (Y_account),
  8. X#y_account.account =:= "Xiaomin",
  9. Y <-mnesia:table (y_info),
  10. X#y_account.id =:= Y#y_info.id
  11. ]),
  12. Qlc:e (Q)
  13. End
  14. Mnesia:transaction (F).

7. Limit Query

[Plain]View Plaincopy
  1. %%===============================================
  2. Percent SELECT * from Y_account limit 2
  3. %%===============================================
  4. Percent use Mnesia:select
  5. F = Fun ()
  6. Matchhead = #y_account {_ = ' _ '},
  7. Mnesia:select (Y_account, [{matchhead, [], [' $_ ']}], 2, none)
  8. End
  9. Mnesia:transaction (F).
  10. Percent use QLC
  11. F = Fun ()
  12. Q = Qlc:q ([E | | E <-mnesia:table (Y_account)]),
  13. QC = Qlc:cursor (Q),
  14. Qlc:next_answers (QC, 2)
  15. End
  16. Mnesia:transaction (F).

8. Select count (*) query

[Plain]View Plaincopy
    1. %%===============================================
    2. Percent of SELECT count (*) from Y_account
    3. %%===============================================
    4. Percent use Mnesia:table_info
    5. F = Fun ()
    6. Mnesia:table_info (y_account, size)
    7. End
    8. Mnesia:transaction (F).

9. Delete Query

[Plain]View Plaincopy
    1. %%===============================================
    2. Percent Delete from Y_account where id=5
    3. %%===============================================
    4. Percent use Mnesia:delete
    5. F = Fun ()
    6. Mnesia:delete ({y_account, 5})
    7. End
    8. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.