Hi,
The architecture you described does not fall into the common relational or
object-oriented model if you want to use 1 table. And in fact, I would recomend
that you review your design because it is very likely that something is really
wrong with it. Here are some ideas for you to consider.
If I get it right, the problem behind the problem is that you want to store
employee properties which structure is dynamic, i.e. may be different for
different employees.
1. Analyze whether all your employees fall into a limited set of "classes". If
yes, you can design a table per each class and link them to some "master" table.
The details can be found here: http://www.chimu.com/publications/objectRelational
2.If there are no stable set of classes OR you want additional flexibility OR
employees can "change" their classes dynamically, consider storing properties of
employees in a separate table. You'll need one master table, which stores employee
ID's and those properties that are guaranteed to be common to all employees (e.g.
Name). Then add another table called "employee_properties" with the structure like
this:
create table employee_properties (
employee_id INT NOT NULL REFERENCES employee,
property INT NOT NULL,
value VARCHAR(255),
PRIMARY KEY (employee_id, value)
)
the fact that an employee with id I has a property with id P and with value V is
represented by presence of the following record in the employee_properties table:
(I, P, V).
To query employees you'll need to use an extra join per each property you
interrogate or display:
SELECT p1.value salary
FROM
employee e, -- employees
employee_properties p1, -- "salary" property, id 111
employee_properties p2 -- "age" property, id 222
WHERE p1.employee_id=e.id AND p1.property=111
AND p2.employee_id=e.id AND p2.property=222 AND p2.value > 35