{"componentChunkName":"component---src-templates-blog-post-js","path":"/mysql-workbench-guide","result":{"data":{"markdownRemark":{"html":"<p>A database is one of the key components in a software system and thus, has to be designed with utmost care. If\nyou have only a limited knowledge of databases and MySql, creating your first database would be a bit challenging.\nThis post is a quick guide to create your first database using MySql Workbench.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133356975-64158712-42f1-4d2f-bb5e-6e62f0f4374a.png\" alt=\"mysql1\"></p>\n<p>The post does not follow a specific software designing architecture but rather gives some general guidelines, and\nassumes that you have MySql and MySql Workbench set up in your machine.</p>\n<h3>1. Define the scope of your project</h3>\n<p>The size of your database depends on the scope of your project. Thus, decide on the specific components that\nyou are going to implement in your system. It would be easier if you list the use cases that would be and would not\nbe there in your system. As an example, if it is a system for handling patient information in one section in a\nhospital, decide if you are handling the payments of patients as well.</p>\n<h3>2. Design an Entity Relationship Diagram</h3>\n<p>An Entity Relationship Diagram (ERD) shows the entities in your database and their relationships between each other.\nIn MySql Workbench, you can easily create an ER Diagram, and forward engineer it to create your database.</p>\n<p>It is better if you have a rough sketch of your ERD before creating it on Workbench. To create a sketch,</p>\n<ol>\n<li>List out the main <strong>entities</strong> (or tables) that should be in your database. For instance, a system for a school\nneeds to have tables such as student, teacher, administration, course, section and resource.</li>\n<li>Add suitable <strong>attributes</strong> to your tables. For a student table, the attributes would include student id, name,\nbirthday, address and contact number.</li>\n<li>Choose the attribute or attributes that would be the <strong>primary key</strong> for each table. The primary key of a table\nis one or more attributes that can uniquely identify each record in the table. Two rows cannot have the same\nvalue for the primary key. In student table, the primary key would be the student id.</li>\n<li>Decide on the <strong>relationships</strong> between your entities. There can be three main types of relationships between\ntables; one-to-one, one-to-many and many-to-many. As an example, the relationship between student and teacher\ntables is many-to-many.</li>\n</ol>\n<h3>3. Create an ERD in MySql Workbench</h3>\n<p>To create an ER diagram, go to <code class=\"language-text\">File &gt; New Model</code> in MySql Workbench and click on the <code class=\"language-text\">Add Diagram</code> icon. This\nwould open a new tab, <code class=\"language-text\">EER Diagram</code>.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357445-722b54ea-5c76-4df2-a248-6b640cae7aa4.png\" alt=\"mysql2\"></p>\n<h3>4. Change database name</h3>\n<p>Right click on the database name in <code class=\"language-text\">Catalog Tree</code> and change the name in the panel that appears.\nMake sure you do not have spaces in your database (or schema) name, table names or attribute names. You can use\nunderscore character instead.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357454-e2c92338-3807-4b29-a523-205a6486fd12.png\" alt=\"mysql3\"></p>\n<h3>5. Add tables and Relationships</h3>\n<p>Now you can simply use the toolbar on left to create the ER diagram. Use Table tool to add a new table.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357549-4914b13e-20ea-4c73-a2d2-331f8005e279.png\" alt=\"mysql4\"></p>\n<p>Double click on the table name to get the panel where you can change table name and add attributes to the table.</p>\n<p>In the panel, the following column flags can be specified for attributes.\nPK – Primary Key\nNN – Not Null\nUQ – Unique Key\nBIN – Binary\nUN – Unsigned\nZF – Zero Filled\nAI – Auto Increment</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357555-8c76f153-45ca-4c0c-a9dd-18e29fc15ae8.png\" alt=\"mysql5\"></p>\n<p>Add relationships using the toolbar. Click on a relationship icon and click on the two tables that you want to connect.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357643-50fb8b66-6e8e-4875-8a0a-6bbcff2c9c16.png\" alt=\"mysql6\"></p>\n<p>If it is a <strong>many-to-many</strong> relationship, the connection would automatically generate another table containing the\nprimary keys of the two tables. These attributes on the new table reference the other two tables, and are called\nforeign keys. Adding relationships generate foreign keys. </p>\n<p>A <strong>foreign key</strong> is an attribute that can uniquely identify a record in another table. Here, a primary key value that\nis not there in the student table cannot be in the student<em>id column in the student</em>has_teacher table. You can\nchange the name and add attributes to this new table as well.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357653-07722e28-318c-4156-88b4-4eccd2e967d8.png\" alt=\"mysql7\"></p>\n<p>If it is a <strong>one-to-many</strong> relationship, first click on the table on the <strong>many</strong> side. This would create a foreign key\nin the table in the <strong>many</strong> side referencing the other table.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357863-00c113ef-1df0-42f1-9291-ccb44ab188cf.png\" alt=\"mysql8\"></p>\n<h3>6. Create a database from the ER Diagram</h3>\n<p>After creating the complete ER Diagram, re check it for any corrections and save it.\nThen select <code class=\"language-text\">Database – Forward Engineer</code> to create the database.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357882-a7e43103-7671-4ad6-a53c-e7ed356cc032.png\" alt=\"mysql9\"></p>\n<p>Specify your MySql Connection details (username and password) and click Next. You can also save the script of your\ndatabase to a file for later reference.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357965-8b02f7e3-1df2-45c4-9395-09bce4425a54.png\" alt=\"mysql10\"></p>\n<p>Go to your connection tab, and refresh the Navigator panel. You would see your new database there. Double click\non it to make it your default database.</p>\n<p><img src=\"https://user-images.githubusercontent.com/10103699/133357973-0d1775e4-8c99-4bb5-9e50-c7fef5bbdff4.png\" alt=\"mysql11\"></p>\n<p>Happy coding with SQL!</p>","frontmatter":{"date":"January 22, 2016","path":"/mysql-workbench-guide","title":"Create your First Database in MySql Workbench","tags":["How to","Databases"]}}},"pageContext":{}},"staticQueryHashes":["3649515864"]}