Home > CodeIgniter, MySQL, PHP > CodeIgniter Tutorial: [Creating Accounting Application] Part 3 ER Diagram and Creating Database

CodeIgniter Tutorial: [Creating Accounting Application] Part 3 ER Diagram and Creating Database

In the previous parts of this tutorial series we have discussed how to setup the application environment using XAMPP and CodeIgniter, and the application specification. We have done the analysis and design using UML use case and class diagram with free UML diagram tool StarUML.

In this part, we will discuss the database design for our application. This database is needed to hold the data for our application.

We are going to draw the entity relationship diagram (ERD) using a free tool called MySQL workbench, formerly fabForce ERD tools for MySQL. Before continuing, please download the program here and install it to your computer.

At the end of the designing process, we will create our database structure directly to a MySQL server that can be used by our application.


The Tables

For the purpose of our application requirements and based on UML use case we defined before, here is a list of mandatory tables that we must create on the application’s database.

Table name

Purpose

coatype store chart of account type data like assets, liability, equity, income, and cost
coa store the actual chart of account data, should be multi level depth
cashbook store the master cashbook document data
cashbook_detail store the details cashbook document data
bankbook store the master bankbook document data
bankbook_detail store the details bankbook document data
journal store the posted and unposted journal data for the purpose of accounting report generating process like balance sheet, income statement, and trial balance
   
   

 

Creating Tables in MySQL Workbench

Installing MySQL Workbench should not be a problem. I assumed that you already have done that. Now, run the program and you will see a blank workspace of MySQL model like this:

Blank mysql model on mysql workbench

Click on Add Diagram icon to create our first diagram. You will see a blank page for drawing our diagram like this:

Blank diagram on mysql workbench

Creating A New Table and Adding Fields

Next, we need to draw a table. Click on the “Place a New Table” located on the left side of the blank diagram. Then place it on the middle of the blank page. After placing the table, you may resize it’s size so that we can see a tall rectangular like this:

Place a new table on the page

Double click on the table to add it’s columns. You will see a new pane at the bottom of the page consisting the table properties like this:

 Set the new table name

On the pane, there’s a lot of tab on it’s bottom part. Click the “Table” where we can give our new table a name. Create our first table called “cashbook”.

Next we need to define the table columns. Click on the “Columns” tab. You will see the following:

Set the first column as primary key

As you can see, the program automatically add a column called “idcashbook” which you can rename it. This column should be the table’s primary key, so we check on the NN (means not null), AI (auto increment), and Flags PRIMARY KEY. When we set it up, then the icon left to the column name will change to a yellow key icon.

Next, we need to add more column. To do that, double click the empty row below the first column. Enter “trxdate” as the column name and DATETIME as the type. Your table property should be like the following:

Adding a new table column

Repeat the step of adding new field for the following new fields:

Field Name

Type

vounumber VARCHAR(45)
doctype VARCHAR(45)
amount DECIMAL(20,2)
notes VARCHAR(100)
posted INT

Creating Other Tables

Here is a list of tables with it’s description that we need to create for our application. Repeat the above steps to create every table listed below.

Table cashbook (should have been created before)

This is the master table of cashbook document. It has it’s detail records in cashbook_details table.

Field Name

Type

Description

idcashbook INT (NOT NULL AI PK) the table’s primary key, not null auto increment integer value
vounumber VARCHAR(45) the cashbook document number
doctype VARCHAR(45) the document type (cash out or receive)
amount DECIMAL(20,2) the document amount
notes VARCHAR(100) additional notes
posted INT whether this document is posted (1) or not (0)

Table cashbook_detail

This is the detail records of table cashbook.

Field Name

Type

Description

idcashbookdetail INT (NOT NULL AI PK) the table’s primary key, not null, auto increment integer value
description VARCHAR(100) transaction description
amount DECIMAL(20,2) transaction amount
cashbook_idcashbook INT NOT NULL (FK) foreign key to cashbook table

Table bankbook

