<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL</title>
	<atom:link href="https://renanmf.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Software development, machine learning</description>
	<lastBuildDate>Thu, 10 Jun 2021 15:12:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://renanmf.com/wp-content/uploads/2020/03/cropped-android-chrome-512x512-2-32x32.png</url>
	<title>SQL</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>SQLite in Python</title>
		<link>https://renanmf.com/sqlite-python/</link>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Wed, 12 May 2021 21:36:47 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=3468</guid>

					<description><![CDATA[<p>If you need and introduction to SQL and Databases, I recommend reading these articles before diving into this one: Introduction to SQL SQL: Tables and Basic Structure SQL: Data Types SQL: Syntax SQLite is a database that comes pre-installed with Python, you just have to import it just like any other module and start using [&#8230;]</p>
<p>The content <a href="https://renanmf.com/sqlite-python/">SQLite in Python</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you need and introduction to SQL and Databases, I recommend reading these articles before diving into this one:</p>
<ul>
<li><a href="https://renanmf.com/introduction-to-sql/">Introduction to SQL</a></li>
<li><a href="https://renanmf.com/sql-tables-and-basic-structure/">SQL: Tables and Basic Structure</a></li>
<li><a href="https://renanmf.com/sql-data-types/">SQL: Data Types</a></li>
<li><a href="https://renanmf.com/sql-syntax/">SQL: Syntax</a></li>
</ul>
<hr />
<p>SQLite is a database that comes pre-installed with Python, you just have to import it just like any other module and start using it.</p>
<p>Another great advantage of SQLite for learning is that it only has 5 types: </p>
<ul>
<li>null: indicates a non-value</li>
<li>integer: represents integer numbers</li>
<li>real: represents floating numbers</li>
<li>text: represents strings</li>
<li>blob: anything that doesn&#8217;t fit in the other types, like images, music files, videos, etc.</li>
</ul>
<p>To start using SQLite, you have to import it with <code>import sqlite3</code>.</p>
<p>Then you open a connection and call the database anything you want, if the database doesn&#8217;t exist yet, it will be created.</p>
<p>We are going to call our database <code>company.db</code>.</p>
<p>For SQLite, the database is just a single file.</p>
<p>After that, you create a <em>cursor</em>, which is the object we are going to call the execute the commands in the database.</p>
<p>Since most commands in SQL are pretty extensive, we will use triple double-quotes for multiline strings, it will help with overall readability.</p>
<p>For the command to be actually executed you have to commit it using <code>commit()</code>.</p>
<p>Finally, when you are done, you <code>close()</code> the connection with the database.</p>
<p>Let&#8217;s see how this works with an example.</p>
<h2>Create Database and Table</h2>
<p>Create a file &#8216;company_database.py&#8217; with the following:</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;&quot;&quot; CREATE TABLE employees (
        name text,
        job_title text,
        salary real,
        hire_date text
    )&quot;&quot;&quot;

cursor.execute(sql)

connection.commit()

connection.close()</code></pre>
<p>This structure covers everything described before.</p>
<p>Import <code>sqlite3</code>, create a database named &#8216;company.db&#8217; to store data about our company, create a cursor from connection, execute the command to create the table &#8217;employees&#8217; with four fields, then commit the transaction and finally, we close the connection.</p>
<p>The four fields are name, job_title, salary, and hire_date. All of them are of the text type, except for salary which has the type real.</p>
<p>We are going to use the structure throughout this article repeatedly.</p>
<p>Execute the code with:</p>
<pre><code>python company_database.py</code></pre>
<p>If no error occurred, you will find a new file named &#8216;company.db&#8217; alongside your program&#8217;s file &#8216;company_database.py&#8217; in the same folder.</p>
<p>We are going to use the same file for every example from now on, keeping the basic structure.</p>
<h2>Insert Data</h2>
<p>In this next piece of code, we are going to &#8216;INSERT&#8217; a record in our &#8217;employees&#8217; tables.</p>
<p>Notice how we have to use a &#8216;?&#8217; to represent every field after the <code>VALUES</code> keyword.</p>
<p>The values are passed in the form of a tuple, notice that the format used for dates follows the pattern &#8216;yyyy-MM-dd HH:mm:ss&#8217;.</p>
<p>When calling <code>execute()</code>, the first argument is the SQL command, and the second is the tuple with the values of the new record.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;&quot;&quot; INSERT INTO employees (
        name,
        job_title,
        salary,
        hire_date
    ) VALUES (?, ?, ?, ?)&quot;&quot;&quot;

values = (&#039;Bob&#039;, &#039;Engineer&#039;, 80000, &#039;2007-09-22 00:00:00&#039;)

cursor.execute(sql, values)

connection.commit()

connection.close()</code></pre>
<p>To insert many records at once, you can create a list of tuples, one tuple for every new record, and instead of calling <code>execute()</code>, you call <code>executemany()</code>.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;&quot;&quot; INSERT INTO employees (
        name,
        job_title,
        salary,
        hire_date
    ) VALUES (?, ?, ?, ?)&quot;&quot;&quot;

values = [
    (&#039;Mary&#039;, &#039;Designer&#039;, 60000, &#039;2012-09-05 00:00:00&#039;),
    (&#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000, &#039;2017-06-21 00:00:00&#039;),
    (&#039;Peter&#039;, &#039;IT Manager&#039;, 95000, &#039;2013-09-05 00:00:00&#039;),
    (&#039;Brian&#039;, &#039;HR Manager&#039;, 92000, &#039;2010-09-05 00:00:00&#039;)
    ]

cursor.executemany(sql, values)

connection.commit()

connection.close()</code></pre>
<h2>Query Data</h2>
<p>Now we have a database, a table, and a few records.</p>
<p>To see the records stored in a table, we use the <code>SELECT</code> command.</p>
<p>We use &#8216;*&#8217; to retrieve all the fields at once.</p>
<p>To bring all the records from the query, we use <code>fetchall()</code>.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;SELECT * FROM employees&quot;

cursor.execute(sql)

print(cursor.fetchall())

connection.commit()

connection.close()</code></pre>
<p>The result will come as a list of tuples, one tuple for each record.</p>
<pre><code>[(&#039;Bob&#039;, &#039;Engineer&#039;, 80000.0, &#039;2007-09-22 00:00:00&#039;), 
(&#039;Mary&#039;, &#039;Designer&#039;, 60000.0, &#039;2012-09-05 00:00:00&#039;), 
(&#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000.0, &#039;2017-06-21 00:00:00&#039;), 
(&#039;Peter&#039;, &#039;IT Manager&#039;, 95000.0, &#039;2013-09-05 00:00:00&#039;), 
(&#039;Brian&#039;, &#039;HR Manager&#039;, 92000.0, &#039;2010-09-05 00:00:00&#039;)]</code></pre>
<p>Every record has a primary, an unique id.</p>
<p>SQLite creates these id&#8217;s automatically and autoincrements them for every new record.</p>
<p>They are implicit and are called &#8216;rowid&#8217; by default.</p>
<p>To bring only some fields in a query, you can specify every field separated by commas.</p>
<p>Here we are using only three fieds: rowid, name, and salary.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;SELECT rowid, name, salary FROM employees&quot;

cursor.execute(sql)

print(cursor.fetchall())

connection.commit()

connection.close()</code></pre>
<p>The output has less information, as requested:</p>
<pre><code>[(1, &#039;Bob&#039;, 80000.0), (2, &#039;Mary&#039;, 60000.0), 
(3, &#039;Sarah&#039;, 98000.0), (4, &#039;Peter&#039;, 95000.0), 
(5, &#039;Brian&#039;, 92000.0)]</code></pre>
<p>Since the result is a tuple, we can use brackets to reference each item in the tuple and format the output while iterating the list of tuples with a <code>for</code> loop.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;SELECT name, salary FROM employees&quot;

cursor.execute(sql)

records = cursor.fetchall()

for record in records:
    print(f&#039;Name: {record[0]}, Salary: {record[1]}&#039;)

connection.commit()

connection.close()</code></pre>
<p>The output will be:</p>
<pre><code>Name: Bob, Salary: 80000.0
Name: Mary, Salary: 60000.0
Name: Sarah, Salary: 98000.0
Name: Peter, Salary: 95000.0
Name: Brian, Salary: 92000.0</code></pre>
<p>The <code>WHERE</code> clause allows you to filter the results of a query.</p>
<p>You can also combine it with operators like <code>&gt;=</code>, <code>&lt;</code>, <code>AND</code>, <code>OR</code>, and others to create more complex filters.</p>
<p>This query lists only employees hired after 2011.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;&quot;&quot;SELECT * FROM employees
        WHERE hire_date &gt; &#039;2011-01-01&#039; &quot;&quot;&quot;

cursor.execute(sql)

records = cursor.fetchall()

for record in records:
    print(record)

connection.commit()

connection.close()</code></pre>
<p>You can see everyone on this list was hired after 2011:</p>
<pre><code>(&#039;Mary&#039;, &#039;Designer&#039;, 60000.0, &#039;2012-09-05 00:00:00&#039;)
(&#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000.0, &#039;2017-06-21 00:00:00&#039;)
(&#039;Peter&#039;, &#039;IT Manager&#039;, 95000.0, &#039;2013-09-05 00:00:00&#039;)</code></pre>
<p>This query lists only employees hired after 2011 and whose salary is below 96,000.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;&quot;&quot;SELECT * FROM employees
        WHERE hire_date &gt; &#039;2011-01-01&#039; 
        AND salary &lt; 96000&quot;&quot;&quot;

cursor.execute(sql)

records = cursor.fetchall()

for record in records:
    print(record)

connection.commit()

connection.close()</code></pre>
<p>You can see Sarah was excluded because her salary is 98,000.</p>
<pre><code>(&#039;Mary&#039;, &#039;Designer&#039;, 60000.0, &#039;2012-09-05 00:00:00&#039;)
(&#039;Peter&#039;, &#039;IT Manager&#039;, 95000.0, &#039;2013-09-05 00:00:00&#039;)</code></pre>
<h2>Update Data</h2>
<p>Now that you know how to query your database, let&#8217;s see how to <code>UPDATE</code> a record.</p>
<p>The general syntax is <code>UPDATE &lt;table name&gt; SET &lt;field&gt; = &lt;new value&gt; WHERE &lt;filter&gt;</code>.</p>
<p>The <code>WHERE</code> clause is actually optional, but get used to always use it to avoid update every single row of your table by mistake, which is very dangerous and may seriously harm your data. This way, your update will only affect the records filtered by the <code>WHERE</code>.</p>
<p>Here we are saying that everyone whose salary is below 90,000 will get an increase of 10,000.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

sql = &quot;&quot;&quot;UPDATE employees SET salary = salary + 10000
            WHERE salary &lt; 90000
     &quot;&quot;&quot;

cursor.execute(sql)

connection.commit()

cursor.execute(&quot;SELECT * FROM employees&quot;)

records = cursor.fetchall()

for record in records:
    print(record)

connection.commit()

connection.close()</code></pre>
<p>Only Bob and Mary had an increase of 10000:</p>
<pre><code>(&#039;Bob&#039;, &#039;Engineer&#039;, 90000.0, &#039;2007-09-22 00:00:00&#039;)
(&#039;Mary&#039;, &#039;Designer&#039;, 70000.0, &#039;2012-09-05 00:00:00&#039;)
(&#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000.0, &#039;2017-06-21 00:00:00&#039;)
(&#039;Peter&#039;, &#039;IT Manager&#039;, 95000.0, &#039;2013-09-05 00:00:00&#039;)
(&#039;Brian&#039;, &#039;HR Manager&#039;, 92000.0, &#039;2010-09-05 00:00:00&#039;)</code></pre>
<h2>Order By</h2>
<p>You can use <code>ORDER BY</code> to choose how to order the results of a query</p>
<p>The default is to sort records by <code>rowid</code> and ascending, i.e, from the smalller to the higher id.</p>
<p>Here we are going to sort by <code>salary</code> descending, that is, from the higher to the smaller salary.</p>
<p>We are also to list <code>rowid</code> explicitly to see how the result is ordered by the <code>salary</code> column, not the id.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

cursor.execute(&quot;SELECT rowid, * FROM employees ORDER BY salary DESC&quot;)

records = cursor.fetchall()

for record in records:
    print(record)

connection.commit()

connection.close()</code></pre>
<p>You can easily see Sarah makes the most with 98,000.</p>
<pre><code>(3, &#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000.0, &#039;2017-06-21 00:00:00&#039;)
(4, &#039;Peter&#039;, &#039;IT Manager&#039;, 95000.0, &#039;2013-09-05 00:00:00&#039;)
(5, &#039;Brian&#039;, &#039;HR Manager&#039;, 92000.0, &#039;2010-09-05 00:00:00&#039;)
(1, &#039;Bob&#039;, &#039;Engineer&#039;, 90000.0, &#039;2007-09-22 00:00:00&#039;)
(2, &#039;Mary&#039;, &#039;Designer&#039;, 70000.0, &#039;2012-09-05 00:00:00&#039;)</code></pre>
<h2>Limit</h2>
<p>We have a very small database, but it is not uncommon to have thousands of records, and sometimes you just want to see a few of them to check the structure of the data.</p>
<p>Use <code>LIMIT</code> followed by the number of records you want.</p>
<p>In this case, we are just listing the first 3.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

cursor.execute(&quot;SELECT rowid, * FROM employees LIMIT 3&quot;)

records = cursor.fetchall()

for record in records:
    print(record)

connection.commit()

connection.close()</code></pre>
<p>You can see that records 4 and 5 were not included.</p>
<pre><code>(1, &#039;Bob&#039;, &#039;Engineer&#039;, 90000.0, &#039;2007-09-22 00:00:00&#039;)
(2, &#039;Mary&#039;, &#039;Designer&#039;, 70000.0, &#039;2012-09-05 00:00:00&#039;)
(3, &#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000.0, &#039;2017-06-21 00:00:00&#039;)
</code></pre>
<h2>Delete</h2>
<p>To delete a record, the general syntax is <code>DELETE FROM &lt;table name&gt; WHERE &lt;filter&gt;</code>.</p>
<p>The <code>WHERE</code> clause is actually optional, but get used to always use it to avoid deleting records by mistake, which is very dangerous and may seriously harm your data. This way, your <code>DELETE</code> will only affect the records filtered by the <code>WHERE</code>.</p>
<p>Here we are deleting the record with Id 1, which is Bob.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

cursor.execute(&quot;&quot;&quot;DELETE FROM employees 
        WHERE rowid = 1
     &quot;&quot;&quot;)

connection.commit()

cursor.execute(&quot;SELECT rowid,* FROM employees&quot;)

records = cursor.fetchall()

for record in records:
    print(record)

connection.commit()

connection.close()</code></pre>
<p>You can see Bob was deleted.</p>
<pre><code>(2, &#039;Mary&#039;, &#039;Designer&#039;, 70000.0, &#039;2012-09-05 00:00:00&#039;)
(3, &#039;Sarah&#039;, &#039;Sales Manager&#039;, 98000.0, &#039;2017-06-21 00:00:00&#039;)
(4, &#039;Peter&#039;, &#039;IT Manager&#039;, 95000.0, &#039;2013-09-05 00:00:00&#039;)
(5, &#039;Brian&#039;, &#039;HR Manager&#039;, 92000.0, &#039;2010-09-05 00:00:00&#039;)</code></pre>
<h2>Drop Table</h2>
<p>To delete a whole table, use the <code>DROP TABLE</code> command indicating the name of the table you want to drop.</p>
<pre><code>import sqlite3

connection = sqlite3.connect(&#039;company.db&#039;)

cursor = connection.cursor() 

cursor.execute(&quot;DROP TABLE employees&quot;)

connection.commit()

connection.close()</code></pre>
<h2>Other Databases</h2>
<p><a href="https://renanmf.com/how-to-connect-with-mysql-database-python/">How to connect to a MySQL database in Python</a></p>
<p><a href="https://renanmf.com/how-to-connect-to-a-postgresql-database-in-python/">How to connect to a PostgreSQL database in Python</a></p>
<p>The content <a href="https://renanmf.com/sqlite-python/">SQLite in Python</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL: Syntax</title>
		<link>https://renanmf.com/sql-syntax/</link>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Mon, 22 Jun 2020 10:53:00 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=1784</guid>

					<description><![CDATA[<p>SQL is not case sensitive, so you can use the keywords in uppercase like SELECT or lowercase like select, both work the same. For the sake of clarity and readability, it is advised to use the reserved keywords in uppercase, while the referenced tables and fields are written in lowercase. Some databases do not require [&#8230;]</p>
<p>The content <a href="https://renanmf.com/sql-syntax/">SQL: Syntax</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>SQL is <strong>not</strong> case sensitive, so you can use the keywords in uppercase like <code>SELECT</code> or lowercase like <code>select</code>, both work the same.</p>
<p>For the sake of clarity and readability, it is advised to use the reserved keywords in uppercase, while the referenced tables and fields are written in lowercase.</p>
<p>Some databases do not require semicolon <code>;</code> at the end of a statement, but as a general rule, always use it.</p>
<p>For instance, a <code>SELECT</code> statement has the following general look based on what was specified before:</p>
<pre><code class="language-sql">SELECT column_name FROM table_name;</code></pre>
<p>Notice that you can easily identify that <code>SELECT</code> and <code>FROM</code> are reserved keywords in SQL, while <code>column_name</code> and <code>table_name</code> reference a field and a table respectively.</p>
<p>The most fundamental commands are:</p>
<ul>
<li><code>SELECT</code>: reads/extracts the data</li>
<li><code>INSERT</code>: inserts new data</li>
<li><code>UPDATE</code>: updates the existing data</li>
<li><code>DELETE</code>: removes the data</li>
</ul>
<p>You will see them commonly referred to as CRUD, an acronym for Create, Read, Update, and Delete.</p>
<p>Together they form the basis of any program that needs to interact with a database.</p>
<p>The content <a href="https://renanmf.com/sql-syntax/">SQL: Syntax</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL: Data Types</title>
		<link>https://renanmf.com/sql-data-types/</link>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Wed, 17 Jun 2020 11:23:48 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=1768</guid>

					<description><![CDATA[<p>Data Types Each field in a Table has a Type. A Data Type is used to specify what kind of object that particular field will store. When creating your own structures, using the right type for the right data is essential to prevent data loss and to work better with the information you have. It [&#8230;]</p>
<p>The content <a href="https://renanmf.com/sql-data-types/">SQL: Data Types</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Data Types</h1>
<p>Each field in a Table has a Type.</p>
<p>A Data Type is used to specify what kind of object that particular field will store.</p>
<p>When creating your own structures, using the right type for the right data is essential to prevent data loss and to work better with the information you have.</p>
<p>It is important to note that each database has different types.</p>
<p>They might be similar, but they are not exactly the same and you should be aware of this when dealing with different products like Oracle or MySQL, for instance.</p>
<p>I will list PostgreSQL Data Types in this article to give you an understanding of the main types.</p>
<p>If you are working with other databases, just check the documentation of each one and you should see lots of similarities to among them.</p>
<p>A few popular examples are:</p>
<ul>
<li><a href="https://dev.mysql.com/doc/refman/8.0/en/data-types.html">MysSQL Data Types</a></li>
<li><a href="https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15">SQL Server Data Types</a></li>
<li><a href="https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT012">Oracle Data Types</a></li>
</ul>
<p>Let&#8217;s learn about PostgreSQL Data Types.</p>
<h2>Data Types in PostgreSQL</h2>
<p>If you check the oficial documentation for <a href="https://www.postgresql.org/docs/9.5/datatype.html">PostgreSQL Data Types</a>, you will see some very specific types for very specific cases.</p>
<p>Let&#8217;s keep things simple and use some Pareto&#8217;s 80/20 to show you the types you are going to use most of the time, they are:</p>
<ul>
<li>Numeric Types: integer and floating-point</li>
<li>Character types: char, varchar and text</li>
<li>Boolean</li>
<li>Date/Time Types: date, time, timestamp, and interval</li>
</ul>
<p>These types are very generic and you can use them to store numbers, text, and dates, which is enough for most use cases.</p>
<h2>Numeric Types</h2>
<p>You can work with either integers of float-pointing numbers.</p>
<h3>Integer</h3>
<p>For integers you can work with:</p>
<ul>
<li>smallint: it stores 2 bytes and can store values in the range of -32768 to +32767.</li>
<li>integer: it stores 4 bytes and can store values in the range of -2147483648 to +2147483647</li>
<li>bigint: it can store 8 bytes and can store values in the range of -9223372036854775808 to +9223372036854775807</li>
</ul>
<p>All of them have their <code>serial</code> counterparts, which is a specific type that automatically increments for every new row, it is very common for ID columns where you store values sequentially for each new record like 1, 2, 3, 4, 5 and so on.</p>
<p>For instance, the serial integer is:</p>
<ul>
<li>serial: stores 4 bytes and ranges from 1 to 2147483647</li>
</ul>
<p>Notice its range starts on 1, not on a negative number since it doesn&#8217;t make much sense to have and ID of, say, -20 for an employee, for instance.</p>
<h3>Floating-point numbers</h3>
<p>For floating-point numbers, in general you can choose to use <code>numeric</code> for most cases.</p>
<p>It supports a very large number of digits and it is easy to work with when you need to deal with monetary values or simply needs a good precision.</p>
<h2>Character types</h2>
<p>These types are used to store strings.</p>
<ul>
<li>varchar: can store a string of the size you pre-define, and it does not pad spaces if the string to be stored is shorter than the declared length</li>
<li>char: can store a string of the size you pre-define, but it pads spaces if the string to be stored is shorter than the declared length</li>
<li>text: can store a string of any size, no need to predefine the number of characters</li>
</ul>
<h2>Boolean</h2>
<p>The boolean type is by far the most simple one.</p>
<p>It stores 1 byte that represents <code>true</code>, <code>false</code> or <code>null</code> value for when you have an unknown state.</p>
<p>If you insert either of these in you database, PostgreSQL will understand them as <code>true</code>: true, yes, on, and 1.</p>
<p>If you insert either of these in you database, PostgreSQL will understand them as <code>false</code>: false, no, off, and 0.</p>
<h2>Date/Time</h2>
<p>You can basically store time, date, date and time simultaneously, and data/time with time zone:</p>
<ul>
<li>timestamp: you can store the date and the time</li>
<li>date: you can store only the date</li>
<li>time: you can store only the time</li>
<li>timestamptz: is the same as timestamp with the addition of time zone</li>
<li>interval: you can store a period of time, a time interval</li>
</ul>
<p>The content <a href="https://renanmf.com/sql-data-types/">SQL: Data Types</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL: Tables and Basic Structure</title>
		<link>https://renanmf.com/sql-tables-and-basic-structure/</link>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Wed, 10 Jun 2020 11:11:55 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=1664</guid>

					<description><![CDATA[<p>This is a direct continuation of my previous article Introduction to SQL. SQL works on a structure with four components: Table, Field, Row, and Column. You can think of those components exactly like the ones in spreadsheets like Excel. A database can have one or more tables in it. Table A table is a very [&#8230;]</p>
<p>The content <a href="https://renanmf.com/sql-tables-and-basic-structure/">SQL: Tables and Basic Structure</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This is a direct continuation of my previous article <a href="https://renanmf.com/introduction-to-sql/">Introduction to SQL</a>.</p>
<p>SQL works on a structure with four components: Table, Field, Row, and Column.</p>
<p>You can think of those components exactly like the ones in spreadsheets like Excel.</p>
<p>A database can have one or more tables in it.</p>
<h2>Table</h2>
<p>A table is a very common and simple way to organize data.</p>
<p>You have columns describe the kind of information and rows/records consisting of the information itself.</p>
<p>The following two tables Employees and Departments are the ones I will use to demonstrate the capabilities of SQL and how to manage data.</p>
<p>The Employees table contains data about the employees of a certain company.</p>
<p><strong>Employees</strong></p>
<pre><code>| employee_id | name  |   job_title   | salary | hire_date  | department_id |
|-------------|-------|---------------|--------|------------|---------------|
| 1           | Bob   | Engineer      | 80000  | 2015-04-12 | 2             |
| 2           | Mary  | Designer      | 60000  | 2017-06-21 | 2             |
| 3           | Sarah | Sales Manager | 98000  | 2013-09-05 | 1             |
| 4           | Peter | IT Manager    | 95000  | 2010-09-05 | 2             |
| 5           | Brian | HR Manager    | 92000  | 2012-09-05 | 3             |</code></pre>
<p>The Departments table describes de departments the company has.</p>
<p><strong>Departments</strong></p>
<pre><code>| department_id | department_name | city      | country | manager_id |
|---------------|-----------------|-----------|---------|------------|
| 1             | Sales           | São Paulo | Brazil  | 3          |
| 2             | IT              | Barcelona | Spain   | 4          |
| 3             | Human Resources | New York  | USA     | 5          |</code></pre>
<h2>Field</h2>
<p>A field specifies the kind of information a particular column has and its type.</p>
<p>The Employees table has the following fields:</p>
<ul>
<li>employee_id: de unique ID that identifies an employee</li>
<li>name: the name of the employee</li>
<li>job_title: the formal title the employee has inside the company</li>
<li>salary: how much the employee makes yearly</li>
<li>hire_date: when the employee was hired</li>
<li>department_id: the unique ID of the department where the employee works</li>
</ul>
<p>The Departments table has the following fields:</p>
<ul>
<li>department_id: the unique ID for the Department</li>
<li>department_name: the name of the Department</li>
<li>city: the city where the Department is located</li>
<li>country: the country where the Department is located</li>
<li>manager_id: the ID of the manager responsible for the department, the manager is an employee</li>
</ul>
<h2>Row</h2>
<p>A row is a record of data inside the Table.</p>
<p>The Employee table has 5 rows.</p>
<p>The first row is Bob\&#8217;s record.</p>
<p>Notice the row with the identifications of the fields does not count as &quot;first&quot; row, the counting begins with the first record.</p>
<pre><code>| employee_id | name  |   job_title   | salary | hire_date  | department_id |
|-------------|-------|---------------|--------|------------|---------------|
| 1           | Bob   | Engineer      | 80000  | 2015-04-12 | 2             |</code></pre>
<h2>Column</h2>
<p>A column is a field and all of its information.</p>
<p>The column <code>employee_id</code> in the Employees table has all the unique IDs of all the employees.</p>
<pre><code>| employee_id |
|-------------|
| 1           |
| 2           |
| 3           |
| 4           |
| 5           |</code></pre>
<p>The content <a href="https://renanmf.com/sql-tables-and-basic-structure/">SQL: Tables and Basic Structure</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Introduction to SQL</title>
		<link>https://renanmf.com/introduction-to-sql/</link>
					<comments>https://renanmf.com/introduction-to-sql/#comments</comments>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Mon, 08 Jun 2020 12:38:18 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=1696</guid>

					<description><![CDATA[<p>It doesn&#8217;t matter if you are a frontend, backend, or a full stack developer, knowing SQL is a must have skill. What is SQL and why you should learn it SQL stands for Structured Query Language. It is pronounced SEQUEL. SQL is a language designed to handle databases. It allows you to manage and access [&#8230;]</p>
<p>The content <a href="https://renanmf.com/introduction-to-sql/">Introduction to SQL</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>It doesn&#8217;t matter if you are a frontend, backend, or a full stack developer, knowing SQL is a must have skill.</p>
<h2>What is SQL and why you should learn it</h2>
<p>SQL stands for Structured Query Language.</p>
<p>It is pronounced <em>SEQUEL</em>.</p>
<p>SQL is a language designed to handle databases.</p>
<p>It allows you to manage and access data in a relational database using a standard approach.</p>
<p>Relational Database Management Systems or simply RDMS are the industry standard for storing data.</p>
<p>RDMS store data in tables, much like the ones you make in your Excel spreadsheets with columns and rows, each column is called a field and each row is a data entry called a record.</p>
<p>Some of the most common RDMS are PostgreSQL, MySQL, Oracle, and SQL Sever.</p>
<p>There are also NoSQL Databases like Mongo that store data in different ways such as documents, they are growing in use year after year, but are still far from becoming as widely used as relational ones.</p>
<h2>SQL Usage</h2>
<p>SQL is used in a variety of ways to manage your application&#8217;s data, such as to:</p>
<ul>
<li>Retrieve, Update, Insert and Delete data</li>
<li>Create and Delete databases and tables</li>
<li>Manage permissions to certain data, so some users can access it and others don&#8217;t</li>
<li>Guarantee the integrity of the data</li>
<li>Automate actions in the database according to some change in the data</li>
</ul>
<h2>SQL Dialects</h2>
<p>Be aware that there are some variations of SQL depending on the RDMS you are using that make SQL even more powerful for those specific systems.</p>
<p>After learning the standard SQL, it is common for some people to specialize in a particular RDMS and its specific dialect like Oracle&#8217;s PL/SQL or PostgreSQL&#8217;s PL/pgSQL.</p>
<p>It is also common to see people referring to standard SQL as ANSI SQL to differentiate it from the specific dialects.</p>
<p>ANSI stands for American National Standards Institute, the institute standardized SQL in 1986.</p>
<p>The content <a href="https://renanmf.com/introduction-to-sql/">Introduction to SQL</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://renanmf.com/introduction-to-sql/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
