{"id":252,"date":"2024-02-29T15:50:04","date_gmt":"2024-02-29T21:50:04","guid":{"rendered":"https:\/\/www.baizhao666.com\/?p=252"},"modified":"2024-07-17T18:42:11","modified_gmt":"2024-07-18T00:42:11","slug":"sql","status":"publish","type":"post","link":"https:\/\/www.baizhao666.com\/?p=252","title":{"rendered":"SQL"},"content":{"rendered":"\n<p>SQL stands for &#8220;structured query language&#8221;. It is a programming language for storing and processing information in a relational database. A relational database stores information in tabular form, with rows and columns representing different data attributes and the various relationships between the data values.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>SQL focus on database management. It is very big right now in the private sector, lots and lots of companies (especially the big companies) need to handle very large and numerous and complicated database, and hence a lot of job openings are specifically for people with experience using SQL. In python, there is a module that allows us to make use of c-basic SQL commands and features called &#8220;sqlite3&#8221;.<\/p>\n\n\n\n<p>SQL files are generally stored with the &#8220;.db&#8221; extension (db = database). The SQL files are binary files, if we open these files in textbook, we will see a bunch of gibberish codes instead of the actual data. In SQL, a &#8220;database&#8221; may have multiple &#8220;Tables&#8221;, and a &#8220;Table&#8221; may have multiple columns, and columns can have multiple data entries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a database<\/h3>\n\n\n\n<p>In order to create a database, we will use the <a href=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2024\/01\/clorox2020.csv\" data-type=\"attachment\" data-id=\"249\">clorox2020.csv<\/a> and turn it into a database so that we can run SQL commands on it.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def data_to_database():\n    data = pandas.read_csv(&quot;clorox2020.csv&quot;)\n\n    # here we establish a connection to the database\n    conn = sqlite3.connect(&quot;clorox2020&quot;)\n    # once we have established a connection, we need to create a cursor object\n    # the cursor object is the object that we use to execute SQL commands\n    cur = conn.cursor()\n\n    # here we create a table called stock\n    sql_command = &quot;CREATE TABLE clorox (Date text, Open float, High float, Low float, Close float, AdjClose float, Volume int)&quot;\n    cur.execute(sql_command)\n\n    for i in range(len(data)):\n        # insert one row of data into the table per loop\n        Date = data[&quot;Date&quot;][i]\n        Date = &quot;&#39;&quot; + Date + &quot;&#39;&quot;\n        Open = data[&quot;Open&quot;][i]\n        High = data[&quot;High&quot;][i]\n        Low = data[&quot;Low&quot;][i]\n        Close = data[&quot;Close&quot;][i]\n        AdjClose = data[&quot;Adj Close&quot;][i]\n        Volume = data[&quot;Volume&quot;][i]\n\n        sql_command = f&quot;INSERT INTO clorox VALUES ({Date}, {Open}, {High}, {Low}, {Close}, {AdjClose}, {Volume})&quot;\n        cur.execute(sql_command)\n\n    # save all the changes and close the database\n    conn.commit()\n    conn.close()\n\n\ndef display_data():\n    conn = sqlite3.connect(&quot;clorox2020&quot;)\n    cur = conn.cursor()\n    sql_command = &quot;SELECT * FROM clorox&quot;\n    db_columns = cur.execute(sql_command)\n    db_entries = db_columns.fetchall()\n    for entry in db_entries:\n        print(entry)\n\n    conn.close()\n\n\ndata_to_database()\ndisplay_data()<\/code><\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">('2020-01-02', 153.380005, 153.410004, 151.550003, 152.610001, 149.262665, 1292500)\n('2020-01-03', 152.570007, 153.880005, 151.240005, 152.910004, 149.556076, 809800)\n('2020-01-06', 153.070007, 153.940002, 152.289993, 153.369995, 150.005981, 869300)\n('2020-01-07', 153.190002, 153.289993, 151.509995, 151.520004, 148.196564, 1198200)\n('2020-01-08', 151.779999, 153.339996, 150.949997, 151.970001, 148.636688, 901200)\n('2020-01-09', 152.229996, 153.009995, 151.720001, 152.800003, 149.448486, 504900)\n......<\/pre>\n\n\n\n<p>Notice that in order to interact with a database, we need to make a connection to that database using conncetion() method. And we need to use cursor() method to create a cursor object to execute some SQL commands. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common SQL commands<\/h3>\n\n\n\n<p>Most of the actions we need to perform on a database are done with SQL statements. SQL statements consists of keywords that are easy to understand. SQL commands are all capitalized (although not necessary) to distinguish them from the Python commands or variable names. The most common operations on a database are CRUD: create, read, update, and delete.<\/p>\n\n\n\n<p>1. Create<\/p>\n\n\n\n<p>Syntax: CREATE TABLE &lt;table name&gt; (&lt;cloumn1&gt; &lt;datatype&gt;, &lt;cloumn2&gt; &lt;datatype&gt;, &#8230;)<\/p>\n\n\n\n<p>In this command, neither the table name nor column name needs to covered by quotation marks. For the datatype, in SQLite text is for string, float is for floating number, int is for integer number, and etc.<\/p>\n\n\n\n<p>When creating a table, if the table we are creating exists, the creation will raise an error. In order to avoid this error we can add [IF NOT EXISTS] before the table name.<\/p>\n\n\n\n<p>Special note: we can actually create multiple tables in a single database, but the table names should be different.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># suppose we would like to create another table under the database clorox2020\n\ndef another_table():\n    conn = sqlite3.connect(&quot;clorox2020&quot;)\n    cur = conn.cursor()\n    sql_comm = &quot;CREATE TABLE IF NOT EXISTS buyers(buyer_id text, amount int)&quot;\n    cur.execute(sql_comm)\n    cur.execute(&quot;INSERT INTO buyers VALUES (&#39;75c&#39;, 13)&quot;)\n    cur.execute(&quot;INSERT INTO buyers VALUES (&#39;16d&#39;, -4)&quot;)\n    cur.execute(&quot;INSERT INTO buyers VALUES (&#39;38a&#39;, 10)&quot;)\n    cur.execute(&quot;INSERT INTO buyers VALUES (&#39;59c&#39;, -9)&quot;)\n    cur.execute(&quot;INSERT INTO buyers VALUES (&#39;66b&#39;, 37)&quot;)\n    cur.execute(&quot;INSERT INTO buyers VALUES (&#39;31a&#39;, -43)&quot;)\n    conn.commit()\n\n    cur.execute(&quot;SELECT * FROM buyers&quot;)\n    results = cur.fetchall()\n    for result in results:\n        print(result)\n\n    conn.close()<\/code><\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">('75c', 13)\n('16d', -4)\n('38a', 10)\n('59c', -9)\n('66b', 37)\n('31a', -43)\n<\/pre>\n\n\n\n<p>2. Read<\/p>\n\n\n\n<p>Syntax: SELECT &lt;*\/column&gt; FROM &lt;table&gt; [WHERE &lt;condition&gt;]<\/p>\n\n\n\n<p>Using this command, we can &#8220;read&#8221; some data from a table. In column, we can use &#8220;*&#8221; to select ALL cloumns or type the specific column name to query the data from that columns. We can also select some statistics data like SUM(), AVG(), and etc. In addition, we can use some conditions to somehow filter the data, such as WHERE [column &gt;\/&lt; some value], or WHERE [column = some value]. <\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># get the data form the database\ndef get_some_data():\n    conn = sqlite3.connect(&quot;clorox2020&quot;)\n    cur = conn.cursor()\n    volume_max = cur.execute(&quot;SELECT MAX(Volume) FROM clorox&quot;).fetchone()[0]\n    volume_min = cur.execute(&quot;SELECT MIN(Volume) FROM clorox&quot;).fetchone()[0]\n    volume_avg = cur.execute(&quot;SELECT AVG(Volume) FROM clorox&quot;).fetchone()[0]\n\n    print(f&quot;The maximum volume is:{volume_max}&quot;)\n    print(f&quot;The minimum volume is:{volume_min}&quot;)\n    print(f&quot;The average volume is:{volume_avg}&quot;)\n\n    conn.close()<\/code><\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The maximum volume is:7478700\nThe minimum volume is:489500\nThe average volume is:1776609.865470852<\/pre>\n\n\n\n<p>3. Update<\/p>\n\n\n\n<p>Syntax: UPDATE &lt;table&gt; SET &lt;cloumn&gt; = &lt;some value&gt; [WHERE &lt;condition&gt;]<\/p>\n\n\n\n<p>Using this command, we can modify the value of some columns in our table to the value that we want. It&#8217;s important to notice that if we don&#8217;t use &#8220;WHERE&#8221; to specify the rows, the whole column will be changed to that value. On the other hand, we can use the &#8220;WHERE&#8221; command to modify more than one line.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># update the clorox table and double the colume\ndef update_volume():\n    conn = sqlite3.connect(&quot;clorox2020&quot;)\n    cur = conn.cursor()\n    \n    cur.execute(&quot;UPDATE clorox SET Volume = Volume * 2&quot;)\n    conn.commit()\n    conn.close()<\/code><\/pre><\/div>\n\n\n\n<p>4. Delete<\/p>\n\n\n\n<p>Syntax: DELETE FROM &lt;table&gt; [WHERE &lt;condition&gt;]<\/p>\n\n\n\n<p>Using this command, we can &#8220;delete&#8221; some data from our table. Similar to &#8220;UPDATE&#8221; command, when no condition is assigned, the whole table will be deleted. Therefore, it&#8217;s always a good idea to specify the row that we want to delete using &#8220;WHERE&#8221; command. And the we can also delete more than one line using the &#8220;WHERE&#8221; command.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># delete the rows which contains open price &gt;= 200\ndef delete_open():\n    conn = sqlite3.connect(&quot;clorox2020&quot;)\n    cur = conn.cursor()\n    cur.execute(&quot;DELETE FROM clorox WHERE Open &gt; 200&quot;)\n    conn.commit()\n    conn.close()<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Execute the commands<\/h3>\n\n\n\n<p>After typing the commands that we would like to use, the next step is executing them. To execute the commands, we need to use <code>cur.execute(sql_command)<\/code>. For the &#8220;SELETE&#8221; command, we need to use fetchall() to get all the row or fetchone() to get one row. The fetchall() function will return a list object, we can use a for loop to iterate it. The fetchone() function will return a tuple object of the data. For the &#8220;CREATE&#8221;, &#8220;UPDATE&#8221;, and &#8220;DELETE&#8221; commands, we need to use <code>conn.commit()<\/code> to commit the changes that we make. Otherwise, the changes <strong>WILL NOT<\/strong> be saved to the table. In the end of all the command, if we no longer want to interact with the database, we should use conn<code>.close()<\/code> to close the connection to the database to avoid memory leak.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL stands for &#8220;structured query language&#8221;. It is a programming language for storing and processing information in a relational database. A relational database stores information in tabular form, with rows and columns representing different data attributes and the various relationships between the data values.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=252"}],"version-history":[{"count":5,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/252\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}