When it comes to web development many people seem to forget one important thing. They all start with designing the graphics and page layouts and making sure the code follows every standard and conventions there is, but sadly that’s it. Many don’t think of database design as a important piece in the development. When you work on your code and you are connected to a MySQL database, how many times have you been forced to edit the database schema? If you answer is: many times. Boy, we have some work to do. The problem with designing a database might not occur when you are working alone or in pair but as soon as you start out with a three member team or larger a database design is highly recommended.
What do I mean with designing a database? First of all we need to figure out what makes a database. If we take a look at a database, any database, we can easily divide it into three layers:
- The conceptual layer
- The implementation layer
- The physical layer
The conceptual layer is where we design. This is where we design the database schema with the tables, constraints and relationships. The design we create here can be transferred to any database because it’s not connected to a specific database type. The conceptual layer is an easy to understand explanations of the database. This is where we are going to look deeper today.
The implementation layer is where we actually implement our conceptual database schema. This is where you choose which type of database we want to work with, for example a relational database such as MySQL. Since the conceptual layer is just an explanation of a database of any type we can use the conceptual design in many different implementation layers.
Lastly the physical layer. This is where we actually store our database and data on the hard drive. Here we setup how we want to access files, index and more.
OK, we go back to the conceptual layer and we are going to introduce a program to use when we want to design a database conceptual schema. The program is called Dia and is available for free.
If you are totally new to the concept of relational database and have absolutely no clue about what I went through earlier I will try to explain a few things before start with designing a test database. When we design we will look upon three different types of objects.
- A relation is the same as a table, but we call it a relation. When we design we are looking for any type of real world object like a car, books or users. A relation is displayed as a rectangular.
- A relationship is the constraint between two relations. A car must be owned by a user for example. A relationship is displayed as a diamond shape.
- A attribute. A relation has attributes and as we might know it different fields. A user might have id, username, password and email attribute. These are connected to a relation via a thin line and the attribute itself is surrounded by a circle.
So now we have three different objects we can use and it looks fairly easy now doesn’t it? We basically have all the tools we need to create a conceptual design everyone can look at. As I mentioned the important thing about designing a database is that it’s crucial not to change the database schema. Of course you might have to do it during development and in rare cases when the site is online but as a ground rule you shouldn’t change the database schema due to the fact that many other applications and users might depend on it.
OK, when you have downloaded the Dia program, install it and run it. On startup two different windows will popup: a workbench and a menu box. In the menu box there is a drop down menu with the default value ‘Diverse’. Click on that and on the second link from the bottom there is a little arrow pointing to the right, click on that and choose the ‘ER’. This stands for Entity-Relationship module. Why do we say Entity-Relationship module when we just talked about Relation, Relationships and Attributes? A relation is sometimes also called a Entity. What we changed in the menu is that now the Dia program is in the ER mode. Under the drop down menu we just entered a few new icons have popped up.
- E inside of a rectangular: this is our relation/entity button
- E inside of a double rectangular: this is also our relation/entity button but for weak entities.
- R in a diamond: our relationship button
- A in a circle: our attribute button
Simple isn’t it? Now we can start designing our conceptual database schema. We will start with a simple example. Lets say we want to save users in our database. Each user has a unique id, username, password, email and gender. How can we display this? We start with dragging out a Entity box. Double click on it to access the settings. Name it to ‘User’ and click apply. The entity is done. Now we should add the attributes. Click on the circle with the A inside. First we create the id attribute, make sure to check the ‘key’ option in the attribute settings panel. When you press apply the attribute will be underlined, this shows that the attribute is a key. Continue adding the username, password, email and gender attribute but don’t apply the key option. Finally draw a line between each attribute and connect it to the entity box. I used the line tool and not the double lined button found under the Entity button. Make sure you don’t use arrows, just a straight line. We should come up with something like this:
Next we add a new entity Car. It will have two attributes: a registration number and color. The registration number will be the unique attribute for a car and the color attribute can be multivalued. Even though the normalization rules tells us that multivalued fields in a database is wrong, we need to remember that we are working on the conceptual design. We do not think of terms like implementation or MySQL.
We add a new Entity and name it Car. We add the registration number attribute and add the ‘key’ option. For the color attribute we make it multivalued by simple adding that option key. The attribute will be boxed in by a double lined circle, this is telling us that the attribute is multivalued. We take our design one step further. Each User is able to own several cars but one Car can only be owned by one User. How do we display this? We use the Relationship object! Insert a Relationship object between our User and Car entity. Name it owns. Double click to access the settings panel in the left cordiality enter ‘n’ and in the right enter 1. Finally connect each Entity to the Relationship. It will look something like this:
Now we have done something really cool. We have created a design of a database with two entities and one relationship between them. It’s easy to see the attributes of each entity and the relationships between them. Imaging a very big database, a design like this would certainly ease it up a bit!
This is only a short guide on how to use the ER model. All I wanted to do was to introduce the Dia program and how you can use it. You will find a useful article about the ER model at the link below. OK, that’s it. A very short introduction on the ER-modelling and how you can use Dia to create it. Thanks for your time!
Greetings!
Brilliant post. Many thanks for introducing Dia! I found it very useful.
I have been designing web applications in php/mysql for over 6 years now, but I have never took the time to design my databases (always created on-the-fly as I code my apps).
I have looked at MySQL Workbench but do not understand what views and layers are and the MySQL team are not going to have a tutorial ready till next year that explains the conventions of a db schema.
I came across this article, which looks interesting, but how would Dia deal with tables that have more than 10 fields and also have 50-100 tables? It looks like it would get very messy, especially as my dbase schema has many tables being linked to many tables so there are a lot of relations.
Regards,
Paul.
Hi Paul,
In my experiance with Dia it doesn’t scale so good. If we have as you say 50-100 different tables connected in different ways the schema would get very messy and cluttered.
For these cases I would suggest grouping the tables into smaller Dia schemas, give each schema a unique name and lastly create an overview where each of the groups are connected. This might not be possible if all the tables are connected to each other but you might find tables you are able to group.
It’s actually an interesting topic you bring forward, I don’t know any database table schema program that scale well.
Thanks for your time and your read!
Kind regards,
Niklas.
I’ve helped database programmers understand the implications of SEO concepts in web design, and now I’m finally going the opposite direction, designing PHP/mySQL databases… and I got stuck on how to design them. Then I found this. Awesome.
So…
Is a relation rectangle box, also the place to denote a PHP function? Afterall, to design, you kind of need to tie everything together, with function, somehow, graphically, in the plan, do we not?