This is the master table of bankbook document. It has it’s detail records in bankbook_details table.

Field Name

Type

Description

idbankbook INT (NOT NULL AI PK) the table’s primary key, not null auto increment integer value
vounumber VARCHAR(45) the bankbook document number
doctype VARCHAR(45) the document type (bank out or receive)
amount DECIMAL(20,2) the document amount
notes VARCHAR(100) additional notes
posted INT whether this document is posted (1) or not (0)
coa_idcoa INT NOT NULL (FK) foreign key to coa table

Table bankbook_detail

This is the detail records of table bankbook.

Field Name

Type

Description

idcashbookdetail INT (NOT NULL AI PK) the table’s primary key, not null, auto increment integer value
description VARCHAR(100) transaction description
amount DECIMAL(20,2) transaction amount
cashbook_idcashbook INT (FK) foreign key to cashbook table
coa_idcoa INT NOT NULL (FK) foreign key to coa table

Table coa

This table store the chart of account data. It will be referenced by cashbook_detail, bankbook_detail, and journal table.

Field Name

Type

Description

idcoa INT (NOT NULL AI PK) the table’s primary key, not null, auto increment integer value
description VARCHAR(100) the account description
code VARCHAR(45) the account code
begin_balance DECIMAL(20,2) beginning balance of the account
isheader INT whether this account is a header account(1) or a detail account(0)
idparent INT link to other account to identify it’s parent in chart of account structure
link VARCHAR(45) link to usage information of this account, for example “cash” or “bank” account
coatype_idcoatype INT NOT NULL (FK) foreign key to coatype table to identify what kind of account is this

Table coatype

This table store the COA type

Name

Type

Description

idcoatype INT (NOT NULL AI PK) the table’s primary key, not null, auto increment integer value
code VARCHAR(45) the account type code
description VARCHAR(100) the account type description

Table journal

This table store journal transactions generated by cashbook and bankbook transaction after being posted and manual journal transaction entered manually.

Name

Type

Description

idjournal INT (NOT NULL AI PK) the table’s primary key, not null, auto increment integer value
trxdate DATETIME transaction date
vounumber VARCHAR(45) the transaction document number
amount_db DECIMAL(20,2) debit amount
amount_cr DECIMAL(20,2) credit amount
description VARCHAR(100) transaction description
posted INT whether this transaction is posted (1) or not (0)

 

Creating Relationships

To create a relationship between two table in MySQL Workbench, simply click on the “Place a Relationship Using Existing Column” icon on the left side of the diagram. Then:

  1. set the FOREIGN KEY column on the referencing table, and
  2. set the PRIMARY KEY column on the referenced table

For example, to create a relationship between cashbook (the referenced table) and cashbook_detail (the referencing table):

1. Click “Place a Relationship Using Existing Column” icon

Step 1 creating relationship

2. click cashbook_idcashbook column on the cashbook_detail table and click “Pick Referenced Column” button

Step 2 creating relationship

3. click idcashbook column on the cashbook table

Step 3 creating relationship

A relationship line will be drawn between those two tables.

Step 4 creating relationship

 

The Finished ER Diagram

After you have created all the tables and relations as specified above, here is the completed ER diagram will looks like:

The completed ERD

And here is the diagram MWB file for you to download.

 

Creating the Database into MySQL Server

After finishing the design of the ERD we need to create the database structure directly into MySQL server. To do this first we need to define the database server connection. Click on the menu Database – Manage Connections… You will see a dialog like this:

MWB create connection

Click on New button and enter the Connection Name for example localhost, the Database System of MySQL, and the Driver of MySQL Native Driver. On the Parameters pane, enter your hostname or IP address on the Hostname field, Port number which is default to 3306, the Username and Password which match to your MySQL server configuration.

You may optionally click on Test Connection to make sure that the connection to the database server was successful. Click Close button.

To generate the database structure first we need to define the database name to be created. Click on the MySQL Model tab on the main page of MySQL Workbench. Then double click on mydb tab under Physical Schemata section. Look under the mydb property pane. Change the Name to the database name to be created, for example acctdb.

