# mysql – First-time database design: am I overengineering?

## The Question :

246 people think this question is useful

# Background

I’m a first year CS student and I work part time for my dad’s small business. I don’t have any experience in real world application development. I have written scripts in Python, some coursework in C, but nothing like this.

My dad has a small training business and currently all classes are scheduled, recorded and followed up via an external web application. There is an export/”reports” feature but it is very generic and we need specific reports. We don’t have access to the actual database to run the queries. I’ve been asked to set up a custom reporting system.

My idea is to create the generic CSV exports and import (probably with Python) them into a MySQL database hosted in the office every night, from where I can run the specific queries that are needed. I don’t have experience in databases but understand the very basics. I’ve read a little about database creation and normal forms.

We may start having international clients soon, so I want the database to not explode if/when that happens. We also currently have a couple big corporations as clients, with different divisions (e.g. ACME parent company, ACME healthcare division, ACME bodycare division)

The schema I have come up with is the following:

1. From the client perspective:
• Clients is the main table
• Clients are linked to the department they work for
• Departments can be scattered around a country: HR in London, Marketing in Swansea, etc.
• Departments are linked to the division of a company
• Divisions are linked to the parent company
2. From the classes perspective:
• Sessions is the main table
• A teacher is linked to each session
• A statusid is given to each session. E.g. 0 – Completed, 1 – Cancelled
• Sessions are grouped into “packs” of an arbitrary size
• Each packs is assigned to a client

I “designed” (more like scribbled) the schema on a piece of paper, trying to keep it normalised to the 3rd form. I then plugged it into MySQL Workbench and it made it all pretty for me:

(source: maian.org)

## Example queries I’ll be running

• Which clients with credit still left are inactive (those without a class scheduled in the future)
• What is the attendance rate per client/department/division (measured by the status id in each session)
• How many classes has a teacher had in a month
• Flag clients who have low attendance rate
• Custom reports for HR departments with attendance rates of people in their division

# Question(s)

• Is this overengineered or am I headed the right way?
• Will the need to join multiple tables for most queries result in a big performance hit?
• I have added a ‘lastsession’ column to clients, as it is probably going to be a common query. Is this a good idea or should I keep the database strictly normalised?

• Dear first year CS student: please keep using StackOverflow. Your question is interesting, well-written and helpful. In other words, you are in the top 1% of question askers.
• Can a Division contain other Divisions? IF that is the case a “has” table might be used to link the Division back to the Division it is contained by.
• Thanks for the kind comments 🙂 Mark I’ll have to go over the documentation for this project again, but I don’t think we identified that case. Thanks for pointing it out.
• I don’t like your primary key naming convensions. table divisions has column named divisionid. Don’t you find that redundant? Just name it id. also your table names including _has_: i would remove that and just name it for example cities_departments. your DATETIME columns should be of type TIMESTAMP unless they’re user-input values. I think it’s a good idea to have the cities and countries tables. you may run into trouble limiting tables to a single status. consider using an INT and perform bitwise comparisons on it- so you can hold more meaning there
• @binnyb There’s a lot of argument about using id as the name of the primary key which people should consider before deciding .

42 people think this answer is useful

1) You’re pretty much on target for someone who is approaching a problem like this for the first time. I think the pointers from others on this question thus far pretty much cover it. Good job!

2 & 3) The performance hit you will take will largely be dependent on having and optimizing the right indexes for your particular queries / procedures and more importantly the volume of records. Unless you are talking about well over a million records in your main tables you seem to be on track to having a sufficiently mainstream design that performance will not be an issue on reasonable hardware.

That said, and this relates to your question 3, with the start you have you probably shouldn’t really be overly worried about performance or hyper-sensitivity to normalization orthodoxy here. This is a reporting server you are building, not a transaction based application backend, which would have a much different profile with respect to the importance of performance or normalization. A database backing a live signup and scheduling application has to be mindful of queries that take seconds to return data. Not only does a report server function have more tolerance for complex and lengthy queries, but the strategies to improve performance are much different.

For example, in a transaction based application environment your performance improvement options might include refactoring your stored procedures and table structures to the nth degree, or developing a caching strategy for small amounts of commonly requested data. In a reporting environment you can certainly do this but you can have an even greater impact on performance by introducing a snapshot mechanism where a scheduled process runs and stores pre-configured reports and your users access the snapshot data with no stress on your db tier on a per request basis.

All of this is a long-winded rant to illustrate that what design principles and tricks you employ may differ given the role of the db you’re creating. I hope that’s helpful.

14 people think this answer is useful

You’ve got the right idea. You can however clean it up, and remove some of the mapping (has*) tables.

What you can do is in the Departments table, add CityId and DivisionId.

Besides that, I think everything is fine…

6 people think this answer is useful

The only changes I would make are:
1- Change your VARCHAR to NVARCHAR, if you might be going international, you may want unicode.

2- Change your int id’s to GUIDs (uniqueidentifier) if possible (this might just be my personal preference). Assuming you eventually get to the point where you have multiple environments (dev/test/staging/prod), you may want to migrate data from one to the other. Have GUID Ids makes this significantly easier.

3- Three layers for your Company -> Division -> Department structure may not be enough. Now, this might be over-engineering, but you could generalize that hierarchy such that you can support n-levels of depth. This will make some of your queries more complex, so that may not be worth the trade-off. Further, it could be that any client that has more layers may be easily “stuffable” into this model.

