<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prasenjeet Kumar on Medium]]></title>
        <description><![CDATA[Stories by Prasenjeet Kumar on Medium]]></description>
        <link>https://medium.com/@prasenjeetsymon?source=rss-1f0743b77f98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mdx6B6nm9W31k5zNh75veg.jpeg</url>
            <title>Stories by Prasenjeet Kumar on Medium</title>
            <link>https://medium.com/@prasenjeetsymon?source=rss-1f0743b77f98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 17:40:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prasenjeetsymon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SQL: The Practical Guide — SELECT with WHERE keyword and comparison operators ( part 10)]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-select-with-where-keyword-and-comparison-operators-part-10-489dee20f083?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/489dee20f083</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Sun, 16 Jun 2019 16:27:25 GMT</pubDate>
            <atom:updated>2019-06-16T16:27:25.934Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — SELECT with WHERE keyword and comparison operators ( part 10)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-W4_ED_6hxva6-cJSpMkXw.jpeg" /></figure><p>Use of the WHERE keyword in the SQL code is that by using this you can filter your final result in the way you want. This is the most interesting feature of the SQL . Please keep reading because the thing is getting hot.</p><p>Consider the code shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*W84_uhdXsvHpTg0-" /><figcaption>select with where keyword</figcaption></figure><p>After running the above code you will get the following result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SwgGBJ0G7Ln6u4BH" /></figure><p>These are the steps taken by the MYSQL server</p><ul><li>MYSQL server first check for the WHERE keyword if it found that then it check for the primary key column in your condition if your condition does not involve only primary key column then it decides that it must go through each and every row of the table to find the row you are looking for because this is the only possible way that MYSQL server can find the row that satisfy your condition by going through every row one by one</li><li>Then it finds the table name from which to extract the data by looking for the FROM keyword. Once it knows from which table to extract the data it moves to next step</li><li>Then in this step it follows the following steps</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MSw21mLUXCEGKt1g" /></figure><p>It first selects the first row from the target table and move to step 1 , In this step it first replace the column_name that is in your conditions with the corresponding value from the selected row, it then evaluate the condition</p><ul><li>If the condition is true ( 1 is true in SQL ) then move to the step 2</li><li>If the condition evaluates to false ( 0 is false in SQL ) then move to step 3</li></ul><p>STEP 2 :</p><p>As you can see in the above image MYSQL accessing from the selected row the value of the columns that is listed after the SELECT keyword and also that appear in the expression like <strong><em>gender </em></strong>and evaluating any expression that is there (like there is an expression “<strong>CONCAT</strong>(“Person’s gender is “,gender)” MYSQL will put the value of gender and then evaluate the expression resulting into value “<strong><em>Person’s gender is male</em></strong> “ ) and finally pushing the final row into the virtual table.</p><p>STEP 3 :</p><p>In this step MYSQL try to change the selected row to the next row of original table, if the next row exist then it makes this row selected and move to STEP 1 and if next row do not exit then it returns the result ( virtual table )</p><h3>COMPARISON OPERATOR</h3><p>These are operators that compare two values and return either 1( True ) or a 0 ( False ) . In SQL we use the comparison operators to filter our result set</p><p>In SQL these are the comparison operator</p><ul><li>=</li></ul><p>This operator compare the two value for the equality and return 1 if equal and 0 if not</p><ul><li>&lt;</li></ul><p>This operator compare the two values for inequality and check if left hand side value is less than the right hand side value if this is the case then it returns 1 ( TRUE ) otherwise return 0</p><ul><li>&gt;</li></ul><p>This operator compare the two values for inequality and check if left hand side value is greater than right hand side value then return 1 ( TRUE ) otherwise return 0</p><ul><li>!=</li></ul><p>This operator compare the two value and check if two values are equal to each other if equal then return 0 otherwise return 1</p><ul><li>&lt;=</li></ul><p>This operator compare the two values and check if two values are equal to each other or left hand side value is less than the right hand side if this is the case then it return 1 otherwise return 0</p><ul><li>&gt;=</li></ul><p>This operator compare the two values and check if two values are equal to each other or left hand side value is more than right hand side then it return 1 otherwise 0</p><p>Let us see all the examples of above comparison operators one by one</p><p><strong>THE EQUAL TO OPERATOR ( = )</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*EvzO6vU761KdOT7Z" /></figure><p>And result set of above query is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RSbGgJ5ozCFrehWa" /></figure><p>Above query is finding the person having the person_id equal to ( = ) 6 from the person_table</p><p><strong>THE LESS THAN OPERATOR</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sqEQ-K2ykRvPKUg1" /></figure><p>And result set of above query is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0XaCmxyDcVy-zzUf" /></figure><p>Above query is finding the person from the person_table having the person_id less than ( &lt; ) 6.</p><p><strong>THE GREATER THAN OPERATOR</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yLPP1KxRK5dL2IwR" /></figure><p>And result set of the above query</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QLXRAe-88MJgYn8V" /></figure><p>Above query is finding the person from the person_table having the person_id greater than (&gt;) 6.</p><p><strong>THE NOT EQUAL TO OPERATOR</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Z7nVUMXOlRlIyd0w" /></figure><p>And result set is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xK26Mz5LHwk1Yzhm" /></figure><p>Above query is finding the person from the person_table having person_id not equal to ( != or &lt;&gt; ) 6</p><p><strong>THE LESS THAN OR EQUAL TO ( &lt;= )</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hAcJXMzn1Hh7fhch" /></figure><p>And the result set is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QLjhmneHCK_vfSJ8" /></figure><p>Above query is finding the person having the person_id less than 6 or equal to 6 ( person with person_id is included in the result set as opposed to less than ( &lt; ) only )</p><p><strong>THE GREATER THAN OR EQUAL TO ( &gt;= )</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Q5_cbJec_fSAAh4g" /></figure><p>And the result of the above query is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pgXx_SlJem0_DkEJ" /></figure><p>Above query is finding all the person having person_id greater than or equal to 6 ( note that person with person_id equal to 6 is included in the result set )</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=489dee20f083" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Selecting data from table ( part 9 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-selecting-data-from-table-part-9-367c2d587a3b?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/367c2d587a3b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Wed, 05 Jun 2019 05:12:39 GMT</pubDate>
            <atom:updated>2019-06-05T05:12:39.845Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Selecting data from table ( part 9 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8keM38gSKWsZx1vRGl1O8g.jpeg" /></figure><p>After adding some dummy data to both these tables our final tables look like shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gvjhayeWv9Ou9foz0t5veg.png" /><figcaption>address_table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eMtGYK4eAzQmkOewsmSqiQ.png" /><figcaption>person_table</figcaption></figure><p>In order to read the data from the table ( select the data ), we are going to use SQL data statement called as <strong><em>SELECT</em></strong></p><p>To select the data from the table you must have to specify the table name to the database server and also from which column in the table you want the data.</p><p>To select every person’s mobile number from the <strong><em>person_table </em></strong>run following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*pWYQCNKuNUmPgLCMPh65Cw.png" /><figcaption>code to select mobile number of every person from person table</figcaption></figure><p>After running the code above you will get the following result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*cYoj1lt1WlFlZ1FZ9rha2A.png" /></figure><p>To select all person’s mobile number and date of birth run the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*bCKWPwQ4oUFl3w17LGhx6g.png" /><figcaption>code to select mobile number and date of birth</figcaption></figure><p>After running the code you will see the following information printed on the screen</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*vPgGKLA7Sh3DM6JwAI6X8w.png" /></figure><p>In the above SQL code on the line 1 we are listing all the column name from which we need the data separated by comma after the <strong>SQL SELECT</strong> keyword , on the line 2 we are telling the MYSQL server the name of the table from which we need the data after the <strong>SQL FROM</strong> keyword</p><p>So , if you need to know the person’s address_id , mobile number, birth date and gender from the <strong><em>person_table </em></strong>you will use the following code .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*YaVfCUlmfyou0E9W_8SZVw.png" /></figure><p>After running the code the result set will look something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*Zr5U1wbZydfCzIB_AagJfQ.png" /></figure><p>As you can see in the result set we have <strong><em>person_id</em></strong>, <strong><em>mobile_number</em></strong>, <strong><em>birth_date </em></strong>and <strong><em>gender </em></strong>values of all the person currently in the table.</p><h3>EXPRESSION WITH SELECT</h3><p>Not only you can use column name after the SELECT keyword but you can also use <strong>expression </strong>too, For example look at this code below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k94PjXt-hNHHtFSTtvthTA.png" /></figure><p>And the result is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FYNpoqmWpK8yipaIJfrvUg.png" /></figure><p>In order to understand what is going on here you need to understand how the <strong>SELECT </strong>statement work or how the code show above is actually executed by the MYSQL server. ( This is just the mind map not the actual process )</p><p>These are the steps that is followed by the MYSQL server to select data from the table.</p><ul><li>First MYSQL server check for the <strong>WHERE </strong>keyword , if there is no <strong>WHERE</strong> keyword then it assume that you are interested in all the record available in the table .</li><li>Then after it check for the table name which is after the keyword <strong>FROM </strong>( this tell the MYSQL server that from which table you want the data )</li><li>Then it create the <strong><em>virtual table</em></strong> ( the table returned by the query ) and create the column with the name that is listed after the keyword <strong>SELECT</strong>. If there is expression in this list then whole expression will be taken for the name of the column in the <strong><em>virtual table .</em></strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*vwSjvPBUGu7g3QXA_gNLlw@2x.png" /><figcaption>virtual table</figcaption></figure><ul><li>Then it start populating the <strong><em>virtual table</em></strong> with data like show below</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yK8NmKgS_c3VEHQ0vEqlbQ.png" /><figcaption>process taken by MYSQL to select the data from table</figcaption></figure><p>The process show above continue until it reach the last row of the original table ( <strong>person_table </strong>).</p><p>Since there is no <strong>WHERE </strong>keyword in our SQL code that’s why MYSQL is assuming that you need every row of the table. ( more on this later )</p><p>As you can see in the above image MYSQL is starting from the first row of original table and accessing the value of the columns that is listed after the <strong>SELECT </strong>keyword and also that appear in the expression like <strong><em>gender </em></strong>and evaluating any expression that is there (like there is expression “<strong>CONCAT</strong>(“Person’s gender is “,gender)”</p><p>MYSQL will put the value of gender and then evaluate the expression resulting into value “<strong><em>Person’s gender is male</em></strong> “ and finally pushing the final row into the virtual table.</p><p>If you know how this process work , then you can understand the future concept in the way that will be very unique for you and finally you will be able to write the complex and efficient query easily.</p><p>Now this the time to explore the SQL code in which there is <strong>WHERE </strong>keyword.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=367c2d587a3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Inserting data into table , Adding Foreign Key constraint and Altering…]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-inserting-data-into-table-adding-foreign-key-constraint-and-altering-54b8536192a7?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/54b8536192a7</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 15:56:13 GMT</pubDate>
            <atom:updated>2019-06-02T16:07:28.073Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Inserting data into table , Adding Foreign Key constraint and Altering Table ( part 8 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CXcr_XG_C5ZSUL4oFGNpIA.jpeg" /></figure><p>In this article we will talk about “How to insert the data into table” and “How to add foreign key constraint by altering the table”</p><p>To insert the data into the table we will use the SQL schema statement called <strong>INSERT INTO</strong></p><p>Before inserting data into the <strong><em>person_table</em></strong>, it would be useful to discuss how values are generated for numeric <strong>primary keys.</strong></p><p>Other than picking a number out of thin air, you have a couple of options:</p><ul><li>Look at the largest value currently in the table and add one.</li><li>Let the database server provide the value for you.</li></ul><p>Although the first option may seem valid, it proves problematic in a multi-user environment, since two users might look at the table at the same time and generate the same value for the primary key. Instead, all database servers on the market today provide a safe, robust method for generating numeric keys. In some servers, such as the Oracle Database, a separate schema object is used (called a sequence)</p><p>In the case of MySQL, however, you simply need to turn on the auto-increment feature for your primary key column. Normally, you would do this at the time of table creation, but doing it now provides the opportunity to learn another SQL schema statement, <strong>ALTER TABLE</strong>, which is used to modify the definition of an existing table</p><p><strong>MODIFYING THE TABLE</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*EdaWsC7YyoIy9CGZ1e0Txg.png" /><figcaption>altering the table person_table to modify person_id column</figcaption></figure><p>As you can see in the above code we are modifying the <strong><em>person_id </em></strong>column of the <strong>person_table </strong>by using the <strong>ALTER TABLE </strong>SQL schema statement. This statement essentially redefines the <strong><em>person_id </em></strong>column of the <strong>person_table</strong></p><p>After running the above code If you describe the table, you will now see the <strong>auto-increment</strong> feature listed under the “Extra” column for <strong><em>person_id</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H_egN_QEYD05JkjvBto6ew.png" /><figcaption>Auto increment feature is ON</figcaption></figure><p>When you will insert data into the <strong>person_table</strong>, simply provide a <strong>null </strong>value for the <strong><em>person_id </em></strong>column, and MySQL will populate the column with the next available number (by default, MySQL starts at 1 for auto-increment columns).</p><p>Activate the <strong>auto_increment </strong>feature of the <strong>address_table</strong>’s primary key column called <strong><em>address_id </em></strong>by running the code below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P98WbBBRZD2kBQ3jbbwufw.png" /></figure><p>Describe the <strong>address_table </strong>to see that <strong>auto_increment </strong>feature is active or not</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ypTruxMXF7TNbI-o-OEtMw.png" /><figcaption>Auto increment feature of the address_table is ON</figcaption></figure><p>As you can see the <strong>auto_increment </strong>feature is active on the primary key column called <strong><em>address_id</em></strong>.</p><p>Now to insert a record into the <strong>person_table </strong>run the following commands:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*CRA6nyOaAIcONyMeTooy8g.png" /><figcaption>code to insert data into person_table</figcaption></figure><p>In the above code Line 1 is telling the MYSQL server which table to choose to insert the data, Line 3 to 11 is listing all the column name in which to insert the data and Line 15 to 23 is listing all the corresponding values in the order in which column names appear</p><p>To see the record that is inserted into the <strong>person_table </strong>run the following command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TXaiwt5jnbP00n9U6VRR8g.png" /><figcaption>selecting all record from person_table</figcaption></figure><p>As you can see one record is added to <strong>person_table</strong></p><p>To insert a record into the <strong>address_table </strong>run the following command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*cVe7wUg8jIzyXlFhowolfA.png" /><figcaption>code to insert the data to address_table</figcaption></figure><p>To see the inserted record run the following command ( more on this later )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HCtYpXPeupTjcDoni2LheQ.png" /><figcaption>selecting all record from address_table</figcaption></figure><p>As you can see there is one record in the <strong>address_table</strong>. As I discussed some time ago that <strong>address_table </strong>is <strong>parent table</strong> and <strong>person_table </strong>is <strong>child table</strong> because one of the columns of the <strong>person_table </strong>called <strong><em>address_id </em></strong>is dependent for its value on the <strong>address_table </strong>column called <strong><em>address_id</em></strong>. And I also said that <strong><em>address_id </em></strong>column of the <strong>person_table </strong>is a foreign key column. This means we must have to insert the data into <strong>address_table </strong>first and use the value of an <strong><em>address_id </em></strong>column of <strong>address_table </strong>to insert the data to <strong>person_table </strong>( we need this value for the <strong><em>address_id </em></strong>column of the <strong>person_table </strong>or we should use the value that already exists in the <strong><em>address_id</em></strong> column of the <strong>address_table</strong>)</p><p>But this is not happening in the above code, we are inserting data into <strong>person_table </strong>without any value in the <strong>address_table</strong>, this should result in an error</p><p>Well, we are not getting the error because MYSQL does not know that <strong>person_table </strong>is dependent on the <strong>address_table</strong>. To tell this thing you have to add the <strong>FOREIGN KEY</strong> constraint on these tables. Note that you can add foreign key constraint at the time of table creation. But we already have these tables we are going to ALTER these tables</p><p><strong>ADDING FOREIGN KEY CONSTRAINT</strong></p><p>To add the foreign key constraint between the <strong><em>address_id </em></strong>column of the <strong>person_table </strong>and <strong><em>address_id </em></strong>column of the <strong>address_table </strong>use the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GDBV6t00Txe2sM21p8Z4Sg.png" /><figcaption>adding foreign key constraint</figcaption></figure><p>The code above is adding the foreign key constraint named “person_table_address_id_to_address_table_address_id” between the <strong><em>address_id </em></strong>column of the <strong>person_table </strong>and <strong><em>address_id </em></strong>column of the <strong>address_table</strong>.</p><p>Now this constraint in place try to insert the record into the child table ( <strong>person_table</strong>) with the foreign key that does not exist in the <strong>address_table</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*gpOkzHV-KeoQh3RzDSgWxg.png" /><figcaption>code to insert the data into person table</figcaption></figure><p>As you can see on line 23, we are trying to add the value 3 to the <strong><em>address_id</em></strong> column of the <strong>person_table </strong>but this value does not exist in <strong><em>address_id</em></strong> column ( primary key column ) of the <strong>address_table</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*8XlHEKaz1E5_8Mk1hudLLw.png" /><figcaption>selecting all record from address_table</figcaption></figure><p>As you can see there is only one record in the <strong>address_table </strong>with <strong><em>address_id</em></strong> column value equal to 1.</p><p>If you will run the above code you will get following error</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SIKe6wmk0mWnX3vvM25M2A.png" /><figcaption>error when inserting data into child table without values in parent table</figcaption></figure><p>Now change the value from 3 to 1 on line 23 and re-run the code. After running the code check all the record of the <strong>person_table</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QqHr8K5eryD1RgrgVMeBRw.png" /><figcaption>selecting all record from person_table</figcaption></figure><p>As you can see the record is successfully added to the <strong>person_table </strong>with person_id 4 ( can you tell why this happen that value of the primary key column directly jump from 1 to 4 )</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54b8536192a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Create Table ( part 7 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-create-table-part-7-e9ede9ffb82c?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9ede9ffb82c</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Sat, 01 Jun 2019 14:28:24 GMT</pubDate>
            <atom:updated>2019-06-01T14:28:24.810Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Create Table ( part 7 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kwkc_7Yosj69YWbWlDQ-oA.jpeg" /></figure><p>Now we know how many tables we need and also the column name and data type this is the time to create the table</p><p><strong>CREATE SCHEMA STATEMENT</strong></p><p>To create the table we will use the SQL SCHEMA STATEMENT called as <strong>CREATE TABLE</strong></p><p>To create the <strong><em>person_table </em></strong>open the MySQL command prompt and type the following code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*XLGYcIFefrZnqpuf1e5g8Q.png" /><figcaption>code to create the person_table</figcaption></figure><p>I will try to teach you this code line by line. Please note that word that is in uppercase is SQL keyword</p><p>Line 1 is creating the table named <strong><em>person_table</em></strong>, line 3 to 11 is creating a <strong><em>column </em></strong>and line 12 is setting a <strong><em>constraint </em></strong>on the column.</p><p><strong>LINE 1: </strong>This line is using the SQL schema statement called as <strong>CREATE TABLE</strong> to create the table named <strong><em>person_table</em></strong><em>.</em></p><p><strong>LINE 3: </strong>This line is creating the column on the <strong><em>person_table </em></strong>called <strong><em>person_id</em></strong> and setting the data type of this column to an unsigned version of INT (<strong><em> INT UNSIGNED</em></strong> )</p><p><strong>LINE 4: </strong>This line is creating the column called <strong><em>first_name </em></strong>and setting the data type of this column to <strong>VARCHAR(20)</strong></p><p><strong>LINE 5: </strong>This line is creating the column called <strong><em>last_name </em></strong>on the <strong><em>person_table</em></strong> and setting the data type of this column to <strong>VARCHAR(20)</strong></p><p><strong>LINE 6</strong>: This line is creating the column called <strong><em>mobile_number </em></strong>and setting it’s data type to <strong>CHAR(10)</strong>. Note the keyword “<strong>NOT NULL</strong>” this keyword tell the MySQL server that this column can’t accept <strong>NULL </strong>value at the time of data insertion on this table. This means you must have to supply value to this column at the time of inserting data to this table</p><p><strong>LINE 7: </strong>This line is creating the column called <strong><em>income_in_year </em></strong>and setting data type of this column to <strong>DOUBLE</strong></p><p><strong>LINE 8:</strong> This line is creating the column called <strong><em>birth_date </em></strong>and setting data type <strong>DATE</strong>, So that this column can only accept the date value</p><p><strong>LINE 9:</strong> This line is creating the column called <strong><em>relation_status </em></strong>and setting the data type of this column to special data type provided by MYSQL called <strong>ENUM</strong>. To declare the <strong>ENUM </strong>data type we use the following syntax</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*7bDZegGInnAuAhEBnr_sOQ.png" /></figure><p>An <strong>ENUM </strong>column can have a maximum of 65,535 distinct elements. This means you can list a maximum of 65,535 elements inside the <strong>ENUM</strong>. Inside the <strong>ENUM </strong>you should only list the sequence of characters.</p><p><strong>LINE 10: </strong>This line is creating the column called <strong><em>gender </em></strong>and setting the data type called <strong>ENUM </strong>on this column</p><p><strong>LINE 11:</strong> This line is creating the column called <strong><em>address_id </em></strong>and setting the data type <strong>INT </strong>( unsigned version )</p><p><strong>LINE 12:</strong> This line is important one this line is creating the constraint called <strong><em>primary_key_person_id </em></strong>and setting the <strong>PRIMARY-KEY </strong>constraint on the <strong><em>person_id </em></strong>column. This means this line is telling the MySQL server that <strong><em>person_id </em></strong>is the primary key column of the table. With this constraint in place, you will not be able to insert the duplicate value to the person_id column and also MYSQL server will index this column for faster searching of the row in this table using B-TREE ( more on this later )</p><p>Now use this code to create the <strong><em>address_table</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*lZHo7VoVnvmLoYlTIMTH5g.png" /><figcaption>code to create the address_table</figcaption></figure><p>After executing this code you will have <strong>person_table </strong>and <strong>address_table</strong>. To check this is the case run this command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*eerQx3uM8qmQQBsIAjpoJw.png" /></figure><p>As you can see we have two tables in our database called <strong>address_table </strong>and <strong>person_table</strong>.</p><p>To see column name and its corresponding data type with other useful information of any table say <strong>person_table </strong>run this command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WnQuyQad9qR0b6zMgZr2BA.png" /><figcaption>describe the person_table</figcaption></figure><p>As you can see above table is describing the <strong>person_table</strong></p><ul><li><strong>Field </strong>column is listing all the column that is inside the <strong><em>person_table</em></strong>.</li><li><strong>Type </strong>column is listing corresponding data type of the column</li><li><strong>Null </strong>column is listing which column of the <strong><em>person_table </em></strong>can accept the value <strong>NULL </strong>at time of inserting the data to table</li><li><strong>Key </strong>column is listing which column of <strong><em>person_table </em></strong>is primary key column. In this case, you can see that <strong>person_id </strong>is acting as primary key column</li><li><strong>Default </strong>column is listing default value of the corresponding column</li></ul><p>Now we have tables this is the time to insert the data to these tables</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9ede9ffb82c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Thought process while creating tables in database ( part 6 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-thought-process-while-creating-tables-in-database-part-6-412d6f8230ea?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/412d6f8230ea</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Sat, 01 Jun 2019 07:24:00 GMT</pubDate>
            <atom:updated>2019-06-01T07:24:00.200Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Thought process while creating tables in database ( part 6 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*5n6U0U7Xmue6z-slyghUxA.jpeg" /></figure><p>In this article we will learn about thought process while designing tables for database</p><p>We will try to create the phone-book database from scratch. We will extend the phone book database by introducing some new column.</p><p>We are going to generate the phone book database which is going to contain the following information about a person</p><ul><li>Person’s full name</li><li>Person’s mobile number</li><li>Person’s income in one year ( in the dollar )</li><li>Person’s date of birth</li><li>Person’s relationship status</li><li>Person’s gender</li><li>Person’s full address ( should contain a street name, city/town/village name, state, country, zip code, latitude, longitude )</li></ul><h3>THOUGHT PROCESS</h3><p>Before creating tables you must decide how many tables you need to fully store your data and what are the names of those tables</p><p><strong>HOW MANY TABLES WE NEED</strong></p><p>To decide this part you should follow the normalization process. As we know this process state that there should not be any duplicate data in the entire database and also there should not be any compound value in the tables.</p><p>If you will carefully look at your data then you will find that address data is one that may repeat in the database because there may be more than one person having the same address. That’s why we need to store this value in a separate table</p><p>The address value is compound in nature because it contains more than one property like street name, city/town/village name, state, country, zip code, latitude and longitude.</p><p>And we also know that properties of compound value should be stored in a separate column.</p><p>Person’s full name value is also compound in nature because it contains more than one property like first name, middle name and last name and these properties should also be stored in a separate column</p><p>So finally we conclude that we need two separate table — one for the address data and other for the person’s data.</p><p>The table that is going to hold the address value we will name that table “address_table” and table that is going to hold the person data will be called as “person_table”. You can call these tables whatever you want but we will stick with this name in this article</p><p><strong>DECIDE THE NAME AND DATA TYPE OF THE COLUMN</strong></p><p>For the person table, we will use the following column name and data type</p><ul><li>person_id: This column is going to act as the primary key column of the table. The data type of this column will be <strong><em>INT UNSIGNED ( </em></strong>unsigned integer )</li><li>first_name : This column will hold the first name of a person and will have data type of <strong><em>VARCHAR( 20 )</em></strong> because I think there will be no any person in the future having first_name greater than 20 characters ( this is just my design decision if you think you need more character you are free to use any number of characters less than 65,535 )</li><li>last_name: This column will hold the last name of the person and will have a data type of <strong><em>VARCHAR( 20 )</em></strong></li><li>mobile_number: This column will hold as you guessed the mobile number of the person. I will assume that mobile number will not be greater than 10 characters long and it will always be 10 characters long ( fixed length ) that’s why we will use in this case <strong><em>CHAR(10)</em></strong> as our data type.</li><li>Income_in_year: This column is going to hold the person’s income in a year. The data type of this column will be <strong><em>Double </em></strong>because income of the person may be floating point number like 2345.345</li><li>birth_date: This column is going to hold the date of birth of a person and data type of this column will be <strong><em>date </em></strong>because the time of birth of a person is not important but if you think you need time also you should use <strong><em>datetime</em></strong> data type</li><li>relation_status: This column is going to hold the relationship status of a person that will tell whether that person is married or not. All possible values that are allowed in this column are “married”, “single” and “complicated”. Since only three values are allowed in this column we are going to use the special data type provided by MYSQL called <strong><em>ENUM.</em></strong> if you are using other databases you may use the equivalent of ENUM or <strong>VARCHAR( 15 )</strong>. With this data type in place you will not be able to insert other value than “married”, ”single”and “complicated”</li><li>gender: This column is going to hold the gender of a person. Since all possible value are “male”, “female”, and ”other” again we will use <strong>ENUM</strong> as our data type ( I will show you later how to declare the <strong>ENUM </strong>data type )</li><li>address_id: This column will act as a foreign key column and will hold the address_id column’s values of the address_table with data type of<strong> INT UNSIGNED</strong></li></ul><p>For the address table, we are going to use the following column name and data type</p><ul><li>address_id: This column is going to act as a primary key column and will hold the primary keys of the address_table. The data type of this column will be <strong>INT UNSIGNED</strong></li><li>street: This column is going to hold the property of the address object called street name and data type of this will be <strong>TEXT </strong>because the street name can be more than 255 characters long.</li><li>town: this column is going to hold the properties like village name, town name or city name of the address object and data type of this column will be <strong>TEXT</strong></li><li>state: This column will hold state name and data type will be <strong>VARCHAR </strong>( 300 ) because I think there is no state name having length of characters greater than 300. If you think there is you can choose any number of characters less than or equal to 65,535</li><li>zip_code: this column will hold zip code property of the address object and data type of this column will be <strong>VARCHAR</strong>(15)</li><li>country: this column will hold country name and data type will be <strong>VARCHAR</strong>(300)</li><li>latitude: this column will hold the latitude and data type will be <strong>Double </strong>because latitude value is floating point number like latitude of Alaska (US) is 64.2008.</li><li>longitude: this column will hold longitude and data type will be <strong>Double</strong></li></ul><p>Now we know how many tables we need and also the column name and data type this is the time to create the table</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=412d6f8230ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Text data type ( part 5.2 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-text-data-type-part-5-2-49939b1960ae?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/49939b1960ae</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Fri, 31 May 2019 05:32:45 GMT</pubDate>
            <atom:updated>2019-06-01T14:32:59.183Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Text data type, Numeric data type and Temporal data type( part 5.2 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CXgmpX95Mhl9jNMFahkdzA.jpeg" /></figure><p>If you want to store the data that might exceed 64 KB limit of VARCHAR, then text data type is good for you</p><p>Clearly, you can only store text value in the text column.</p><p>There is different types of text data in SQL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*iXRA2GL4M0RvWco5-Jvvug.png" /></figure><p><strong>TINYTEXT</strong></p><p>If you want to store a maximum of 255 characters in the column you can use <strong><em>tinytext</em></strong> data type. Yes, you can also use CHAR data type, I will suggest you to use CHAR type.</p><p><strong>TEXT</strong></p><p>A column with data type <strong><em>text </em></strong>can store up to 64 KB of data ( sequence of characters of length 65, 535). But this is also the maximum limit of the VARCHAR, So you can use VARCHAR also to store such value. I will suggest you to use the VARCHAR instead</p><p><strong>MEDIUMTEXT</strong></p><p>You can also use <em>mediumtext </em>if your data is greater than 65, 535 Byte. This data type is capable of storing data of size 16 MB</p><p><strong>LONGTEXT</strong></p><p>You can store data having a size greater than 16,777,215 Byte. if you want to store the whole article you can use this data type on your column. This data type is capable of storing up to 4 GB of data</p><p>When choosing to use one of the text types, you should be aware of the following:</p><ul><li>If the data being loaded into a text column exceeds the maximum size for that type, the data will be truncated if MySQL is not in strict mode</li><li>Trailing spaces will not be removed when data is loaded into the column.</li><li>When using text columns for sorting or grouping, only the first 1,024 bytes are used, although this limit may be increased if necessary.</li></ul><h3><strong>NUMERIC TYPE</strong></h3><p>To store the number in the table there is a numeric data type that you can use.</p><p>There is a different types of the numeric data type</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*PnHtwh0IqVlneNYXwC_yoA.png" /></figure><p><strong>TINYINT</strong></p><p>If you want to store integer you can use this data type. You can use either a signed or unsigned version of this data type.</p><blockquote>The signed version will store the data which is either a positive integer or negative integer</blockquote><blockquote>In the signed version you can store positive integer not more than 127. if you will try to store the positive integer greater than 127 say 129 MySQL server is going to give you an error</blockquote><blockquote>You can store the negative number not less than -128 in the signed version of the <em>tinyint</em>. If you will try to insert the number less than -128 say -134 then MySQL server is going to give you an error</blockquote><blockquote>The unsigned version will store the data which is a positive integer.</blockquote><blockquote>In the unsigned version, you can store the positive integer not more than 255. if you will do so MySQL server will give you an error</blockquote><p><strong>SMALLINT</strong></p><p>A signed version of this data type can hold integer not less than -32,768 and not greater than 32, 767</p><p>An unsigned version of this data type can hold positive integer not greater than 65,535</p><p><strong>MEDIUMINT</strong></p><p>A signed version of this data type can not hold integer less than -8,388,608 and greater than 8,388,607</p><p>An unsigned version of this data type can hold positive integer not greater than 16,777,215</p><p><strong>INT</strong></p><p>Signed version of this data type can’t hold integer less than -2,147,483,648</p><p>And greater than 2,147,483,647</p><p>An unsigned version of this data type can hold positive integer not greater than 4,294,967,295</p><p><strong>BIGINT</strong></p><p>Signed version of this data type can hold integer not less than -9,223,372,036,854,775,808 and not greater than 9,223,372,036,854,775,807</p><p>Unsigned version of this data type can hold positive integer not more than 18,446,744,073,709,551,615</p><p>When you create a column using one of the integer types, MySQL will allocate an appropriate amount of space to store the data, which ranges from one byte for a <em>tinyint </em>to eight bytes for a <em>bigint</em>. Therefore, you should try to choose a type that will be large enough to hold the biggest number you can envision being stored in the column without needlessly wasting storage space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ftIYkTpMbH_pbSg960Wthg.png" /></figure><p><strong>FLOATING POINT</strong></p><p>For floating-point numbers (such as 3.1415927), you may choose from the numeric types shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*PpwJQVxWlKfzc_08RN_G5w.png" /></figure><p>When using a floating-point type, you can specify a precision (the total number of allowable digits both to the left and to the right of the decimal point) and a scale (the number of allowable digits to the right of the decimal point), but they are not required. I suggest you not to use the precision and scale value.</p><p>If you specify a precision and scale for your floating-point column, remember that the data stored in the column will be rounded if the number of digits exceeds the scale and/or precision of the column.</p><p>For example, a column defined as<em> float(4,2) </em>will store a total of four digits, two to the left of the decimal and two to the right of the decimal.</p><p>Therefore, such a column would handle the numbers 27.44 and 8.19 just fine, but the number 17.8675 would be rounded to 17.87, and attempting to store the number 178.375 in your float(4,2) column would generate an error.</p><p>Like the integer types, floating-point columns can be defined as unsigned, but this designation only prevents negative numbers from being stored in the column rather than altering the range of data that may be stored in the column.</p><p><strong>Temporal Data</strong></p><p>Along with strings and numbers, you will almost certainly be working with information about dates and/or times. This type of data is referred to as temporal data.</p><p>There is different type temporal data as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*oAIzR_AKbb-A-Axul9Z6fw.png" /></figure><p><strong>DATE</strong></p><p>If you want to hold the date only in the column you should use the <strong>date</strong> data type. As you can see in the allowable values column in the above table it’s range is 1000–01–01 to 9999–12–31 .</p><p>This means you can insert the date which is between 1000–01–01 to 9999–12–31. To insert the date your date format should be exactly like YYYY-MM-DD So for example in the column with the <strong><em>date </em></strong>data type you can insert the date like ‘2019–04–05’ but you can’t insert date like ’23 Jan 2019’ ( invalid date format for MySQL )</p><p><strong>DATETIME</strong></p><p>If you also want to store the time value with the date you can use the <strong><em>datetime </em></strong>temporal data type</p><p>Its format is YYYY-MM-DD HH:MI: SS For example datetime format like “2019–03–07 10:35:45” is correct format to use in the <strong><em>datetime </em></strong>column</p><p>You can only insert the date and time that is between 1000–01–01 00:00:00 to 9999–12–31 23:59:59 ( note that date time is 24-hour format )</p><p><strong>TIMESTAMP</strong></p><p>A column with this data type also store the date and time with a range</p><p>1970–01–01 00:00:00 to 2037–12–31 23:59:59</p><p>There is a difference between <strong><em>datetime </em></strong>and <strong><em>timestamp</em></strong></p><ul><li>The range is different for the timestamp ( smaller range )</li><li>A column with data type <strong><em>timestamp </em></strong>is automatically populated with current date time of the server by database server at the time of insertion of data into the table but in <strong><em>datetime </em></strong>column, you have to manually insert the date and time with correct format.</li></ul><p><strong>YEAR</strong></p><p>A column with this data type can hold year value of the date like “2019”, “2022” etc.</p><p>Minimum year value that you can insert into this column is “1901” and maximum is “2155”</p><p><strong>TIME</strong></p><p>You can store the time value in this type of column with a minimum value of -838:59:59 and a maximum value of 838:59: 59. For example, if you want to store 34 Hours 32 minute and 44 seconds you can store it in this format</p><p>“34:32:44”.</p><p>If your thinking what is the use of negative time then consider this, If any event that is going to happen in future then that will be represented by positive time value and if some event happened in past then that event time is represented by a negative value</p><p>Please note that all date and time should be in the correct format before inserting into the table. For datetime and timestamp the format is YYYY-MM-DD HH:MI:SS and for the time the format is HHH:MI:SS</p><p>The table below lists the range of various component of datetime format</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*-LtahnYoywGKgiJAMzUOEg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49939b1960ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Character data type ( part 5.1 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-character-data-type-part-5-1-d8c51f438718?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8c51f438718</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Tue, 28 May 2019 10:34:17 GMT</pubDate>
            <atom:updated>2019-05-28T10:34:17.869Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Character data type ( part 5.1 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fgAJwpDT1tvCyv0DezerbQ.jpeg" /></figure><p>A column with this data type can store the sequence of characters</p><p><strong>How to declare the column with this data type</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OPX3-fnPTIseu9EDJ96Fjg.png" /><figcaption>way of declaring the character data type</figcaption></figure><p>In the CHAR( n), n is the number that can be 1, 2, 3, or 255. This is just the way of telling the server that you want to store the “n” number of character in the column. For example if you declare your column with <strong>CHAR( 3 ) </strong>then this tell the MySQL server that you want to store a maximum of three characters in the column so in your column you can either store “a” or “aa” or “aaa” but if you try to store “aaaa” server will give you error.</p><p>What if you want to store more than 255 characters in your column, for this, you use the <strong>VARCHAR ( n )</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r8-VW5dP1WRdA434TfpYtw.png" /><figcaption>way of declaring the varchar data type in SQL</figcaption></figure><p>In the VARCHAR( n), “n” can hold the value like 1, 2, 3, or 65535. This way you can store a maximum of 65535 characters in your column</p><h3>Difference between CHAR and VARCHAR</h3><p><strong>CHAR ( n )</strong></p><p>A column with this type of data can store a maximum of 255 characters.</p><p>A column with this type of data can store only the fixed length of characters controlled by “n”. Say you declare your column with CHAR( 3 ) then your column is going to accept a sequence of three characters at a time. Ex “aaa”, “bbb”, “fgh”, “DDN” etc</p><p>If you will try to insert the sequence of character having length less than three like “a” then MySQL server will convert this character into sequence of characters having length 3 by adding required white space on the right side of the character like “a “ ( MySQL added two white space on the right of character “a” to make it a sequence of character having length 3 ) . When you will ask the MySQL server to read this value for you then it is going to remove that two extra white space character and will give you the character “a”.</p><p>Please note that MYSQL will remove any white-space right to the character at the time of reading value, for example, if your column is of data type CHAR ( 3 ) and you inserted the value “a “ which is of the length 3 ( there is two white space after the character a ) into this column then MYSQL will insert this safely but at the time of reading this value MYSQL will remove that two white space and will give the result “a”. MYSQL will always remove the right padded white space at the time of reading value.</p><p>This means no matter how much length your sequence of character has, it is going to always insert the same number of character in the column as defined by n. if the number of character is more than n then it is going to give you error ( if you will try to insert the “aaaa” in the above-defined column then the server will give you error )</p><p>This also means your column value is going to take the same amount of memory on the disk ( depending on the value on n ) no matter what is the length of the sequence of characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*Q0FMzOJwebgz3aqEAMriTQ.png" /></figure><p>In the above table, you can see that no matter what is your value it is always going to consume the same amount of memory.</p><p>Look at the last row of the table which holds the value “abcdefgh”, when inserted into the column with data type CHAR( 4 ), value is truncated to “abcd”. This happens only when MySQL is not in the strict mode otherwise it will give you an error</p><p><strong>VARCHAR( n )</strong></p><p>A column with this data type also store the sequence of characters but instead of a maximum of 255 characters it can store a maximum of<strong> </strong>65, 535 character</p><p>This type of column can store a variable number of characters and will consume only the necessary amount of memory. To understand it more clearly let us consider an example</p><p>Let say you declare a column with data type VARCHAR( 4 ) , this tells the MYSQL server that you want to store the maximum of 4 characters in the column , this means you can store the sequence of characters like “a”, “aa”, “aaa” and “aaaa” but if you will try to insert the “aaaaaa” then MYSQL is going to truncate the value to “aaaa” ( length of 4 characters) if MYSQL is not in strict mode otherwise it will give you error</p><p>It feels like there is no difference between CHAR and VARCHAR, well there is in terms of memory consumption and speed of reading the value.</p><p>Let us take a look at how this data type is going to consume memory on the disk space</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*IMc6q9r6W8yEnKwGaVu86w.png" /></figure><p>As you can see in the table VARCHAR is consuming memory depending on the length of a sequence of characters, this data type is not adding white space on the right of the character and also at time of reading value will not remove any white space.</p><p>You can also note that VARCHAR is consuming one extra byte as compared to CHAR data type</p><p>In terms of speed, sorting or accessing the value from the column having data type CHAR is faster as compared to a column having data type VARCHAR this is all due to the facts that they are placed in memory in a different manner</p><p>So, before creating the table make sure that which data type you should use. I will suggest you to use CHAR if you know that your sequence of characters is not going to exceed 255 characters</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8c51f438718" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — The Language ( part 5 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-the-language-part-5-34c5b2471f01?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/34c5b2471f01</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Mon, 27 May 2019 03:33:27 GMT</pubDate>
            <atom:updated>2019-05-27T03:33:27.369Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — The Language ( part 5 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mtWreUOyPQrcq2NtzPXqxw.jpeg" /></figure><p>SQL is a programming language that we use to store the data in the relational database and also to interact with that data.</p><h3>History</h3><p>In 1970, Dr.E.F. Codd of IBM’s research laboratory published a paper titled “ A relational model of data for large shared data banks” that proposed that data can be stored as sets of tables</p><p>During that period he also proposed a language called DSL/Alpha for manipulating data in the relational tables. Shortly after this paper was released, IBM commissioned a group of the scientist to build the prototype based on the Codd’s ideas.</p><p>This group created a simplified version of the DSL/Alpha called as <strong>SQUARE</strong>. More refinement of this version led to a language called as <strong>SEQUEL </strong>( most of the people still use this name ) which was finally renamed <strong>SQL.</strong></p><p>One thing to note is that SQL does not stand for anything ( but there is a majority of people out there who called it <strong>Structured Query Language</strong> )</p><p>Enough history let’s jump to language itself.</p><h3>SQL: A non-procedural language</h3><p><strong>PROCEDURAL LANGUAGE:</strong> In this type of language you are in full control because you can define what you want and also the mechanism or process by which you are going to achieve that result or goal</p><p>Some examples of procedural language are</p><ul><li>JAVA</li><li>C++</li><li>C</li><li>JavaScript</li><li>Python</li></ul><p><strong>NON-PROCEDURAL LANGUAGE:</strong> In this type of language some of the control is taken away from you, you can define what you want but this the task of the external entity that will decide how to achieve that result.</p><p>SQL is non-procedural language, you tell the language what you want and from where you want but this is up to the database server that will decide the process by which it is going to generate the result set.</p><h3>SQL statement</h3><p>SQL statement is divided into three part</p><ul><li>SQL schema statement</li><li>SQL data statement</li><li>SQL transaction statement</li></ul><p><strong>SQL SCHEMA STATEMENT</strong></p><p>This SQL statement is used to define the data structures stored in the database, like <strong><em>create</em></strong> a statement</p><p><strong>SQL DATA STATEMENT</strong></p><p>This statement is used to manipulate the data structures previously defined using the schema statement. Ex. SELECT statement.</p><p><strong>SQL TRANSACTION STATEMENT</strong></p><p>This statement is used to begin end and roll back the transaction ( we will learn more about this later on )</p><h3>Diving into datatype of the SQL</h3><p>There are too many relational databases out there but we are going to choose the MYSQL (an open source and free database). MYSQL is just a software that implemented the concept of a relational database.</p><p>To understand the data type let us consider the following table</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B1BvIVGF3A6H6eI0r8pY-Q.png" /></figure><p>In the above table, there are seven columns namely <strong>student_id</strong>, <strong>first_name</strong>, <strong>last_name</strong>, <strong>class</strong>, <strong>maths</strong>, <strong>physics </strong>and <strong>roll</strong></p><p><strong>student_id </strong>column is holding the unique student id of the student of the given school and this value is of type number as you can see above, So we can say that this column is holding the data of the type number or more precisely of the type positive integer.</p><p><strong>first_name </strong>column is holding the first name of the student, and data type of these value is string or more precisely sequence of character ( like A, a, B, b … ) this is also true for the last_name column</p><p><strong>class </strong>column is holding the value that is class of the student, data type of this value is number or more precisely positive integer, this is also true for the roll column</p><p><strong>maths </strong>column is holding the marks obtained by the student in maths subject out of 100, data that this column is holding is of the type number ( it may be a negative number, negative integer, a positive integer, positive number or 0), this is also true for the physics column</p><p>This analysis show that every column of the given table must have some sort of data type ( data type of the value that it holds) and also you can only put value having the data type that is allowed by that column, for example, if you will try to insert the value “Mukesh” in the class column database server will give you error</p><p>A single column can hold only the same type of data that is assigned to it at time of table creation, you can’t insert different type of data in the same column</p><p>Now the question is what are all possible type of data that a column can accept</p><p>Well this depends on the type of database software you are using but these are the list of the data type allowed by the majority of the database server which we will discuss one by one</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34c5b2471f01" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Foreign-Key ( part 4 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-foreign-key-part-4-59e68745fdd9?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/59e68745fdd9</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Sat, 25 May 2019 16:58:11 GMT</pubDate>
            <atom:updated>2019-05-27T02:52:02.654Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Foreign-Key ( part 4 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sXKhgH-LINDIrxm9w1DBQw.jpeg" /></figure><p>Foreign-key is the value that connects the two dependent table together.</p><p>To understand the foreign key let us consider the following two tables namely <strong><em>person_table</em></strong> and <strong><em>address_table</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r9If2EQ8_8JXqNnaPGuEqQ.png" /><figcaption>This is the table called <strong>person_table </strong>that holds the name and mobile number of the persons</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Amj6cf7ugwg36gNLEEZzQ.png" /><figcaption>This is the table called <strong>address_table </strong>that holds the address of the region</figcaption></figure><p>Using these two table if I need to find out the name and address of a person given his/her mobile number let say <strong><em>5256551423, </em></strong>I will follow these steps</p><p><strong>STEP 1 :</strong></p><p>First, I will search for the row ( record ) in the person table where a mobile number is <strong>5256551423</strong>. This will give you the following row</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3JBktiw_UDAiw6-4iQJ1A.png" /><figcaption>record extracted from <strong>person_table </strong>where mobile number is equal to <strong>5256551423</strong></figcaption></figure><p>Clearly, this row contains <strong>name</strong>, <strong>phone_number </strong>and also the <strong>address_id</strong>.</p><p>We have only two value related to the person-<strong> name </strong>and <strong>phone number</strong></p><p>In order to find the address of that person we will use the value of <strong>address_id </strong>column that is actually the primary key of the <strong><em>address_table</em></strong></p><p><strong>STEP 2 :</strong></p><p>Next, we will go to <strong><em>address_table </em></strong>and using the value of the <strong>address_id </strong>column of the <strong>person_table </strong>( which is 1 ) we will search for row in the <strong><em>address_table </em></strong>where <strong>address_id </strong>is equal to 1. This will give you following record from <strong><em>address_table</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z9pJk6Ho6ahXu8FAFYayfw.png" /><figcaption>row extracted from the address_table where address_id is equal to 1</figcaption></figure><p>Now we have the address of the person having mobile number <strong>5256551423</strong></p><p><strong>STEP 3 :</strong></p><p>Finally, we will combine these two record to form the final record that will hold the value of <strong>name</strong>, <strong>mobile number</strong>, <strong>street</strong>, <strong>city </strong>and <strong>pin code</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*Kza0HOMgp4upIxc-5oeCAw.png" /></figure><p>Since <strong>address_id </strong>column do not make sense in the final result set we will remove the <strong>address_id </strong>column ( here <strong>address_id </strong>column is only acting as a bridge that connects the <strong><em>person_table </em></strong>and <strong><em>address_table </em></strong>together or more simply it is acting as a foreign key column )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9vGYMboRENH6YdeSMHERQ.png" /><figcaption>Final result set</figcaption></figure><p>We see above that <strong>address_id </strong>column in the <strong><em>person_table </em></strong>is holding the primary keys of the <strong><em>address_table</em></strong>, This column acted as a bridge that connected the <strong><em>person_table </em></strong>and <strong><em>address_table </em></strong>together. Only using this column we were able to find the <strong>address_value </strong>in the <strong><em>address_table</em></strong></p><p><strong>Foreign-key</strong> is the value that connects the two dependent table together.</p><p>We can define more than one column in the given table as a foreign-key column as we can with primary-key column</p><p>In this case, <strong><em>person_table </em></strong>is acting as child table and <strong><em>address_table </em></strong>is acting as parent table because one of the <strong><em>person_table </em></strong>column ( <strong>address_id </strong>) is dependent on the value from the <strong><em>address_table </em></strong>( <strong>address_id </strong>column )</p><p>Some database engine enforce the foreign-key constraint , such that if you will try to insert the value in the foreign key column of the child table ( <strong><em>person_table</em></strong>’s <strong>address_id </strong>column ) and if that value does not exist in the parent table ( <strong><em>address_table</em></strong>’s <strong>address_id </strong>column ) then database engine will not insert that value and will give you error.</p><blockquote>As you can see there is duplicate value in the foreign-key column of the <strong>person_table </strong>and this is perfectly fine.</blockquote><p>Now you do understand what is foreign key , lets move on to next topic and that is <strong>SQL: The query language</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59e68745fdd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL: The Practical Guide — Normalization ( part 3 )]]></title>
            <link>https://medium.com/@prasenjeetsymon/sql-the-practical-guide-normalization-part-3-8029b0ddbf18?source=rss-1f0743b77f98------2</link>
            <guid isPermaLink="false">https://medium.com/p/8029b0ddbf18</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Prasenjeet Kumar]]></dc:creator>
            <pubDate>Fri, 24 May 2019 04:10:47 GMT</pubDate>
            <atom:updated>2019-05-24T04:10:47.126Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL: The Practical Guide — Normalization ( part 3 )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JME-EkuI1Uh_uC-uFeBVgQ.jpeg" /></figure><p>In simple term normalization is the process of distributing the data in the form of a table in such a way that there is no duplicate data ( other than foreign keys — more on this later ) and compound data in the entire database.</p><p>I think you will understand it better by looking at the example</p><p>Let us consider the following table</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e0K_0TN-4kn8E2zvKDQyqA.png" /><figcaption>Table holding the name, address and phone number of the person</figcaption></figure><p>In this table see the <strong>address </strong>column, this column is holding the address of a person. The address is such type of data that may repeat because there may be more than one person having the same address ( this is the case in the above table ), but a well-designed database never hold duplicate data other than foreign-key that’s why we are going to break this table into two table. One of the table will hold the name and phone number of person and other will hold the address of the person.</p><p>After doing some simple work our data now look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r9If2EQ8_8JXqNnaPGuEqQ.png" /><figcaption>person_table holding the name and phone number of the person</figcaption></figure><p>The table above ( <strong>person_table</strong> ) is holding the name and phone number of the person</p><p>There is also a special column called address_id ( foreign-key column ) that is holding the primary key of the record that is in the <strong><em>address_table </em></strong>so that we can easily find the corresponding address of a given person.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WaV2SskNXwIC0tFbCa7f3w.png" /><figcaption>This table is holding the unique address</figcaption></figure><p>The table above is holding the unique address of the region such that there is no duplicate address in the database.</p><p>As you can see there are two columns in the address table the <strong><em>address_id </em></strong>column and <strong><em>address </em></strong>column.</p><p><strong>address_id</strong>: This column is holding the primary key of the address table.</p><p><strong>address</strong>: This is holding the address object ( value )</p><blockquote>Until now we have completed only one part of the process that is <em>no duplicate data in the database </em>but to complete the second part and finally to complete the normalization process we should check that there should be no compound data ( object having more than one properties).</blockquote><p>After looking at the <strong><em>address_table </em></strong>we found that address is actually compound data</p><p>The address is compound data because the address is that type of object that contains more than one properties.</p><p>Address object contains following properties :-</p><ul><li>Street</li><li>Pin-code</li><li>City</li><li>State</li><li>Country</li><li>Latitude</li><li>Longitude</li></ul><p>We should convert the properties of the compound object into the columns, after doing this we get our final address table that satisfies the normalization process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Amj6cf7ugwg36gNLEEZzQ.png" /><figcaption>address table after normalization</figcaption></figure><p>Now you know what is normalization let’s move onto the next concept and that is foreign-key.</p><blockquote>Can you find another compound object in the person table</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8029b0ddbf18" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>