4

The file which contains the day to day activities of each employee. And it shows the detailed description of employee what they done at work time. So these files are prepared by employee.

Now, my requirement is how to design a database for this model. I know only the basics of databases and yet I didn't design any databases. So any one help me to solve how to create a database design for the application.

The design should also work for the query to get details about individual employee like

For Ex:

  1. Total time spent by the shiva 'shiva' on CCTNS project

  2. To find the team leader for the specified project

  3. To find the members in a particular project

0

1 Answer 1

7

I would suggest two fundamental tables for this purpose, if you're creating this all from scratch. This assumes that this is being created in PostgreSQL.

An employee table containing their name and whatever other details are important for your application, with a numeric primary key. The primary key is important because you may have more than one employee with the same name, and you may have employees that change names in the future. Using a key allows the information about their time to remain consistent.

An activity log table containing:

  • a primary key column (type SERIAL is likely fine for this)
  • an employee ID column (this will reference the column in the other table)
  • a project column (you can make this free-text, or create another table with a primary key and reference it here), this column should be nullable to reflect activity log entries that aren't related to a project (eg tea break)
  • a tstzrange column to represent the period for the log entry

Now this schema doesn't contain enough information to identify a team leader. You could add this information to another table, referencing your employee (and possibly project) table.

Here's a quick example schema you can play with:

 CREATE TABLE employees (
     employee_id SERIAL NOT NULL,
     employee_name text NOT NULL,
     PRIMARY KEY (employee_id)
 );

 CREATE TABLE projects (
     project_id SERIAL NOT NULL,
     project_name text NOT NULL,
     PRIMARY KEY (project_id)
 );

 CREATE TABLE project_memberships (
     employee_id integer NOT NULL REFERENCES employees,
     project_id integer NOT NULL REFERENCES projects,
     supervisor_employee_id integer REFERENCES employees,
     PRIMARY KEY (employee_id, project_id)
 );

 CREATE TABLE activity_logs (
     activity_log_id SERIAL NOT NULL,
     employee_id integer NOT NULL REFERENCES employees,
     project_id integer,
     activity_time tstzrange NOT NULL,
     activity_notes text,
     PRIMARY KEY (activity_log_id)
 );
 ALTER TABLE ONLY activity_logs
     ADD CONSTRAINT activity_logs_project_member_fkey FOREIGN KEY (employee_id, project_id) REFERENCES project_memberships(employee_id, project_id);
4
  • Here, How do I differentiate the phases for a particular project. For ex: if user john spend 3 hours in a project on SRS phase and also he spent 2 hours for another project on pseudo coding phase. So, how do I differentiate that two for requirement like "Find the total number of hours used by john for project A on SRS phase."
    – mohangraj
    Commented Jan 31, 2016 at 19:53
  • You can create an additional table if the phases of the project are strict (eg they have a specific order or particular legally-significant labels, and you want to enforce that) or you can simply add a free-form text column in the activity_logs table that can be filled in however is relevant. It depends on your application. Commented Jan 31, 2016 at 19:57
  • supervisor_employee_id is also a foreign key to employees, and may be better in projects. Commented Jan 31, 2016 at 20:57
  • @MichaelGreen Correct, thanks for pointing out the missing FK. Of course, depending on how you define "supervisor", you may be right. My real-life work, on the other hand, uses a hierarchy of supervisors per project, and my original thought in including it was to FK back to project_memberships itself. I didn't really flesh it out all the way, it was a quick jot to get a concept across. Commented Jan 31, 2016 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.