<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krithika</title>
    <description>The latest articles on DEV Community by Krithika (@krithikabalu).</description>
    <link>https://dev.to/krithikabalu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F411089%2F199b8e0a-9ff4-48ee-b302-7976061baa21.png</url>
      <title>DEV Community: Krithika</title>
      <link>https://dev.to/krithikabalu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/krithikabalu"/>
    <language>en</language>
    <item>
      <title>What differentiates schema on read from schema on write?</title>
      <dc:creator>Krithika</dc:creator>
      <pubDate>Sun, 21 Jun 2020 13:32:19 +0000</pubDate>
      <link>https://dev.to/krithikabalu/what-differentiates-schema-on-read-from-schema-on-write-gfb</link>
      <guid>https://dev.to/krithikabalu/what-differentiates-schema-on-read-from-schema-on-write-gfb</guid>
      <description>&lt;p&gt;Using a story-telling example, I would like to differentiate between schema on read and schema on write.&lt;/p&gt;

&lt;p&gt;Ria is running a retail shop. She had to store a lot of information on products, sales, customers and many more. Lets focus on the use case of product table here.&lt;/p&gt;

&lt;p&gt;When she started storing the data, she created a product table with the following fields - 'product id' of type number which is the &lt;strong&gt;primary key&lt;/strong&gt;, 'product name' of type text with &lt;strong&gt;unique&lt;/strong&gt; constraint, 'cost' of type float, 'category' of type text, 'base discount percentage' of type float. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--er7Fe54N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g0dm6vbyvgocun78nywy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--er7Fe54N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g0dm6vbyvgocun78nywy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Six months had passed and she had about a few thousand records in it. To expand her customer base, she introduced promotional discount for her products. She had to alter the table definition to add a new column 'promotional discount percentage' of type float.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LjVxBDVK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1s9ge1omxajzv8zec2yg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LjVxBDVK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1s9ge1omxajzv8zec2yg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After a few months, she realized that it would be beneficial to store the supplier information in the product table. Again, she had to alter the table to add a new column 'supplier'. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bGbndIxF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6xup33fwn68j3y45vp2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bGbndIxF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6xup33fwn68j3y45vp2c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next day, she was talking with her data analyst, Tim. Tim helps in calculating the recommended price of products based on various factors. After talking, she realized that Tim did not need the supplier column, yet the table he used for analysis had the supplier column. She felt it was unnecessary to have the columns one does not need. But she could not remove that column since she needed it for logistics purposes. She was perplexed.&lt;/p&gt;

&lt;p&gt;Later, one day, she was taken aback to know that product name is not unique. It looked like product names can repeat with different categories. Because of the unique constraint, she was not able to put some product data into the table. She had to remove the unique constraint from the table immediately to avoid data loss.&lt;/p&gt;

&lt;p&gt;She had to face similar problems frequently until she met her friend Amy. Amy patiently listened to her problem. She explained to Ria that she has been following 'schema on write' approach for her data. She added that 'schema on read' would have been a better fit for her use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Schema on write
&lt;/h3&gt;

&lt;p&gt;'Schema on write' approach is where we have a predefined schema for the data. Traditionally, most applications use relational databases to store data. In relational databases, we have multiple tables which has well defined column names, data types, constraints and even indexes. This is suitable for transaction data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Schema on read
&lt;/h3&gt;

&lt;p&gt;'Schema on read' approach is where we do not enforce any schema during data collection. When reading the data, we use a schema based on our requirements. In our example, Ria could have two different schemas: one with 'supplier' column for her logistic needs and one without 'supplier' column for the data analyst.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--00ZQ_QdX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kwnca49zo3ulo6ytj5jv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--00ZQ_QdX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kwnca49zo3ulo6ytj5jv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there is an additional column coming as part of the data, that would be collected even before modifying the schema. Schema can be modified to include the additional column if it is needed. One big risk with Schema on Write is that schema is enforced on data up front and there are chances that we might lose additional information.&lt;/p&gt;

&lt;p&gt;Also if any constraint in schema used for reading is not satisfied, it does not affect data collection. Data collection is independent of the schema. &lt;/p&gt;

&lt;p&gt;Ria understood what Amy suggested. She agreed that 'schema on read' would have been a better fit for her use case and started working towards it.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Please post your feedback and comments.&lt;/p&gt;

</description>
      <category>database</category>
      <category>data</category>
      <category>dataengineering</category>
      <category>schema</category>
    </item>
  </channel>
</rss>
