<p>I believe you are over complicating your life.</p>

<p>Having a BlockChain (a Linked Ledger) in SQL can be prototyped in the following manner</p>

<ol>
<li><p>Create a table, such as:</p>

<pre><code>CREATE TABLE IF NOT EXISTS 
MyBlockChain (
    LedgerID int(10) NOT NULL auto_increment,
    block varchar(255),
    datetimestamp datetime,
    guid varchar(255),   --Have a Default GUID Generated 
    PRIMARY KEY( `LedgerID` )
);
</code></pre></li>
<li><p>Having an AutoGenerated Sequence Number (as a primary key) - This creates the sequence in your ledger.<br>
Assign Proper Security to only allow INSERTS to happen over a Public API.</p></li>
<li>Only the GUID is used to access a Record (block) via ReadOnly permissions.</li>
<li>When users can only INSERT Data (No DELETE or UPDATE) you, in essence, created a Ledger.</li>
<li>And since the Sequence Number will be used to determine ordering of actions.</li>
</ol>

<p>The biggest challenge to distributing the MySQL database over multiple instances an having an API interface and ensuring ReadOnly and Insert Only security.</p>

<p>That by default is a simple implementation of a BlockChain</p>

CC BY-SA 4.0
2019-01-09T05:34:23.013Z
In order for this to be more blockchain-like, I believe you'd have to make `guid` the primary key instead of `LedgerID`, and then make a `prev_guid` the link to the previous block (row in this case). Of course, you'd have to uniquely generate each guid to keep database integrity.
Volomike
CC BY-SA 4.0
2019-01-09T05:37:16.887Z
Meanwhile, for a faster table speed, you could move `block` to the end of the table and then even switch it from `varchar(255)` to `text`, `mediumtext`, or `longtext`.
Volomike
CC BY-SA 4.0
2019-01-09T05:42:17.910Z
Permit no inserts unless no other block (row in this case) shares the same `prev_guid`, and your `guid` is unique.
Volomike
CC BY-SA 3.0
2017-12-13T16:24:20.110Z
2017-12-13T21:01:19.747Z
2017-12-13T21:01:19.747Z
Greg Vitetzakis
<p>I want to create local blockchain for my application. New blocks should be created while giving reference to previous block. But I am not getting exactly how will I do it in MySQL? I got some idea from <a href="http://blog.theodorejb.me/sql-linked-lists/" rel="nofollow noreferrer">http://blog.theodorejb.me/sql-linked-lists/</a>
. Now I want to retrieve top block every time I create a new block. But Is it possible in MySQL to retrieve Newly inserted row's primary key? Or Is is possible to implement stack(with reference) in MySQL ? So that I can retrieve top block. </p>

<blockchain><database><block-header>
To create a blockchain using MySQL database
DOLLY PATWA
# To create a blockchain using MySQL database

(Asked by: username_0 on 2017-08-23)

I want to create local blockchain for my application. New blocks should be created while giving reference to previous block. But I am not getting exactly how will I do it in MySQL? I got some idea from <http://blog.theodorejb.me/sql-linked-lists/>
. Now I want to retrieve top block every time I create a new block. But Is it possible in MySQL to retrieve Newly inserted row's primary key? Or Is is possible to implement stack(with reference) in MySQL ? So that I can retrieve top block.

**Tags:** blockchain, database, block-header


---

## 1 answers

### Answer by username_1 on 2017-12-13. Score: 3

I believe you are over complicating your life.

Having a BlockChain (a Linked Ledger) in SQL can be prototyped in the following manner

1. Create a table, such as:

   ```
   CREATE TABLE IF NOT EXISTS 
   MyBlockChain (
       LedgerID int(10) NOT NULL auto_increment,
       block varchar(255),
       datetimestamp datetime,
       guid varchar(255),   --Have a Default GUID Generated 
       PRIMARY KEY( `LedgerID` )
   );

   ```
2. Having an AutoGenerated Sequence Number (as a primary key) - This creates the sequence in your ledger.  
   Assign Proper Security to only allow INSERTS to happen over a Public API.
3. Only the GUID is used to access a Record (block) via ReadOnly permissions.
4. When users can only INSERT Data (No DELETE or UPDATE) you, in essence, created a Ledger.
5. And since the Sequence Number will be used to determine ordering of actions.

The biggest challenge to distributing the MySQL database over multiple instances an having an API interface and ensuring ReadOnly and Insert Only security.

That by default is a simple implementation of a BlockChain

> **Comments:**
> **username_2**: In order for this to be more blockchain-like, I believe you'd have to make `guid` the primary key instead of `LedgerID`, and then make a `prev_guid` the link to the previous block (row in this case). Of course, you'd have to uniquely generate each guid to keep database integrity.  
> **username_2**: Meanwhile, for a faster table speed, you could move `block` to the end of the table and then even switch it from `varchar(255)` to `text`, `mediumtext`, or `longtext`.  
> **username_2**: Permit no inserts unless no other block (row in this case) shares the same `prev_guid`, and your `guid` is unique.  

---

