{"id":2153,"date":"2020-02-13T06:26:15","date_gmt":"2020-02-13T06:26:15","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=2153"},"modified":"2020-02-24T12:59:02","modified_gmt":"2020-02-24T12:59:02","slug":"sql-best-practices","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/sql-best-practices\/","title":{"rendered":"SQL Best Practices"},"content":{"rendered":"<h2>Several SQL(structured query language) best practices are:<\/h2>\n<ol>\n<li>\n<h3>Choose relationships wisely<\/h3>\n<p>The SQL (structured query language) language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d. Orphans are the tables that don\u2019t have a relationship with any other table and are considered poor database designing techniques. Each table should have a relationship with another, so when we query the database for records, we can join the tables together through their relationships.<br \/>\nWe can build a relationship through primary and foreign keys. For example, suppose we have a customer table and an item table. Without the customer ID in the order table, the order table is considered orphaned. By placing the customer ID in the item table, we now have a relationship between the two tables\n<\/li>\n<li>\n<h3>Always keep performance in mind<\/h3>\n<p>Just because the SQL database has few records currently doesn\u2019t mean the database won\u2019t grow significantly after we move it to production. We must always join tables on indexes and have a primary key in each table. One common mistake is to run a poorly performing query in development when the system has very few records. The query runs fast because there\u2019s currently no load on the database server. But, when the query is promoted to production and run in a busy environment, the query performs poorly and lowers site performance. Always consider performance even if the query seems it won\u2019t need any resource from the database server.<\/li>\n<li>\n<h3>Use table aliases for better readability<\/h3>\n<p><strong>Aliases <\/strong>shorten the name of a table and make it easier to read and understand the logic in an SQL statement. When building a database, always consider the way the code should be written. It becomes easy for us and another developer to determine the logic and data set behind it. Poorly coded SQL code can lead to bugs and another developer may need to edit it.<\/li>\n<li>\n<h3>Be specific with SELECT statements<\/h3>\n<p>In any common SQL language, ( * ) tells the database engine to return all columns within a table. There are several issues related to this habit. The primary issue is security. If a hacker is able to use a SQL injection attack on our database, it could leave every column of the table available for theft. If you have customer passwords stored in the table, the attacker can get those passwords easily.<\/p>\n<p>Another issue is performance. If we have a million records returned from a query, performance can be affected when we return all the columns rather than the few that we need.<\/p>\n<p>The third issue is that it is difficult to determine what data will be returned. If we specify columns in our SELECT queries, we already know exactly which ones will be returned.\n<\/li>\n<li>\n<h3>Use NOCOUNT for Large Batch Queries<\/h3>\n<p>When we perform queries such as update and insert on our database, the engine first performs the action, then gives us a count of the number of records that were affected. This work is done in a great way when we want to confirm the changes are to a specific number of records, but it shouldn\u2019t be done on queries that run regularly.<br \/>\nWhen NOCOUNT isn\u2019t used, the <a href=\"https:\/\/studysection.com\/blog\/introduction-to-database-concepts\/\">database<\/a> must have to count the number of rows that were affected. This isn\u2019t necessary for queries that run regularly. Use NOCOUNT at the top of the stored procedure or ad hoc query to improve the performance.\n<\/li>\n<li>\n<h3>Avoid Dynamic SQL as much as possible<\/h3>\n<p>An SQL injection is one of the common attacks on the web. This type of attack leads to data breaches that expose millions of records to an attacker. Extremely strategic SQL injection attacks can even escalate permissions for the attacker to give them administrative rights on the database server.<br \/>\nDynamic SQL is a type of coding that builds an SQL statement based on input from the user. Commonly, it\u2019s done on the front-end application side, but some SQL developers use it. Dynamic SQL should be avoided at all costs\u2014it\u2019s the type of development that leads to SQL injection exposure.\n<\/li>\n<\/ol>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>The key to good database design is, to always put performance and security on priority. Remember to properly format the code for other developers who might need to edit the code or maintain it in the future. SQL may get complicated if we don\u2019t organize our code according to the techniques discussed above. We should always create a design layout before starting the code.<\/p>\n<p><small><em>The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an <a href=\"https:\/\/www.studysection.com\/advanced-english-advanced\">English certification exam<\/a> that tests English language proficiency in English grammar, reading, and writing.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Several SQL(structured query language) best practices are: Choose relationships wisely The SQL (structured query language) language is the language of<\/p>\n","protected":false},"author":1,"featured_media":2154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[96,306],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - SQL (Structured Query Language) Best Practices<\/title>\n<meta name=\"description\" content=\"The SQL language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/sql-best-practices\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - SQL (Structured Query Language) Best Practices\" \/>\n<meta property=\"og:description\" content=\"The SQL language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/sql-best-practices\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-13T06:26:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-02-24T12:59:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/02\/sql.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"SQL Best Practices\",\"datePublished\":\"2020-02-13T06:26:15+00:00\",\"dateModified\":\"2020-02-24T12:59:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/\"},\"wordCount\":801,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"practices\",\"SQL\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/sql-best-practices\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/\",\"url\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/\",\"name\":\"StudySection Blog - SQL (Structured Query Language) Best Practices\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-02-13T06:26:15+00:00\",\"dateModified\":\"2020-02-24T12:59:02+00:00\",\"description\":\"The SQL language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/sql-best-practices\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/sql-best-practices\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Best Practices\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - SQL (Structured Query Language) Best Practices","description":"The SQL language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/sql-best-practices\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - SQL (Structured Query Language) Best Practices","og_description":"The SQL language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d.","og_url":"https:\/\/studysection.com\/blog\/sql-best-practices\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-02-13T06:26:15+00:00","article_modified_time":"2020-02-24T12:59:02+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/02\/sql.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/sql-best-practices\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/sql-best-practices\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"SQL Best Practices","datePublished":"2020-02-13T06:26:15+00:00","dateModified":"2020-02-24T12:59:02+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/sql-best-practices\/"},"wordCount":801,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["practices","SQL"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/sql-best-practices\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/sql-best-practices\/","url":"https:\/\/studysection.com\/blog\/sql-best-practices\/","name":"StudySection Blog - SQL (Structured Query Language) Best Practices","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-02-13T06:26:15+00:00","dateModified":"2020-02-24T12:59:02+00:00","description":"The SQL language is the language of relational databases. We need to create tables based on relationships and must not create \u201corphans\u201d.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/sql-best-practices\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/sql-best-practices\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/sql-best-practices\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SQL Best Practices"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":397,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/2153"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=2153"}],"version-history":[{"count":6,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/2153\/revisions"}],"predecessor-version":[{"id":2227,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/2153\/revisions\/2227"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/2154"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=2153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=2153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=2153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}