1

I am trying to determine how the tables need to be linked in which ways.

The employees tables is directly linked to a number of tables which provide more information. A few of those tables have even more details.

  • Employees have a unique employeeid but I understand best practice is to still have a id?
  • Customers have a unique customerid
  • Employees have a manager
  • Managers are employees
  • Customers have a manager associated with them. manager associated with them
  • Employees may have a academic, certification and/or professional information.

With all of this said what would be the best recommendation for creating primary and foreign keys? Is there a better way to handle the design?

Version2


EDIT

Updated diagram to reflect feedback thus far. See comments to understand changes taking place.

v3schema

Astron
  • 1,211
  • 5
  • 20
  • 42
  • your diagram still needs (lots of) work before going towards any kind of implementation. With progress so far, I would suggest that you focus now into small segments of it and work out each one. You can go with a separate question for each smaller part of the problem and then simply glue it alt together. – Damir Sudarevic Jul 31 '11 at 13:07
  • For example, I would focus on Party, Customer, Employee, Manager first. But before you post another question -- considering that your keyes still do not propagate properly -- read-up on super-type/subtype pattern (category) Here is a link to check out http://stackoverflow.com/search?q=user%3A196713+subtype – Damir Sudarevic Jul 31 '11 at 13:08
  • I agree, definitely not ready for any type of implementation. I am going to post another question in order to figure out which tables need to have a primary key associated with them and which do not. – Astron Jul 31 '11 at 13:39

2 Answers2

2

Employees have unique employeeID but I understand best practice is to still have a id?

No. (But keep reading.) You need an id number in two cases: 1) when you don't have any other way to identify an entity, and 2) when you're trying to improve join performance. And that doesn't always work. ID numbers always require more joins, but when the critical information is carried in human-readable codes or in natural keys, you don't need a join to get to it. Also, routine use of ID numbers often introduces data integrity problems, because unique constraints on natural keys are often omitted. For example, USPS postal codes for state names are unique, but tables that use ID numbers instead of USPS postal codes often omit the unique constraint on that two-character column. In your case, you need a unique constraint on employee number regardless. (But keep reading.)

Employees have a manager.

