<?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 Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/sql/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Thu, 11 Dec 2025 13:13:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQLite3: Python and SQL</title>
		<link>https://creatronix.de/sqlite3-python-and-sql/</link>
					<comments>https://creatronix.de/sqlite3-python-and-sql/#respond</comments>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 02 Apr 2020 08:57:45 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1323</guid>

					<description><![CDATA[<p>Everything we did in the last articles of the series SQL-Tutorial was a dry run because we just used SQLFiddle. So let&#8217;s start with a real database like SQLite. SQLite is a file based DBRMS and can be used for e.g. web sites. The official docs say: &#8220;SQLite works great as the database engine for&#8230;</p>
<p>The post <a href="https://creatronix.de/sqlite3-python-and-sql/">SQLite3: Python and SQL</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Everything we did in the last articles of the series <a href="https://creatronix.de/sql-tutorial/">SQL-Tutorial</a> was a dry run because we just used SQLFiddle.</p>
<p>So let&#8217;s start with a real database like SQLite.</p>
<p>SQLite is a file based DBRMS and can be used for e.g. web sites. The official docs say:</p>
<p><em>&#8220;SQLite works great as the database engine for most low to medium traffic websites (which is to say, most websites). [..] Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite.&#8221;</em></p>
<p>Because Knight Industries is not Google, Amazon nor Facebook we can definitely use SQLite.</p>
<h2>Creating and connecting to a database</h2>
<p>In Python it is pretty easy to connect to a SQLite database:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>from sqlite3 import connect 
db_connection = connect('knight_industries.db')</code></pre>
</div>
<p>If the file knight_industries.db does not exist, it will be created automagically. A nice little feature of the sqlite3 library.</p>
<p>But be careful: If You already have a database file and you mess up the path in the connect statement you will wonder why you cannot access your data, because a new file is created silently.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>cursor = db_connection.cursor()
cursor.execute('''CREATE TABLE operatives (id INTEGER, name TEXT, birthday DATE)''')
cursor.execute('''INSERT INTO operatives (id, name, birthday) \
                  VALUES (1, "Michael Arthur Long", "1949-01-09")''')

db_connection.commit()
cursor.execute('''SELECT * FROM operatives''')
print cursor.fetchone()
db_connection.close()</code></pre>
</div>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/sqlite3-python-and-sql/">SQLite3: Python and SQL</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://creatronix.de/sqlite3-python-and-sql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL-LIKE and wildcards</title>
		<link>https://creatronix.de/sql-like-and-wildcards/</link>
					<comments>https://creatronix.de/sql-like-and-wildcards/#respond</comments>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 18 Oct 2019 15:51:12 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[wildcards]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1367</guid>

					<description><![CDATA[<p>This article is part of my SQL-Tutorial Motivation Sometimes you need to retrieve data from a database without knowing the exact string to look up so wildcards for the rescue Underscore Se_en represents a pattern with a wildcard character. The _ means you can substitute any individual character here without breaking the pattern. It matches&#8230;</p>
<p>The post <a href="https://creatronix.de/sql-like-and-wildcards/">SQL-LIKE and wildcards</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This article is part of my <a href="https://creatronix.de/sql-tutorial/">SQL-Tutorial</a></p>
<h3>Motivation</h3>
<p>Sometimes you need to retrieve data from a database without knowing the exact string to look up</p>
<p>so wildcards for the rescue</p>
<h3>Underscore</h3>
<p><code>Se_en</code> represents a pattern with a <em>wildcard</em> character.</p>
<p>The <code>_</code> means you can substitute any individual character here without breaking the pattern.</p>
<p>It matches exactly one character.</p>
<p>The names <code>Seven</code> and <code>Se7en</code> both match this pattern.</p>
<h3>Percentage sign</h3>
<p>The percentage sign <code>%</code> is another wildcard character that can be used with <code>LIKE</code>.</p>
<p>It matches one or more characters</p>
<p>The post <a href="https://creatronix.de/sql-like-and-wildcards/">SQL-LIKE and wildcards</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://creatronix.de/sql-like-and-wildcards/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL-Basics: Relations</title>
		<link>https://creatronix.de/sql-basics-relations/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 18 Apr 2019 08:02:28 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[constraint]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1326</guid>

					<description><![CDATA[<p>As the Junior Data Scientist of Knight Industries we created a table to keep track of all our operatives: SQL-Basics: Create, Read, Update, Delete Devon asks us to keep track of our operations aka missions as well. For the first implementation let us assume that a mission has one operative and an operative can participate&#8230;</p>
<p>The post <a href="https://creatronix.de/sql-basics-relations/">SQL-Basics: Relations</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>As the Junior Data Scientist of Knight Industries we created a table to keep track of all our operatives: <a href="https://creatronix.de/sql-basics-create-read-update-delete/">SQL-Basics: Create, Read, Update, Delete</a></p>
<p>Devon asks us to keep track of our operations aka missions as well. For the first implementation let us assume that a mission has one operative and an operative can participate in multiple missions. that&#8217;s what we call an 1 to many relationship.</p>
<pre>  +------------+                 +----------+
  |            |1   active in   n|          |
  | operative  +-----------------+ missions |
  |            |                 |          |
  +------------+                 +----------+
</pre>
<p>Missions have an id, a code name like &#8220;Phantom Liberty&#8221;, an operative id and a total cost.</p>
<p>To say that the operative id is equivalent to the id in the operative column we use the FOREIGN KEY constraint.</p>
<p>So let&#8217;s create the new table in our database:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>CREATE TABLE ki_missions 
(id INTEGER, code_name TEXT, operative_id INTEGER, total_cost DECIMAL(15,2), PRIMARY KEY(id), FOREIGN KEY (operative_id) REFERENCES operatives(id));
INSERT INTO ki_missions VALUES (1, "Knight of the Phoenix", 1, 6246382.43);
INSERT INTO ki_missions VALUES (2, "Deadly Maneuvers", 1, 4568893.53);</code></pre>
</div>
<p>Our first join</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT ki_missions.code_name, ki_operatives.name FROM ki_missions LEFT JOIN ki_operatives ON ki_missions.operative_id = ki_operatives.id;</code></pre>
</div>
<p>More on joins <a href="https://dsin.files.wordpress.com/2013/03/sqljoins_cheatsheet.png">https://dsin.files.wordpress.com/2013/03/sqljoins_cheatsheet.png</a></p>
<p>Next chapter: <a href="https://creatronix.de/sql-functions/">SQL-Functions &#8211; SQL-Basics 3</a></p>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/sql-basics-relations/">SQL-Basics: Relations</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL-Basics: Create &#8211; Read &#8211; Update &#8211; Delete</title>
		<link>https://creatronix.de/sql-basics-create-read-update-delete/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 09 Feb 2018 15:35:23 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[knight rider]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[statements]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1300</guid>

					<description><![CDATA[<p>This episode is about the basic statements needed to create, read, update and delete data in a database system. It is part of my SQL-Tutorial Motivation Let&#8217;s assume we work as a data scientist for Knight Industries.  We want to help the Foundation of Law and Government to keep track of our operatives. We decide&#8230;</p>
<p>The post <a href="https://creatronix.de/sql-basics-create-read-update-delete/">SQL-Basics: Create &#8211; Read &#8211; Update &#8211; Delete</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This episode is about the basic statements needed to create, read, update and delete data in a database system. It is part of my <a href="https://creatronix.de/sql-tutorial/">SQL-Tutorial</a></p>
<h2>Motivation</h2>
<p>Let&#8217;s assume we work as a data scientist for Knight Industries.  We want to help the Foundation of Law and Government to keep track of our operatives.</p>
<p>We decide to use a classic relational database management system or RDBMS. In order to explore Database Management Systems we can either install one locally or we can use an online tool like <a href="http://www.sqlfiddle.com/">SQLFiddle.</a></p>
<p>To interact with RDBMS we use SQL &#8211; the Structured Query Language.</p>
<p>As the name says SQL (speak either S-Q-L or Sequel) is used to write structured queries. Think of &#8220;conversations&#8221; when You think of &#8220;queries&#8221;.</p>
<p>So, let&#8217;s fire up SQLFiddle.</p>
<h2>Creating the table</h2>
<p>At first we have an empty database, so we create a table named <strong>operatives</strong> with the following columns: id, name and birthday</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>CREATE TABLE ki_operatives (id INTEGER, name TEXT, birthday DATE, PRIMARY KEY(id));</code></pre>
</div>
<p>Type that into the left column of the SQLfiddle window and press &#8220;Build Scheme&#8221;.  Everything green? If not, please check for typos. Please also make sure that MySQL 5.6 is selected in the menu bar.</p>
<p>So we learned our first SQL statement: CREATE TABLE. It does exactly what its name suggest: creating a table in a database.</p>
<p>In SQL it does not matter if you write the statements in all lower or upper case, we could have written as well: &#8220;create table&#8221;, but as good practice let&#8217;s write SQL keywords always in all uppercase letters.</p>
<p>INTEGER, TEXT and DATE are built-in data types of SQL. PRIMARY KEY (id) tells the DBRMS that the id column is a special one: it may not contain duplicates and must always have a value assigned.</p>
<p>With that kind of unique identifier we can always reference an entry in a table.</p>
<p>To check if the table was created we can type</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SHOW TABLES;</code></pre>
</div>
<p>into the right column and press &#8220;Run SQL&#8221;.</p>
<p>All we have by now is an empty table. We can check that by typing the following statement into the right column as well and press &#8220;Run SQL&#8221; again.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT * FROM ki_operatives;</code></pre>
</div>
<p>The Output should read like &#8220;Record Count: 0;&#8221;</p>
<p>SELECT is SQLs way of getting data out of the database. The * means &#8220;all columns&#8221;, so you don&#8217;t need to know all the column names of a table beforehand.</p>
<h2>Adding some data</h2>
<p>As most important operative we add Michael Arthur Long to our operatives table:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>INSERT INTO ki_operatives (id, name, birthday) VALUES (1, "Michael Arthur Long", "1949-01-09");</code></pre>
</div>
<p>Good practice: Although You could insert the first record into the database without repeating the column names it is considered a good practice to state the names of the columns you want to insert your data into.</p>
<p>We add three more of our employees to the operatives table.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>INSERT INTO ki_operatives (id, name, birthday)VALUES 
(2, "Devon Miles", "1942-07-12");
INSERT INTO ki_operatives (id, name, birthday)VALUES 
(3, "Dr. Bonnie Barstow", "1954-11-24");
INSERT INTO ki_operatives (id, name, birthday)VALUES 
(4, "Reginald Cornelius III", "1952-05-04");</code></pre>
</div>
<p>&nbsp;</p>
<h2>Times they are a changing</h2>
<p>Michael Arthur Long gets his new name: Michael Knight. We update his record in our database.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>UPDATE ki_operatives SET name = "Michael Knight" WHERE id = 1;</code></pre>
</div>
<p>The Update keyword does again exactly what its name suggests: updating a database record. With SET You select a specific column and the WHERE clause specifies the row of the table.</p>
<p>We want to keep track of the different occupations of our team members and add a new column to operatives table.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>ALTER TABLE ki_operatives ADD COLUMN occupation TEXT;</code></pre>
</div>
<p>With the ALTER TABLE clause we can add or delete columns or change the data type of a column as well.</p>
<p>Let&#8217;s update the occupation of all operatives:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>UPDATE ki_operatives SET occupation = "Field Agent" WHERE id = 1; 
UPDATE ki_operatives SET occupation = "Head of Operations" WHERE id = 2; 
UPDATE ki_operatives SET occupation = "Technician" WHERE id = 3; 
UPDATE ki_operatives SET occupation = "Truck Driver" WHERE id = 4;</code></pre>
</div>
<p>Bonnie left in 1983 to pursue her graduate studies. We remove her from our operatives database.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>DELETE FROM ki_operatives WHERE id = 3;</code></pre>
</div>
<p>DELETE is the clause for -you guessed it- deleting records from the database. Be careful: Don&#8217;t forget the WHERE clause or you delete all columns from the table at once!</p>
<p>Because FLAG needs a lead technician, we add April Curtis as Head of Design Team.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>INSERT INTO ki_operatives VALUES (5, "April Curtis", "1952-05-04", "Senior Design Officer");</code></pre>
</div>
<p>To evaluate each step of our implementation we can always run</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT * FROM operatives;</code></pre>
</div>
<p>in the right window by clicking &#8220;Run SQL&#8221;</p>
<p>Today we learned</p>
<ul>
<li>creating a table</li>
<li>inserting some data into it</li>
<li>reading data from the table</li>
<li>adding more columns</li>
<li>deleting data</li>
</ul>
<p>In the next article we will deal with the &#8220;relational&#8221; in the &#8220;relational database management system&#8221;.</p>
<p>For a relation we need at least to parties / tables that know each other.</p>
<p>Read the next chapter:  <a href="https://creatronix.de/sql-basics-relations/">SQL-Basics: Relations</a></p>
<p>The post <a href="https://creatronix.de/sql-basics-create-read-update-delete/">SQL-Basics: Create &#8211; Read &#8211; Update &#8211; Delete</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL-Basics &#8211; Subqueries: Update column with values from another column</title>
		<link>https://creatronix.de/sql-subqueries-update-column-with-values-from-another-column/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Wed, 07 Feb 2018 15:38:42 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[normalization]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[subqueries]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1278</guid>

					<description><![CDATA[<p>Sometimes You screw up your database design and you have redundancies i.e. your database is not normalized. If You want to correct that: Subqueries for the rescue! In our example we have two tables which contain almost the same information: CREATE TABLE installed_device (`id` int, `device` text, `info` text); INSERT INTO installed_device (`id`, `device`, `info`)&#8230;</p>
<p>The post <a href="https://creatronix.de/sql-subqueries-update-column-with-values-from-another-column/">SQL-Basics &#8211; Subqueries: Update column with values from another column</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Sometimes You screw up your database design and you have redundancies i.e. your database is not normalized. If You want to correct that: Subqueries for the rescue!</p>
<p>In our example we have two tables which contain almost the same information:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>CREATE TABLE installed_device (`id` int, `device` text, `info` text);
INSERT INTO installed_device (`id`, `device`, `info`) VALUES (1, "Device_1", "Works!"), (2, "Device_2", "Doesn't work!");
CREATE TABLE device (`id` int, `device_name` text);
INSERT INTO device (`id`, `device_name`) VALUES  (1, "Device_1"), (2, "Device_2");</code></pre>
</div>
<p>&nbsp;</p>
<p>installed_device</p>
<table>
<tbody>
<tr>
<th>id</th>
<th>device</th>
<th>info</th>
</tr>
<tr>
<td>1</td>
<td>Device_1</td>
<td>Works!</td>
</tr>
<tr>
<td>2</td>
<td>Device_2</td>
<td>Doesn&#8217;t work!</td>
</tr>
</tbody>
</table>
<p>device</p>
<table>
<tbody>
<tr>
<th>id</th>
<th>device_name</th>
</tr>
<tr>
<td>1</td>
<td>Device_1</td>
</tr>
<tr>
<td>2</td>
<td>Device_2</td>
</tr>
</tbody>
</table>
<p>The goal is to replace the values in installed_device&#8217;s device column with the id from table device:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-sql" data-lang="SQL"><code>UPDATE installed_device SET device = (SELECT id FROM device WHERE device.device_name = installed_device.device);</code></pre>
</div>
<p>The Result</p>
<table>
<tbody>
<tr>
<th>id</th>
<th>device</th>
<th>info</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>Works!</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>Doesn&#8217;t work!</td>
</tr>
</tbody>
</table>
<p>The post <a href="https://creatronix.de/sql-subqueries-update-column-with-values-from-another-column/">SQL-Basics &#8211; Subqueries: Update column with values from another column</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
