<?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>statements Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/statements/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/statements/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Sat, 05 Nov 2022 08:12:50 +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>
	<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>
	</channel>
</rss>
