Software developers nowadays usually work in various fields of programming and development. Such as UI design, server-side programming(even in multiple languages like PHP, Asp.NET, etc.), and database management. In each area, if we use and follow some traditional best practices. Then, they will always be organized and easy to keep track of from time to time among other developers. As a developer, we may not be highly expert in working with databases, but maintaining a few MySQL best practices will definitely help us deliver quality products.
Why follow Database best practice conventions?
Suppose you can design a database and its table that is easily understandable to other developers who are working on the same database(or will work in the future). In such cases, you will get a clap for what you have done after experiencing the efficiency, easily understandably you have kept for them, and you will be in a good eye of them too. To direct you for beginning in such way, in this tutorial, i will try to give some of the MySQL best practices. I will use the MySQL database in PHPMyAdmin as management here(as they are used by most of the open-source developers).
Deep Look At DB Engine/Collation Settings:
First, you must have to keep an eye open from the beginning when you are creating a database table for your application. You should be aware of these two things at least:
Database Engine
Collation
Although they are set by default to ‘MyIasm’ and ‘latin1_swedish_ci’ correspondingly, sometimes, for a robust application, they should be changed to a suitable type for optimized support from the database. For example, if your application strictly needs transaction operations and/or foreign key constraints, then you should use ‘Innodb’ as your database engine. It’s not required for all tables but only for the tables that make use of such operations. Similarly, ‘utf8_general_ci’ and ‘utf8-unicode_ci’ are two general, mainly used alternative forms for collation/character-set. So, check and make the necessary changes from your PhpMyadmin’s ‘operations’ tab for a particular table. You will get advantages like:
Server coding will be efficient enough to keep track of transaction-type operations, whereas, in the case of the default ‘MyIasm’ database engine, you had to take care of these types of operations yourself right inside the coding, which might be buggy.
If your application goes live(it contains some data) and you want to change the collation type/engine type at that time, you may get into some unexpected trouble. It’s better to take care of this at the beginning.
Database Table Column/Field Definitions:
Another point is when we are creating a table and filling in the field info. We are always in a hurry to only fill in the necessary parts like ‘auto-increment,’ ‘column name,’ ‘type,’ and ‘length’ and save the table. Most of us don’t even see the other info/fields. But they also need to be marked seriously. Like, at the bottom, you will be able to see a field for comment about the table, write necessary description here for the reasons this table is to be used. Also, there is a similar field for each column you are about to create to add some comments about the field. Although most of the column names are self-explanatory, if you are using any particular fields that may not be understood by all easily, you can put a small comment about it to explain this to everyone. This comment will be visible as a tooltip on the structure tab of that table. Also, the field names will be underlined to indicate this field has some particular info(a little different but similar to the primary key column that has been highlighted). The advantages of this are:
Next time, anyone who sees that table structure can get a clear idea about the special column that he might not have been able to understand before and might have to ask you(wasting time for both of you).
If you are working long-term in a system, then there is also a chance to forget about some unusual columns and what they are for exactly. This comment will also help you in those cases.
If you or somebody else is going to clean up the database, this will help show unused columns that can be removed.
If you are not using the PhpMyadmin interface and want to set the DB engine by SQL query, you can use the following code sample for executing a query that will do the same as PHPMyAdmin:
/*your database user must have to have permission to execute this query, either for sql query window or executing script like php*/
ALTER TABLE `your_table_name` ENGINE = Innodb
Code language:JavaScript(javascript)
Other MySQL Best Practices to follow:
Table naming. Try to use a prefix at the beginning of each table. This is very helpful when you want to set up two or more separate application/module tables in the same database; this will easily distinguish between them instead of mixing them up.
Column naming. Try to use the same standard and convention in naming all table’s column names. This can be camel case, Pascal case, or lower case words separated by an underscore(this is most preferable to avoid unexpected case sensitivity problems). This is also essential/recommended by many database layer abstraction libraries (such as Doctrine), which auto-generate properties to/from the database and take care of the singular/plural form themselves.
Try to use the proper data type and their length. If you assign an email field to length 255, it will be meaningless and a waste of memory.
As soon as you follow these database best practices for a few days, you will adjust to them, and as a result, you will go ahead with one more step in the field of good programming and development. I will try my best to share many more tips on best practices in the future. Happy programming 🙂
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Google SEO says
Thanks a million for this, I appreciate the info