<blockquote>
  <p>I'm currently trying to find practical examples for the usage of a blockchain, instead of a typical database like MySQL. I understand that the big advantage of a blockchain is the high security of data not being compromised.</p>
</blockquote>

<p>That's a meaningless question. A blockchain is not a replacement for a database; it is a mechanism for multiple parties which are already running <em>their own</em> databases to make sure they all end up in the same state.</p>

<p>For example, in Bitcoin, that database is the UTXO set, which maintains for every unspent coin the amount and owner. Every Bitcoin full node has its own copy of that UTXO set. It can be stored in any kind of database engine - the most common ones using a specialized LevelDB instance. The blockchain consists of a list of authenticated <em>updates</em> to that database. You never search the blockchain itself, as that would be like going through all your bank statements to find your balance. Instead, you just look at the balance itself.</p>

<p>In the case of Bitcoin, that database is a very simple key-value store, as that is sufficient for verifying transactions. It is highly specialized, and does not maintain any indexes beyond what is necessary for validating things. But in theory, it could instead be backed by MySQL - allowing you to issue queries against <em>your own</em> UTXO set.</p>

<blockquote>
  <p>Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?</p>
</blockquote>

<p>That would be ridiculous. The problem a blockchain solves is consistency while permitting multiple independent and distrusting parties to make updates which may conflict with each other ("double spends" in Bitcoin's case). If you have a single party (the employer) who must approve updates to the database anyway (or a collection of identifiable parties who trust each other to some extent) much more efficient technologies exist.</p>

CC BY-SA 4.0
2022-01-19T19:20:57.580Z
So what does a blockchain store? If there's LevelDB to store the data, when will data from blockchain be ever fetched?
anotherDev
CC BY-SA 4.0
2018-05-06T01:30:49.550Z
2018-05-06T19:58:48.953Z
2018-05-06T19:58:48.953Z
Pieter Wuille
<p>One possible simple implementation of this would be by making an <a href="https://solidity.readthedocs.io/en/develop/" rel="nofollow noreferrer">Ethereum smart contract</a>. You could easily store employee information in a mapping of their ethereum address to whatever information. You can fetch the stored information in javascript. The contract could be written so only the employee could view his data. </p>

CC BY-SA 3.0
2018-01-18T21:25:13.020Z
So a smart contract can have his own storage?
Felix
CC BY-SA 3.0
2018-01-18T21:30:28.833Z
Yes, but writing is very expensive (you will need to pay ethereum gas), so I would not recommend to actually do this, but it's the easiest implementation that I can think of. Reading data is not expensive though.
Cedric Martens
CC BY-SA 4.0
2022-01-19T19:26:04.297Z
When you say a smart contract can store data, means the data will still be stored on the blockchain, no? Because smart contracts live on the blockchain, right?
anotherDev
CC BY-SA 3.0
2018-01-18T21:19:24.293Z
2018-01-18T21:19:24.293Z
Cedric Martens
<p>Blockchain is a P2P decentralised network of nodes.</p>

<p>The query of "Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?"</p>

<p>One simply will not use blockchain for this query.</p>

CC BY-SA 4.0
2019-12-08T23:17:26.573Z
2019-12-08T23:17:26.573Z
KrakenYu
<p>You can access blockchain data like the way you access normal data in any  MVC where you fetch data from database ,manipulate it in backend and display the result in front end. Incase of blockchain,what ayou need to understand is it is  nothing but a decentralized database where transactions occor dynamically and the data is distibuted among all the nodes.</p>
<p>So to explain <strong>how the data is actually fetched</strong> from blockchain, what I do is ,<strong>Crawl each block in the blockchain and store the data in your preferred data base like MySql or Mongo</strong></p>
<p>You can use <em>rpc</em>  <code>getblockhash &quot;n&quot;</code>, which will return the hash of the block &quot;n&quot;, for example:
eee7f58d111ef0b25f7f6e8703d80ab6b51ca957170daea949e93e6467906889</p>
<p>You can retrieve the block's contents by calling  <code>get block &quot;b&quot;</code>, where &quot;b&quot; is the hash. Which can look like this:</p>
<pre><code>{
        &quot;hash&quot; : &quot;eee7f58d111ef0b25f7f6e8703d80ab6b51ca957170daea949e93e6467906889&quot;,
        &quot;confirmations&quot; : 176023,
        &quot;size&quot; : 261,
        &quot;height&quot; : 0,
        &quot;version&quot; : 1,
        &quot;merkleroot&quot; : &quot;695191c2e8f8fddc75331a38d658ab07672970b09981ad77c29c4b0b17580941&quot;,
        &quot;tx&quot; : [
            &quot;695191c2e8f8fddc75331a38d658ab07672970b09981ad77c29c4b0b17580941&quot;
        ],
        &quot;time&quot; : 1518520240,
        &quot;nonce&quot; : 2084635057,
        &quot;bits&quot; : &quot;1e0ffff0&quot;,
        &quot;difficulty&quot; : 0.00024414,
        &quot;nextblockhash&quot; : &quot;1bb4a4c35103a363ad63bf59bf7fa45c06babcfa8d848ceeb7e4958f9a9f660e&quot;
    }
</code></pre>
<p>Now every key value of above output replicates some important concept which cannot be ignored. But for now lets just focus on <strong>&quot;tx&quot;</strong>,
<strong>the array of tx are the list of transaction that have been mined in the &quot;n&quot;th block</strong>, and now you can crawl each and evry transaction given in tx array using rpc <code>getrawtransaction</code> to know the detail of each transaction.</p>
<p>Personally, I store the details in MongoDB so I can keep track of my applications' users, their addresses and the transactions they have done.</p>
<p>So this is a simple approach by which you can access data from a blockchain.</p>

CC BY-SA 4.0
2018-05-15T14:55:48.207Z
2021-12-04T15:15:56.543Z
2021-12-04T15:15:56.543Z
Parishilan Rayamajhi
<p>I'm currently trying to find practical examples for the usage of a blockchain, instead of a typical database like MySQL.
I understand that the big advantage of a blockchain is the high security of data not being compromised.</p>

<p>But how can I imagine accessing or searching for a specific record in the blockchain?</p>

<p>Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?</p>

<blockchain>
How can data be accessed/searched for in a blockchain?
Felix
# How can data be accessed/searched for in a blockchain?

(Asked by: username_0 on 2018-01-18)

I'm currently trying to find practical examples for the usage of a blockchain, instead of a typical database like MySQL.
I understand that the big advantage of a blockchain is the high security of data not being compromised.

But how can I imagine accessing or searching for a specific record in the blockchain?

Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?

**Tags:** blockchain


---

## 4 answers

### Answer by username_5 on 2018-05-15. Score: 5 | Accepted answer

You can access blockchain data like the way you access normal data in any MVC where you fetch data from database ,manipulate it in backend and display the result in front end. Incase of blockchain,what ayou need to understand is it is nothing but a decentralized database where transactions occor dynamically and the data is distibuted among all the nodes.

So to explain **how the data is actually fetched** from blockchain, what I do is ,**Crawl each block in the blockchain and store the data in your preferred data base like MySql or Mongo**

You can use *rpc* `getblockhash "n"`, which will return the hash of the block "n", for example:
eee7f58d111ef0b25f7f6e8703d80ab6b51ca957170daea949e93e6467906889

You can retrieve the block's contents by calling `get block "b"`, where "b" is the hash. Which can look like this:

```
{
        "hash" : "eee7f58d111ef0b25f7f6e8703d80ab6b51ca957170daea949e93e6467906889",
        "confirmations" : 176023,
        "size" : 261,
        "height" : 0,
        "version" : 1,
        "merkleroot" : "695191c2e8f8fddc75331a38d658ab07672970b09981ad77c29c4b0b17580941",
        "tx" : [
            "695191c2e8f8fddc75331a38d658ab07672970b09981ad77c29c4b0b17580941"
        ],
        "time" : 1518520240,
        "nonce" : 2084635057,
        "bits" : "1e0ffff0",
        "difficulty" : 0.00024414,
        "nextblockhash" : "1bb4a4c35103a363ad63bf59bf7fa45c06babcfa8d848ceeb7e4958f9a9f660e"
    }

```

Now every key value of above output replicates some important concept which cannot be ignored. But for now lets just focus on **"tx"**,
**the array of tx are the list of transaction that have been mined in the "n"th block**, and now you can crawl each and evry transaction given in tx array using rpc `getrawtransaction` to know the detail of each transaction.

Personally, I store the details in MongoDB so I can keep track of my applications' users, their addresses and the transactions they have done.

So this is a simple approach by which you can access data from a blockchain.


---

### Answer by username_1 on 2018-05-06. Score: 3

> I'm currently trying to find practical examples for the usage of a blockchain, instead of a typical database like MySQL. I understand that the big advantage of a blockchain is the high security of data not being compromised.

That's a meaningless question. A blockchain is not a replacement for a database; it is a mechanism for multiple parties which are already running *their own* databases to make sure they all end up in the same state.

For example, in Bitcoin, that database is the UTXO set, which maintains for every unspent coin the amount and owner. Every Bitcoin full node has its own copy of that UTXO set. It can be stored in any kind of database engine - the most common ones using a specialized LevelDB instance. The blockchain consists of a list of authenticated *updates* to that database. You never search the blockchain itself, as that would be like going through all your bank statements to find your balance. Instead, you just look at the balance itself.

In the case of Bitcoin, that database is a very simple key-value store, as that is sufficient for verifying transactions. It is highly specialized, and does not maintain any indexes beyond what is necessary for validating things. But in theory, it could instead be backed by MySQL - allowing you to issue queries against *your own* UTXO set.

> Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?

That would be ridiculous. The problem a blockchain solves is consistency while permitting multiple independent and distrusting parties to make updates which may conflict with each other ("double spends" in Bitcoin's case). If you have a single party (the employer) who must approve updates to the database anyway (or a collection of identifiable parties who trust each other to some extent) much more efficient technologies exist.

> **Comments:**
> **username_2**: So what does a blockchain store? If there's LevelDB to store the data, when will data from blockchain be ever fetched?  

---

### Answer by username_3 on 2018-01-18. Score: 0

One possible simple implementation of this would be by making an [Ethereum smart contract](https://solidity.readthedocs.io/en/develop/). You could easily store employee information in a mapping of their ethereum address to whatever information. You can fetch the stored information in javascript. The contract could be written so only the employee could view his data.

> **Comments:**
> **username_0**: So a smart contract can have his own storage?  
> **username_3**: Yes, but writing is very expensive (you will need to pay ethereum gas), so I would not recommend to actually do this, but it's the easiest implementation that I can think of. Reading data is not expensive though.  
> **username_2**: When you say a smart contract can store data, means the data will still be stored on the blockchain, no? Because smart contracts live on the blockchain, right?  

---

### Answer by username_4 on 2019-12-08. Score: 0

Blockchain is a P2P decentralised network of nodes.

The query of "Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?"

One simply will not use blockchain for this query.


---