Does the table "team" implement this requirement? If the "manager" table identifies managers, then your other manager columns should reference "manager". (In this diagram, that's customers, team, and customer_orders.)

Managers are employees.

And that information is stored in the table "manager"?

Customers have a manager associated with them.

And so does each order. Did you mean to do that?

Employees may have a academic, certification and/or professional information.

But apparently you can't record any of that information unless you store a skill first. That doesn't sound like a good approach. Give it some more thought.


Whenever you design tables with overlapping predicates (overlapping meanings), you need to stop and sit on your hands for a few minutes. In this case, the predicates of the tables "employees" and "customers" overlap.

If employees can be customers, which is the case for almost every business, then you have set the stage for update anomalies. An employee's last name changes. Clearly, you must update "employees". But do you have to update "customers" too? How do you know? You can't really tell, because those two tables have independent id numbers.

An informal rule of thumb: if any real-world entity has more than one primary key identifying it in your database, you have a problem. In this case, an employee who is also a customer would have two independent primary keys identifying that person--an employee id and a customer id.

Consider adding a table of persons, some of whom will be employees, and some of whom will be customers. If your database is well-designed and useful, I'll bet that later some of the persons will be prospects, some will be job applicants, and so on. You will need an id number for persons, because in the most general case all you can count on knowing is their name.

At some point, you'll have to take your database design knowledge to the next level. For an early start, read this article on people's names.

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
  • Very good points and very insightful. In this case the customers and employees do have unique identifies. Say customerid is **c0001** and employeeid is **e0001**. Everything else could just use an auto incrementing number. I have updated the diagram. Take a look and let me know what you think. – Astron Jul 30 '11 at 15:17
  • Two independent, unique identifiers in two independent tables identifying one person is a problem, not a solution. Maybe I didn't make that clear. Think about putting all persons in one table, not in two. – Mike Sherrill 'Cat Recall' Jul 30 '11 at 15:39
  • You were very clear about the unique identifiers. Employees have their own unique id provided by the company. An external customer does not have a unique identifier created by the company so one is made up for them hence the reason for have them in separate tables. In thinking ahead I do not want to use the same format of data as a new employee may be assigned an employeeid of an existing customerid that we made up just to make it work. Are you suggesting have a **people** table with an **employee** and **customer** table reference? Thanks – Astron Jul 30 '11 at 16:18
  • 1
    Yes. Attributes common to all people (names and such) go in the table of people; attributes unique to employees (employee id numbers) go in the table of employees; attributes unique to customers go in the table of customers. "Parties" is more general still; all organizations and persons are parties. Addresses reference parties, phone numbers reference parties, and so on. (Because both organizations and persons have addresses, phone numbers, and so on.) – Mike Sherrill 'Cat Recall' Jul 30 '11 at 16:51
  • Thanks for the help. I have updated the diagram and created a general (global in my mind) **party** table and respective id. Will this party allow me to associate employees and managers with customers since they are now separate or do I have to perform some type of other association? – Astron Jul 30 '11 at 20:47
  • 1
    *Global* is a good term in this case. Details depend on whether you need an exclusive arc. Among parties, "Person" and "Organization" are exclusive; every party is either one or the other, not both. This SO answer has source code for parties: http://stackoverflow.com/questions/5466163/same-data-from-different-entities-in-database-best-practice-phone-numbers-exa/5471265#5471265 You can associate pretty much anything with anything using foreign key relationships with this kind of structure. – Mike Sherrill 'Cat Recall' Jul 31 '11 at 09:38
2

Though your question is sensible, before you go any further in design, I would suggest for you to spend some time understanding relationships, foreign keys and how they propagate through relationships.

The diagram is utterly wrong. It will help it you start naming primary keys with full name, TableNameID, like EmployeeID; then it will become obvious how keys propagate through relationships. If you had full names you would have noticed that all your arrows are pointing in the wrong direction; parent and child are reversed. Anyway, takes some practice. So I would suggest that you rework the diagram and post the new version, so that we can comment on that one. It should start looking something like this (just a small segment)

enter image description here


EDIT

This is supposed to point you to towards the next step. See if you can read description (specification) and follow the diagram at the same time.

  • Each employee has one manager, a manager manages many employees.
  • A manager is an employee.
  • Each customer is managed by an employee who acts as an account manager for that customer.
  • Account manages for a customer may change over time.
  • Each employee is a member of one team, each team has many employees.
  • Employee performance for each employee is tracked over time.
  • Employee may have many credentials, each credential belongs to one employee only.
  • Credential is academic or professional.

enter image description here

Damir Sudarevic
  • 21,891
  • 3
  • 47
  • 71
  • Thanks for the example. I think I may be getting the hang of it. Feel free to check out the updated question and diagram and provide any more feedback. – Astron Jul 30 '11 at 15:18
  • 1
    @Astron; Well, in small steps. The category symbol is used for **IS** type relationships -- like `Manger IS Employee`. Well, customer order is not an employee, nor is billing. You may not have noticed, but when you use category symbol, the primary ID of the super-type table is supposed to propagate into all sub-types. I will post an update to my answer which is supposed to nudge you towards next step. – Damir Sudarevic Jul 30 '11 at 15:45
  • Great information. Why does your **Employee** table loop back to itself? Does every table have to have a unique id? For example the **manager** table in my updated diagram does but the *reporting** table does not. In regards to the manager, since there is now a global **partyid**, do I still have to reference a specific **managerid** and **employeeid** for my customer table or does the **partyid** suffice? – Astron Jul 30 '11 at 20:43
  • @Astron; Self-referential relationship for managers. Each employee has a link to his/her manager (who is also an employee). – Damir Sudarevic Jul 31 '11 at 12:56
  • Is that why there is no ManagerID PK? – Astron Jul 31 '11 at 13:33
  • 1
    @Astron; Manager is an employee, so he has an `EmpoyeeID`. Key `EmployeeID` propagates into `Manager` table. The `Manager` table has only fields specific to managers, which other employees do not have. – Damir Sudarevic Jul 31 '11 at 14:37
  • Thanks, I've started a new post http://stackoverflow.com/questions/6890133/implmenting-super-type-subtype-correctly-in-mysql to further elaborate. – Astron Jul 31 '11 at 14:54