MWB define the database name

Don’t forget to click on Save Current Model icon to save our diagram.

Next, click on the menu Database – Forward Engineer.. to begin creating the database structure. A dialog box will appear:

MWB forward engineer step 1

Click Run Validations to make sure that the table definitions and relationship in our diagram was all correct. Then click on Next button. The next dialog box will appear:

MWB forward engineer step 2 

On that dialog, you specify the options for creating the database. The first options is to drop objects before each CREATE Object, and the second is to generate separate CREATE INDEX statements. Then click on Next button. The next dialog box will appear:

MWB forward engineer step 3

This time we select which objects in our diagram that will be generated on the MySQL database. Select all tables. Then click on Next button. The next dialog box will appear:

MWB forward engineer step 4 

Here we can review the SQL statements to be executed to the MySQL Server. As you can see, the SQL statement was generated based on the diagram that was created before in the design. This including the database, tables, fields, index, and the relations between the tables. Then click on Next button. The next dialog box will appear:

MWB forward engineer step 5

Here we must choose the database connection that we have defined before. If there’s a change on the hostname, username and password, you still can do it here by modifying it on the appropriate field. Then click on Execute button to execute the SQL statement on the server. The next dialog box will appear:

MWB forward engineer step 6

The execution is now complete. It means that the database is created on the server with the tables and relations as defined in our diagram. You may check that on any MySQL front end program like phpMyAdmin, Navicat MySQL, and any other programs. Here is the snapshot of the database structure as viewed with Navicat MySQL program:

The database is successfully created in MySQL server

 

In this part, we have discussed the database design for our application. We have drawn the entity relationship diagram (ERD) using a free tool called MySQL workbench. Also we have created our database structure directly to a MySQL server that can be used by our application. You may download the diagram MWB file here.

In the next article, we are ready to begin the coding of the application.

  • Share/Bookmark
  1. August 13th, 2009 at 21:05 | #1

    mantep bos artikelnya .. :)
    Lanjutkan,ane masi menyimak.
    Uda ga sabar bagian CI nya bos.

  2. August 14th, 2009 at 05:48 | #2

    ocey… minggu depan seri 4 nya insya Allah, sudah masuk ke CI

  3. first
    August 28th, 2009 at 16:06 | #3

    Mas part 4 nya ko blm keluar2?????di tunggu mas….

  4. Steve
    September 2nd, 2009 at 19:46 | #4

    Please keep the tuts coming. I have been working on learning CI and accounting (separately). This will bring both of these projects together for me.
    Also, I have learned about some tools that I did not know about (e.g. MySQL Workbench).

    Thanks !

    Please keep it coming.

  5. Carlos Caniguante
    September 3rd, 2009 at 00:56 | #5

    Really nice tuts, when the fourth part is coming?

    I’m visiting your blog too much times in a day :D

  6. September 13th, 2009 at 22:10 | #6

    R U still out there??? still waiting…

  7. September 14th, 2009 at 05:47 | #7

    yes.. but i’m quite busy lately.. developing a new website for a client : http://www.GpsTrackingIndonesia.com.. hope can continue writing starting today. Thanks for your attention.

  8. Jazzman
    March 9th, 2010 at 04:26 | #8

    Where is the rest of the tutorials? Can’t find part 5 and so on, where can I get them?

  9. Prabowo
    February 11th, 2011 at 10:44 | #9

    Pemakaian Mysql work bench ini untuk yangversi komersial ya pak?dikomputer saya kok abu-2 pilihan untuk menu Database – Forward Engineer nya.
    Kalau yang versi free ada gak ya?thanks

  10. ndu
    July 30th, 2011 at 16:01 | #10

    Mantap gan, semoga beroleh berkah .. :D

  1. No trackbacks yet.
This site uses a Hackadelic PlugIn, Hackadelic SEO Table Of Contents 1.6.0.