You are on page 1of 10

How To Create and Populate a Basic Database Using MySQL 2009

How To:

Create and Populate A Basic Database Using MySQL

Presented by: Roshelle Thompson

How To Create and Populate a Basic Database Using MySQL 2009

Table of Contents

1.0 Introduction..............................................................................................................................3 2.0 Materials Needed.....................................................................................................................3 3.0 Creating the Database.............................................................................................................3


3.1 Examining Data For the Database.....................................................................3 3.1.1 Department Table....................................................................................... 4 3.1.2 Manager Table............................................................................................ 4 3.1.3 Employee Table........................................................................................... 4 3.2 Entity Relationship Diagram.............................................................................. 4 3.2.1 Adding Entities to Diagram.........................................................................4 3.2.2 Assigning Attributes.................................................................................... 5 3.2.3 Defining Relationships.................................................................................6 3.3 Creating and Populating the Database in MySQL..............................................8 3.3.1 Creating the Database................................................................................ 8 3.3.2 Populating the Database............................................................................. 8 3.3.2.1 The CREATE Statement.....................................................................8 3.3.2.1 The INSERT INTO Statement.............................................................9 3.4 Testing the Database........................................................................................ 9

4.0 Conclusion..............................................................................................................................10

How To Create and Populate a Basic Database Using MySQL 2009

1.0 Introduction
Hello, my name is Roshelle Thompson and I am a senior at Florida State University, majoring in Information Technology. Database Administration and Web Development are the two aspects that thrill me the most in the IT field. I enjoy learning new things, and do so with ease. I believe that my superior customer service skills allow me to communicate with the client in order to ensure not just satisfaction, but a general positive relationship between the company and client. I will be showing you how to create an Entity Relationship diagram, a simple database, and populate that database, using MySQL. This manual was written for an audience with basic database knowledge who are familiar with terms such as entity, attribute, foreign key, and primary key. It is also to supplement my How To video found at www.youtube.com/

2.0 Materials Needed


Microsoft Excel 2007 Microsoft Visio 2007 Up to date MySQL

3.0 Creating the Database


3.1 Examining Data for the Database
The first step to creating the database is to examine the information that you will be putting into it. For the purposes of this tutorial we will be using the following data from the following three tables. Each department has an ID number (departmentID), name (deptName), and manager. Each manager has an ID (managerID),last name (mLName), first name (mFName), and phone number (mPhone). The employee table follows the same style, except that each has a department he or she works for, as well as a manager. The tables are populated as follows:

How To Create and Populate a Basic Database Using MySQL 2009 3.1.1 Department Table
department ID* 1 2 3 dName HR Accountin g Sales managerI D* 2 1 3

managerID* 1 2 3

mLName Thompson Miner Smith

mFName Thomas Tracy Zachary

mPhone 850-555-1234 850-555-3333 850-555-2009

3.1.2 Manager Table

3.1.3 Employee Table


employee ID* 1 2 3 4 managerI D* 1 1 2 3 departmentI D* 2 2 1 3

eLName Schaffer Robinson James Arnold

eFName Mary Jennifer Paul John

ePhone 850-5556745 850-5559032 850-5552123 850-5551334

*Please note that the employeeID, managerID, and departmentID are unique, auto generated numbers.

3.2 Entity Relationship Diagram


The next step is to create an Entity Relationship Diagram. This diagram allows the person creating a database to have a visual representation of how the database will be structured, and how the different tables are linked. 3.2.1 Adding Entities to Diagram Upon starting Microsoft Visio, you will select the Software and Database folder located in the menu in the left hand side of the screen. From here 9

How To Create and Populate a Basic Database Using MySQL 2009 you will choose the Database Model Diagram (US units) You will then see a table to the left of the screen which contains different shapes that will help you create the diagram. Click on the Entity shape and drag it to your black page.

From here you will see a Database Properties table at the bottom of the screen. In the Definition category, you can change the physical name of the Entity to anything you would like. In our case we will name our first table Department.

3.2.2 Assigning Attributes Next you will want to go into the Columns category to add the attributes. Here you can type in each attributes name into the Physical Name column. You will also want to select the data type for each, either by using the drop down menu or physically inputting it. You are able to make an attribute the Primary Key, or PK, by checking the box towards the right. Microsoft Visio will also automatically make this attribute required. You do not need to worry about adding in the Foreign Keys, as they will be added automatically when relationships are created between entities. When finished your Columns category should look as follows:

