Mnesia database operations (1)

Source: Internet
Author: User
The operations of the Mnesia database are almost different from those of the previously used SQL database. They are not used at the beginning. Below are some common read/write operations: 1. Example of dirty read mnesia: dirty_read (Tab, Key)-ValueList | exit ({aborted, Reason: for a set table, this operation returns one or more empty tables because of the set table key.

The operations of the Mnesia database are almost different from those of the previously used SQL database. They are not used at the beginning. Below are some common read/write operations: 1. Example of dirty read mnesia: dirty_read (Tab, Key)-ValueList | exit ({aborted, Reason: for a set table, this operation returns one or more empty tables because of the set table key.

The operations of the Mnesia database are almost different from those of the previously used SQL database. They are not used at the beginning. Below are some common read/write operations:

1. Dirty read

Mnesia: dirty_read (Tab, Key)-> ValueList | exit ({aborted, Reason })

Example:



For a set table, this operation returns a value or an empty table because the key values of the set table cannot be repeated. If you use dirty reads for the bag type (key values can be repeated, but cannot be the same as the key and value), the result is a list of one or more values, and none of the values is an empty table.

Example:



2. Dirty writing

Mnesia: dirty_write (Tab, Record)-> OK | exit ({abouted, Reason })

Example:



3. read

This operation can only be used in transactions, so it cannot be used directly in shell.

Example:



4. write

Same as above, normal write operations must also be completed in the transaction; otherwise, an error will be reported.

Example:



Conclusion: both read and write are used for transactions. When transactions are not used, dirty_read and dirty_write are used directly.

In addition, the above two read operations are performed based on the primary key.


5. Use mnesia with matching rules: select



This command must also be placed in the transaction for execution. The first parameter is the table to be queried, and the second parameter is the matching specification. Is a triple in the format of {Head, Conditions, Results.

Head is an Erlang itemtype used to describe the query mode. atoms such as '$ 1' and' $ 2' are used to represent variables, it corresponds to the first and second fields in the record, and so on.


'_' (Only used in the Head part) indicates that it does not matter and any value can be matched. For example:



'$' (Only used in Results and Conditions) is equivalent to listing all variables '$ 1',' $ 1' matched in the Head part in sequence ', '$ 1' and so on. For an example, see. This is mainly used when we are only interested in some fields in the query results. We do not need to select the entire record.


'$ _' (Only used in Results and Conditions) indicates the entire record that matches the query condition. For example:



6. Use QLC)

The full name of QLC is Query List Comprehension. On the surface, it is similar to the normal list speed structure. When used, it must be nested in the appearance similar to the qlc: q () used in function call. In fact, the outer qlc: q (...) is just a tag that allows the compiler to treat the EXPRESSIONS differently. To enable this function, add the following line to the module source code:

-Include_lib ("stdlib/include/qlc. hrl ").

However, as a special case, You can directly use qlc: q (...) in Erlang shell (...). You can also use QLC in transactions. Any Mnesia function that can be used in transactions can be used together with QLC.


Example:


Only obtain the acc field in the users table.



This is to obtain the entire record in the users table.


For more complex QLC usage, see Chapter 20th, "Mnesia: The Erlang Database", in The new "Programming Erlang, 2nd" by Joe Armstrong, father of Erlang. For example, "two tables join query ".


Example of QLC usage in erl source code:



7. delete mnesia: delete

To delete a row from the database, you need to know the ID (OID) of the row ). An object ID consists of a table name and a primary key.

Example:



8. Update operations

There is no UPDATE concept in the Mnesia data table. to modify a record, you can delete it first, insert a new record, or add a new record to replace the old record.


9. Transaction operations

Structure of the transaction code:

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.