{"id":1911,"date":"2024-10-21T16:34:49","date_gmt":"2024-10-21T22:34:49","guid":{"rendered":"https:\/\/pelnar.tech\/?p=1911"},"modified":"2024-10-21T16:34:49","modified_gmt":"2024-10-21T22:34:49","slug":"sql-fixes","status":"publish","type":"post","link":"https:\/\/pelnar.tech\/?p=1911","title":{"rendered":"SQL FIXES"},"content":{"rendered":"\n<p>I&#8217;ve made quite a few mistakes. I will try to both highlight them here as well as fix them in the original posts if that applies. <\/p>\n\n\n\n<p>This is a little out of order, because I haven&#8217;t posted about this table yet, but I created a table that referred to all of the other tables. I had not added any data to it, but realized I forgot a field. I added that field, but I didn&#8217;t like the order. <\/p>\n\n\n\n<p>So the fix for this was to <strong>save any query that adds a table.<\/strong> Since I hadn&#8217;t added any data it was easy to drop the table, modify my CREATE query, and add the table again.<\/p>\n\n\n\n<p>(A tip, not a fix, is to view the top 1000 rows on a table even if there is no data, just to get a sense for the layout.)<\/p>\n\n\n\n<p>Another issue I had was I realized one of the first tables I made needed another field. I added the field, and I&#8217;m willing to accept where it is in the table. (I&#8217;m going to be using views and probably Powershell Scripts to get the data anyway.,) I messed up the DECIMAL data type when adding that field though. I messed up a lot of those. <\/p>\n\n\n\n<p>The fix is to ALTER the table and the column:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE Caliber\nALTER COLUMN MaxOAL DECIMAL(4,3);<\/code><\/pre>\n\n\n\n<p>I seem to have gotten my left and right confused. This field now allows decimals up to 9.999 which is what I need. Originally I set it up as 999.9. <\/p>\n\n\n\n<p>I also wanted that field to be required, but since it was null when added, I couldn&#8217;t do that. So I&#8217;m going back and updating the data manually to add the value. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE Caliber\nSET MaxOAL = 2.810\nwhere CaliberName = '.308 Win';<\/code><\/pre>\n\n\n\n<p>Finally, same thing happened when I began considering the Primer table. The Primer is generally driven by the Caliber. (Sometimes a caliber will have both a Large and Small version, but not often enough to worry about.) So, in this case unfortunately I have a lot to fix. Fortunately ChatGPT planned it out for me;<\/p>\n\n\n\n<p>First, add the PrimerType to Caliber:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE Caliber\nADD PrimerType VARCHAR(50);<\/code><\/pre>\n\n\n\n<p>Then update the table with the data. I found it easiest to just use the CaliberID rather than the name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE Caliber\nSET PrimerType = 'Small Pistol' \nwhere CaliberID = 1;<\/code><\/pre>\n\n\n\n<p>I found that if the primer type is the same across multiple IDs I could use OR to do them all at once and I didn&#8217;t need a single quote:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WHERE CaliberID = 1 or CaliberID = 3;<\/code><\/pre>\n\n\n\n<p>Now dropping the Primer table was tough. There was a foreign key reference to it in the PublishedLoad table. I tried dropping the collumn from the table, and that also didn&#8217;t work. I had to get this command from ChatGPT that gave me all Constraints on the table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT fk.name AS ConstraintName\nFROM sys.foreign_keys AS fk\nJOIN sys.tables AS t ON fk.parent_object_id = t.object_id\nWHERE t.name = 'PublishedLoad';\n<\/code><\/pre>\n\n\n\n<p>And then once I found it I ran this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE PublishedLoad\nDROP CONSTRAINT ConstraintName;  -- Replace ConstraintName with the actual constraint name\n<\/code><\/pre>\n\n\n\n<p>I&#8217;m not going to act like I completely understood that, but for now I just needed to get it done and not understand it. <\/p>\n\n\n\n<p>I was then able to drop the Primer table. <\/p>\n\n\n\n<p>And then I dropped the PrimerID column from the PublishedLoad table. That was weird for me. On one hand, I just <em>know <\/em>the primer type by caliber. On the other, creating a good DB that will give complete output means making sure the data is there. (Garbage in, garbage out. Nothing in, nothing out.) <\/p>\n\n\n\n<p>Then I remembered the data IS there. The PublishedLoad table references the Caliber table, the Powder table, and the Bullet table. The complete view will come later with joins and views. Again, I think I&#8217;m getting the point of SQL! <\/p>\n\n\n\n<p>Last, I needed to remove some test data. Since I tend to use the word &#8220;test&#8221; any time I enter test data, that made it easy:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM Caliber\nWHERE CaliberName LIKE '%Test%';\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve made quite a few mistakes. I will try to both highlight them here as well as fix them in the original posts if that applies. This is a little&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1911","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false,"newspaperly-grid":false,"newspaperly-slider":false,"newspaperly-small":false},"uagb_author_info":{"display_name":"admin","author_link":"https:\/\/pelnar.tech\/?author=1"},"uagb_comment_info":2,"uagb_excerpt":"I&#8217;ve made quite a few mistakes. I will try to both highlight them here as well as fix them in the original posts if that applies. This is a little...","_links":{"self":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts\/1911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1911"}],"version-history":[{"count":3,"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts\/1911\/revisions"}],"predecessor-version":[{"id":1917,"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts\/1911\/revisions\/1917"}],"wp:attachment":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}