Home Database Mysql Tutorial erlang mnesia数据库设置主键自增

erlang mnesia数据库设置主键自增

Jun 07, 2016 pm 03:11 PM
erlang primary key database self-increasing set up

Mnesia是erlang/otp自带的分布式数据库管理系统。mnesia配合erlang的实现近乎理想,但在实际使用当中差强人意,总会有一些不足。mnesia数据表没有主键自增的功能,但在mnesia函数中有一个自增函数可以用来生成自增的id,在后面的内容将讲述如何实现主键自增

Mnesia是erlang/otp自带的分布式数据库管理系统。mnesia配合erlang的实现近乎理想,但在实际使用当中差强人意,总会有一些不足。mnesia数据表没有主键自增的功能,但在mnesia函数中有一个自增函数可以用来生成自增的id,在后面的内容将讲述如何实现主键自增的功能。

参照SQLite的做法,就是在数据库中单独建立一个的sqlite_sequence表来做其他表的自增索引表。同样,我们也创建这样一个表erlang_sequence,用来索引其他表的自增id,看似麻烦,效果还是很理想。

-module(m).
-export([init/0, reg/2]).
-record(user, {id, name, age}).
-record(erlang_sequence, {name, seq}).%自增索引表,维护其他表的自增id

init() ->
	mnesia:create_schema([node()]),
	mnesia:start(),
	mnesia:create_table(erlang_sequence, [{attributes, record_info(fields,
	  erlang_sequence)} , {type,set}, {disc_copies, [node()]} ]),
	mnesia:create_table(user, [{attributes, record_info(fields, user)} ,
	  {type,set}, {disc_copies, [node()]} ]),
	ok.

reg(Name, Age) ->
	F = fun() ->
		UserId = mnesia:dirty_update_counter(erlang_sequence, user, 1),
		User = #user{id = UserId, name = Name, age = Age },
		mnesia:write(User)
	end,
	mnesia:transaction(F),
	ok.
Copy after login

运行代码:

D:\>erl -mnesia dir '"mnesia"'
Eshell V5.10.2  (abort with ^G)
1> c(m).
{ok,m}
2> m:init().
ok
3> m:reg("xiaomin",18).
ok
4> m:reg("xiaohong",17).
ok
5> tv:start().
Copy after login

Mnesia数据库视图:

erlang mnesia数据库设置主键自增



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do Not Disturb Mode Not Working in iPhone: Fix Do Not Disturb Mode Not Working in iPhone: Fix Apr 24, 2024 pm 04:50 PM

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

How to set the watermark in the middle on Weibo_How to set the watermark in the middle on Weibo How to set the watermark in the middle on Weibo_How to set the watermark in the middle on Weibo Mar 29, 2024 pm 03:31 PM

1. First enter Weibo, then click on me in the lower right corner and select [Customer Service]. 2. Then enter [Watermark] in the search box and select [Set Weibo Image Watermark]. 3. Then click [Link] in the interface. 4. Then click [Image Watermark Settings] in the newly opened window. 5. Finally, check [Picture Center] and click [Save].

How to set up scheduled publishing on Weibo_Tutorial on how to set up scheduled publishing on Weibo How to set up scheduled publishing on Weibo_Tutorial on how to set up scheduled publishing on Weibo Mar 29, 2024 pm 03:51 PM

1. Open the Weibo client, click the three little dots on the editing page, and then click Scheduled Post. 2. After clicking on scheduled posting, there will be a time option on the right side of the publishing time. Set the time, edit the article, and click on the yellow words in the lower right corner to schedule posting. 3. The mobile version of Weibo does not currently support scheduled publishing. This function can only be used on the PC client!

How to set the countdown to grab tickets in Damai How to set the countdown to grab tickets in Damai Apr 01, 2024 pm 07:01 PM

When buying tickets on Damai.com, in order to ensure that the ticket purchase time can be accurately grasped, users can set a floating clock to grab tickets. The detailed setting method is below, let us learn together. How to bind the floating clock to Damai 1. Click to open the floating clock app on your phone to enter the interface, and click on the location where the flash sale check is set, as shown in the figure below: 2. After coming to the page of adding new records, click on Damai.com Copy the ticket purchase link page copied in. 3. Next, set the flash sale time and notification time below, turn on the switch button behind [Save to Calendar], and click [Save] below. 4. Click to turn on [Countdown], as shown in the figure below: 5. When the reminder time comes, click the [Start Picture-in-Picture] button below. 6. When the ticket purchase time comes

How to set Douyin playback to automatically close? Why does the playback process automatically exit? How to set Douyin playback to automatically close? Why does the playback process automatically exit? Apr 01, 2024 pm 12:06 PM

Douyin is one of the most popular short video platforms today. While enjoying the fun of short videos, users also hope to have better control over their time. Recently, some users reported that Douyin would automatically close during playback, which troubled them very much. 1. How to set the Douyin playback to automatically turn off? 1. Check the Douyin version. Please make sure that the Douyin version you are using is the latest. Douyin will be updated regularly to fix known issues. If your version of TikTok is too old, it may automatically close. You can check and update TikTok in the App Store. 2. Check the mobile phone system version. The automatic shutdown of Douyin playback may also be related to the mobile phone system version. Please make sure your mobile phone system version is the latest. If the mobile phone system version is too old, it may cause Douyin to run unstable. you can

How to set up 3D map in Google Maps mobile version How to set up 3D map in Google Maps mobile version Apr 01, 2024 pm 04:26 PM

The 3D mode of Google Maps is favored by users because it can present map information in a more three-dimensional way, allowing users to clearly view their location. Therefore, the editor of this article has prepared a method to enable the 3D map mode of the mobile version of Google Maps. Let’s learn together. . How to turn on the 3D map mode of Google Maps mobile version 1. First, enter the location you want to view at any time in the input box above. 2. After finding the location, click the small icon of the location shown in the picture below. 3. Then click [3D] in the function menu. Option 4. After selecting the 3D function to turn on, click on the location you want to view. You can see a small window in the lower left corner. Click on the window to enlarge. 5. Finally, we can see the 3D real-time street view.

How to set up group chat bubbles on Weibo_How to set up group chat bubbles on Weibo How to set up group chat bubbles on Weibo_How to set up group chat bubbles on Weibo Mar 30, 2024 pm 01:31 PM

1. Open the Weibo group chat and select Settings (as shown in the picture). 2. Select [Chat Bubble] (as shown in the picture). 3. After reaching the corresponding level, you can receive the bubbles and set them up (as shown in the picture).

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

See all articles