Pregunta

Problem Statement To Solve:

  1. You need to store employee information (name, age, gender and address) and Project information (name, client name, client city) in database. Employees can work on multiple projects and a project can have multiple employees. Some employee might not be on any project. Please create table structure for this and necessary constraints, don’t use create table query just structure is important and show primary key and data type of each column.
  2. For above scenario write a query for following case:- To get list of projects, For each project the details needed in the list are-project name, client name, no. of employee working on project.

I little bit understand that there is problem in table 1 that is Project table because it's not normalized

¿Fue útil?

Solución 2

employee

id|name| age| gender| address

client

id|name|city

project

id| name| client_id(FK client.id)

project_employee_mapping

employee_id (FK employee.id) | project_id (FK project.id)

Query:

SELECT p.name, c.client_name, COUNT(pem.employee_id)
FROM project p, project_employee_mapping pem, client c
WHERE pem.project_id = p.id
AND c.id = p.client_id
GROUP BY p.id

Otros consejos

Can’t make much suggestions about constraints or keys, but definitely Employee and Projects tables need to be primary keys as Employee ID and Project ID respectively, the gender in employees table could be enum.
Project_employees table is suggested to have a primary key as well, so it can uniquely identify a record in case there are multiple entries of same project or employee ids. That way there won’t be a need of using both of these columns in criteria.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top