How To Create and Populate a Basic Database Using MySQL 2009

Repeat this for each entity you are adding. I have chosen to use the Data Type VARCHAR(15) for every name, and VARCHAR(12) for the phone numbers. The IDs will all be classified as INTEGERS. When finished your entities should look as follows:

3.2.3 Defining Relationships The next step to creating the ER Diagram is to define the relationships. There are three different types of relationships: one-to-many, many-tomany, and one-to-one. Looking at our entities we can deduce that there are three relationships present: ONE Manager to MANY Employees ONE Department to MANY Employees ONE Manager to ONE Department As you can see, we have two one-to-many relationships, and one one-toone. To show these relationships in your ER Diagram you must first select the Relationship shape, located to the left of the screen, as well as the connector tool located at the top.

How To Create and Populate a Basic Database Using MySQL 2009

Once you have selected these options, you are ready to draw the relationship. To do this you must first point your curser over the entity on the One side of the relationship, in this case the Database entity, until its border becomes red. From here, guide your mouse over to the entity on the Many side, which will be the Employee entity for us. Once that entitys border turns red, release the mouse and an arrow will appear. Microsoft Visio will automatically add the PK of the Many entity as an FK in the One entity. If the relationship is one-to-one, it does not matter which PK becomes the FK, I chose to make managerID the FK in Department. Repeat this for any and all relationships in the diagram. In the end, your diagram should look as follows:

How To Create and Populate a Basic Database Using MySQL 2009

3.3 Creating and Populating the Database in MySQL


Now we are ready to begin creating the actual database. To begin, log into your server via MySQL Query Browser, with your username and password. 3.3.1 Creating the Database To create a database to which you will store your data in, you will construct a simple CREATE statement. To do this you must type CREATE DATABASE followed by the name of the database you wish to create and a semi-colon. In this demonstration I have chosen to call my database demoDB. Therefore, my CREATE statement will be as follows: CREATE DATABASE demoDB; To ensure your database was created, right click in the Schemata window to the right of the screen and select the Refresh button. Your database should now show up in that window. 3.3.2 Adding Data to Database To add the data into our database we will be using two types of statements: the CREATE statement, and the INSERT INTO statement. The CREATE statement will create all of our entities and attributes, while the INSERT INTO statements puts data into these fields 3.3.2.1 The CREATE Statement We start by creating the tables. The syntax for this is as follows:
CREATE TABLE [entity name] ( [Primary Key] int primary auto_increment**, [Attribute] [data type], [Attribute] [data type] ); * key

* Please note: anything in the square brackets is to be filled in with your own information ** This line tells MySQL to automatically assign a number for this field, incrementing by one each time. This ensures that the primary key is in fact unique. For this demonstration, my CREATE statement for the Department entity would be:
CREATE TABLE Department ( departmentID auto_increment, dName varchar(15), managerID int ) ; int primary key

Do this for each entity in your database.

How To Create and Populate a Basic Database Using MySQL 2009 3.3.2.2 The INSERT INTO Statement To add actual data into the table we must use the INSERT INTO statement. The syntax for this is as follows:
INSERT INTO [entity name] VALUES (NULL, [varchar or char], [int] );

For data that is automatically entered, such as the primary key in our place, the value would be NULL. If entering a varchar or char data type it must be enclosed in single quotations. Integers do not need any quotations, you can simply write them in. Each piece of data must be separated by a comma, and there must also be a semi-colon at the end. You must construct a INSERT statement for each row of data in your entity. For the demonstration, my INSERT INTO statements for the Department entity would be:
INSERT INTO Department VALUES (NULL, HR, 2); INSERT INTO Department VALUES (NULL, Accounting, 1); INSERT INTO Department VALUES (NULL, Sales, 3);

Construct such statements for each row in each table.

3.4 Testing Database


To test the database in order to ensure all of your data was successfully added, you are able to double click each table in your database. This will automatically create a SELECT all statement, which you can execute. All of the data in the that table will be shown at the bottom of the screen as such:

How To Create and Populate a Basic Database Using MySQL 2009

4.0 Conclusion
By following each of my steps you will be left with a fully functional database. I have shown you how to construct a Entity Relationship Diagram, and actually Create and add data to a database. From here I encourage you to learn more about databases such as how to query specific information, or how to add users with passwords. The MySQL website, www.mysql.com, has many reference manuals that do a great job in assisting you on your SQL journey. Any comments, questions, roshellethompson@gmail.com or concerns can be sent to my email:

You might also like