{"id":1911,"date":"2014-05-08T22:17:58","date_gmt":"2014-05-08T22:17:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1911"},"modified":"2014-05-08T22:17:58","modified_gmt":"2014-05-08T22:17:58","slug":"sql-basics-part-3","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/sql-basics-part-3\/","title":{"rendered":"SQL Basics (Part 3)"},"content":{"rendered":"<p><strong>CRUD<\/strong><\/p>\n<p>In previous SQL Basics posts we&#8217;ve looked at querying the database, but we actually need to get some data into the database. So let&#8217;s go over the <strong>C<\/strong>reate, <strong>R<\/strong>etrieve, <strong>U<\/strong>pdate, <strong>D<\/strong>elete commands.<\/p>\n<p><strong>Create<\/strong><\/p>\n<p>To create data in SQL we use the INSERT keyword, for example<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nINSERT INTO Plants (CommonName, Type_Id) VALUES ('Strawberry', 2)\r\n<\/pre>\n<p>This creates a new plant with the CommonName Strawberry and the Type_Id 2.<\/p>\n<p><strong>Retreieve<\/strong><\/p>\n<p>We&#8217;re not going to go through this again as we&#8217;ve dealt with the SELECT query already (this is the way we retrieve data using SQL). <\/p>\n<p><strong>Update<\/strong><\/p>\n<p>The UPDATE keyword is used to alter data with SQL. <\/p>\n<p><strong>Beware, if no WHERE clause is used then ALL rows will be updated<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUPDATE Plants p SET p.CommonName = 'Strawberry' where p.Id = 123\r\n<\/pre>\n<p><strong>Delete<\/strong><\/p>\n<p>DELETE is used to remove items from the database using SQL. We can delete one or more rows at a time but only from one table at a time, for example<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nDELETE FROM Plants\r\n<\/pre>\n<p><strong>Beware you can easily delete ALL rows from a table if you do not specify a WHERE clause<\/strong><\/p>\n<p><strong>Transactions<\/strong><\/p>\n<p>We&#8217;ve finished looking at the CRUD operations but you&#8217;ve probably noted some of the pitfalls of not correctly forming your DELETE or UPDATE queries. So let&#8217;s look a transactions which allow us to make changes which are not permanent until the transaction is completed.<\/p>\n<p>First off, let&#8217;s look at the concept of ACID&#8230;<\/p>\n<p>ACID stands for <strong>A<\/strong>tomic, <strong>C<\/strong>onsistent, <strong>I<\/strong>solated and finally <strong>D<\/strong>urable.<\/p>\n<p>A transaction is said to be atomic in that it either happens or doesn&#8217;t happen, i.e. we cannot have a partially altered set of data. It&#8217;s consistent if the transaction leaves the database in a consistent state. It&#8217;s isolated in that it occurs in a serial way and finally it&#8217;s durable if it&#8217;s &#8220;permanently&#8221; stored, i.e. it&#8217;s not kept in memory but stored on disc so it will still be available after a reboot (for example). <\/p>\n<p>Transaction syntax requires we tell the database we&#8217;re beginning a transaction, then we run our SQL command before either committing the transaction or rolling it back.  For example<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nBEGIN TRANSACTION\r\nDELETE FROM Plants\r\n--ROLLBACK TRANSACTION\r\n--COMMIT TRANSACTION\r\n<\/pre>\n<p>Now in the sample above I have started a transaction and deleted all Plants. If I now try to get all rows from the Plants table I&#8217;ll find they&#8217;re all gone. But uncommenting the ROLLBACK will allow us to cancel the transaction and return all the rows we seemed to have deleted.<\/p>\n<p>Obviously had this been our intention then we could alternatively just uncomment the COMMIT transaction and commit out changes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CRUD In previous SQL Basics posts we&#8217;ve looked at querying the database, but we actually need to get some data into the database. So let&#8217;s go over the Create, Retrieve, Update, Delete commands. Create To create data in SQL we use the INSERT keyword, for example INSERT INTO Plants (CommonName, Type_Id) VALUES (&#8216;Strawberry&#8217;, 2) This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[22,10],"tags":[],"class_list":["post-1911","post","type-post","status-publish","format-standard","hentry","category-database","category-sql"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1911"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1911\/revisions"}],"predecessor-version":[{"id":1974,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1911\/revisions\/1974"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}