4- You also have a Status in the Client Table that is a VARCHAR and has no link to the Statuses table. I’d expect a little more clarity there as to what the Client Status represents.

6 people think this answer is useful

No. It looks like you’re designing at a good level of detail.

I think that Countries and Companies are really the same entity in your design, as are Cities and Divisions. I’d get rid of the Countries and Cities tables (and Cities_Has_Departments) and, if necessary, add a boolean flag IsPublicSector to the Companies table (or a CompanyType column if there are more choices than simply Private Sector / Public Sector).

Also, I think there’s an error in your usage of the Departments table. It looks like the Departments table serves as a reference to the various kinds of departments that each customer division can have. If so, it should be called DepartmentTypes. But your clients (who are, I assume, attendees) do not belong to a department TYPE, they belong to an actual department instance in a company. As it stands now, you will know that a given client belongs to an HR department somewhere, but not which one!

In other words, Clients should be linked to the table that you call Divisions_Has_Departments (but that I would call simply Departments). If this is so, then you must collapse Cities into Divisions as discussed above if you want to use standard referential integrity in the database.

5 people think this answer is useful

By the way, it’s worth noting that if you’re generating CSVs already and want to load them into a mySQL database, LOAD DATA LOCAL INFILE is your best friend: http://dev.mysql.com/doc/refman/5.1/en/load-data.html . Mysqlimport is also worth looking into, and is a command-line tool that’s basically a nice wrapper around load data infile.

3 people think this answer is useful

Most things have already been said, but I feel that I can add one thing: it is quite common for younger developers to worry about performance a little bit too much up-front, and your question about joining tables seems to go into that direction. This is a software development anti-pattern called ‘Premature Optimization‘. Try to banish that reflex from your mind 🙂

One more thing: Do you believe you really need the ‘cities’ and ‘countries’ tables? Wouldn’t having a ‘city’ and ‘country’ column in the departments table suffice for your use cases? E.g. does your application need to list departments by city and cities by country?

3 people think this answer is useful

Following comments based on role as a Business Intelligence/Reporting specialist and strategy/planning manager:

1. I agree with Larry’s direction above. IMHO, It’s not so much over engineered, some things just look a little out of place. To keep it simple, I would tag client directly to a Company ID, Department Description, Division Description, Department Type ID, Division Type ID. Use Department Type ID and Division Type ID as references to lookup tables and internal reporting/analysis fields for long term consistency.

2. Packs table contains “Credit” column, shouldn’t that actually be tied to the Client base table so if they many packs you can see how much credit owed is left for future classes? The application can take care of the calc and store it centrally in the Client table.

3. Company info could use many more fields, including the obvious address/phone/etc. information. I’d also be prepared to add in D&B “DUNs” columns (Site/Branch/Ultimate) long term, Dun and Bradstreet (D&B) has a huge catalog of companies and you’ll find later down the road their information is very helpful for reporting/analysis. This will take care of the multiple division issue you mention, and allow you to roll up their hierarchy for sub/division/branches/etc. of large corps.

4. You don’t mention how many records you’ll be working with which could imply setting yourself up for a large development initiative which could have been done quicker and far fewer headaches with prepackaged “reporting” software. If your not dealing with a large database (< 65000) rows, make sure MS-Access, OpenOffice (Base) or related report/app dev solutions couldn’t do the trick. I use Oracle’s free APEX software quite a bit myself, it comes with their free database Oracle XE just download it from their site.

5. FYI – Reporting insight: for large databases, you typically have two database instances a) transaction database for recording each detailed record. b) reporting database (data mart/data warehouse) housed on a separate machine. For more information search google both Star Schema and Snowflake Schema.

Regards.

2 people think this answer is useful

I want to address only the concern that joining to mutiple tables will casue a performance hit. Do not be afraid to normalize because you will have to do joins. Joins are normal and expected in relational datbases and they are designed to handle them well. You will need to set PK/FK relationships (for data integrity, this is important to consider in designing) but in many databases FKs are not automatically indexed. Since they wil be used in the joins, you will definitelty want to start by indexing the FKS. PKs generally get an index on creation as they have to be unique. It is true that datawarehouse design reduces the number of joins, but usually one doesn’t get to the point of data warehousing until one has millions of records needed to be accessed in one report. Even then almost all data warehouses start with a transactional database to collect the data in real time and then data is moved to the warehouse on a schedule (nightly or monthly or whatever the business need is). So this is a good start even if you need to design a data warehouse later to improve report performance.

I must say your design is impressive for a first year CS student.

1 people think this answer is useful

It isn’t over-engineered, this is how I would approach the problem. Joining is fine, there won’t be much of a performance hit (it’s completely necessary unless you de-normalise the database out which isn’t recommended!). For statuses, see if you can use an enum datatype instead to optimise that table out.

1 people think this answer is useful

I’ve worked in the training / school domain and I thought I’d point out that there’s generally a M:1 relationship between what you call “sessions” (instances of a given course) and the course itself. In other words, your catalog offers the course (“Spanish 101” or whatever), but you might have two different instances of it during a single semester (Tu-Th taught by Smith, Wed-Fri taught by Jones).

Other than that, it looks like a good start. I bet you’ll find that the client domain (graphs leading to “clients”) is more complex than you’ve modeled, but don’t go overboard with that until you’ve got some real data to guide you.