Modeling Employee/ Team Leader relationship - sql

Consider the follwing tables:
EMP
PK: EMP_ID
PROJECT
PK: PROJECT_ID
EMP_PROJECT ( Connection table to creat M:N relation)
PK: EMP_ID, PROJECT_ID
FK: EMP_ID, PROJECT_ID
Employee can work on many projects. Each Project can be done by many employees as well. I want to represent in my model the fact that the team ( group of employees) handling a project must have a team leader.
I thought of creating a new table Supervision (PK: Supervision_ID) with an attribute for the start date. But I am not sure to wich table must this table relate.

I think the draft model will help you:

Related

How to implement ER Relationships :One to one, One To Many, Many to Many in Oracle?

Perhaps, this could seem like the basics of database design.But,certainly i find these concepts a lil tricky in the sense that how they relate to each other.
Theory as per my understanding.
One to One :
When each particular entity has one record.
One to Many :
When each particular entity has many record
Many to Many :
Multiple entities have multiple records.
As per the above if i could relate an example as
ONE TO ONE
Each employee having a unique passport number.
Table Employee
Empid(pk)
empname
passpordid(fk to passport)
Table passport
passportid(pk)
passportno
ONE TO MANY
An organisation having multiple employees
TABLE ORGANISATION
ORGID (PK)
ORGNAME
EMPID (FK TO EMPLOYEE)
TABLE EMPLOYEE
EMPID (PK)
EMPNAME
SALARY
This is the part that i want to know more that is many to many. I mean if we see one to many here. As a whole it could be said as many to many as many organisations having many employees but does that mean the whole relationship is many to many not one to many or one to many is a subset of many to many in the above example.
I wanna know the difference mainly between one to many and many to many both theoritically and by implementation.
An example of a many-to-many relationship would be EMPLOYEES and SKILLS, where SKILLS are things like "SQL", "Javascript", "Management" etc. An employee may have many skills (e.g. may know SQL and Javascript), and a particular skill by be possessed by many employees (e.g. Jack and Jill both know SQL).
In a database like Oracle, you need a third table to express the many-to-many relationship between EMPLOYEES and SKILLS:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
Note that this third table has a foreign key to both of the other tables.
The table can also hold further information about the relationship - for example:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, rating number
, date_certified date
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
This is more a theory question that a programming one, but why not.
You could imagine a "many to many" as 1 table being a list of employees, and another table being a list of products sold.
Each employee is likely to "handle" more than 1 product, and each product could very well be handled by multiple employees, and it's quite possible that products A and B can be handled by, at the same time, employees C and D.
You seem to have the basic idea of one-to-one and one-to many down, in theory.
One-to-one: each item A has a separate and unique item B
In a database, you could include the new data in the same table. You could put it in a separate table, and enforce the one-to-one constraint by making the id into the remote table "unique" (indexing constraint), forcing the id for item B to not be repeated in the table. In your example, this would be a constraint added to the passportid.
One to many: each item A has some number of item Bs
In a database, you put the key in the table on the "many" side. So many Bs would have the same foreign key into table A. In your example, put an orgid as a foireign key into the employee table. Many employees can then point to a single organization, and an employee can belong to one organization.
Many to many: some number of As can be linked to any number of Bs and vice-versa
In a database, this is usually implemented as a link table, having just the IDs from both A and B. Following you example, consider that an employee can be part of multiple groups (or projects), multitasking across those groups. You could have a group table:
GROUPID (PK)
GROUPNAME
And the link table would then look like
LINKID (PK)
EMPID (FK TO EMPLOYEE)
GROUPID (FK TO GROUP)
This lets any employee be part of any number of groups, and groups to have any number of employees, AKA many-to-many.
A side comment:
Technically, you don't HAVE to have a LINKID in the link table. But it makes it easier to manage the data in that table, and is generally considered "best practice".

SQL relationship between student, lessons and attendance

For a school project I'm trying to store students, their attendance and which online lessons they are allowed to view.
For example a yellow belt student is allowed to watch white and yellow belt lessons. The belt system is replaced by a kyu, which can also have a dan.
So far I've created the following:
Student: studentID (PK), firstname, lastname, age, kyu, dan
Attendance: attendanceID (PK), studentID (FK), date
Lesson: lessonID(PK), name, kyu, dan
I'm stuck at finding a possible relationship between lesson and student, since it feels wrong to just put a FK to lesson in student.
As far as I'm aware, it's just one big class, so I can't form a relationship between student-class and class-lesson.
I understand that you have a N-M relationship between lessons and students, where each student can enroll in several lessons, and each lesson may be (and hopefully is) taken by many students.
The typical way to represent such relationship is to create a bridge table, that stores which student participates which lesson. Say table student_lesson :
id : primary key
lessonID : foreign key towards table lesson
studentID : foreign key towards table student
You would need to create a unique constraint on columns (lessonID, studentID) - or drop skip id, and make (lessonID, studentID) the primary key of the bridge table.
As far as I understood, it should be many to many relationship, as many students can go to the same lesson, and student can have many leasons, so another table holding student and leason id will solve the problem, I think

Many to many relationship with several tables

I have the following tables
Company:
Id,Name
Person:
Id,Name
A company can have one or more directors. A director can be either another company or a person.
To link them I have a table Director:
Director: Id,CompanyId,DirectorCompanyId,PersonId
where if a company is a director DirectorCompanyId has a value and PersonId is null or if a Person is a director PersonId has a value and DirectorCompanyId is null
But I feel like this is not a correct design.
You're right, it's not a correct design. To decompose a M:M relationship into two 1:M relationships you need a third table:
CompanyPerson
--these columns are vital to decompose the many:many relationship
--the PK of this table should be a compound of these two columns
--so that the same person cannot twice work for the same company
--with different roles etc
PersonID -> FK to Person.ID
CompanyID -> FK to Company.ID
--plus other properties like:
RoleID -> FK to Role table --if roles are a defined set of options
StartDate -> --when the person began this employment
ManagerPersonId -> --the person's mananger.. etc
PersonID + CompanyID is the composite primary key for this table
It maps people to companies and the role they have at each. Can also have other info like start date, manager at that Company etc.. (might also need to make start date part of primary key if a person will ever leave and come back to the same co, and you wanted to recycle the PersonID)
Note: You could call this table Employee, because that's effectively what the people inside it are, but I tend to find it more helpful that these middle-man tables that create associations between two other tables are better off called Table1Table2 because you can more clearly see/understand the relationship/purpose of the table than if it's called something more abstract like Employee
The following design seems to be corresponding to demands
Another option is to use inheritance:
Director <-- CompanyDirector
<-- PersonDirector

SQL create two tables

I have to create two tables in SQL one with employees and one with their projects. The problem is: one employee can work to one or more projects and on a project can work one or more employees and I don't know how to create these 2 tables and how to put the foreign keys. I guess the primary keys are the IDs.
This is a many-to-many relationship. You need three tables: Employee, Project, and EmployeeProject. EmployeeProject would have two columns EmployeeId and ProjectId. Its sole purpose would be to represent the relationship between Employee and Project.
I think you should create third table which is link employee to projects
like below:
Project Table:
ID ProjName Date ...
Employee Table:
ID EmpName ....
Link Table:
ID EmpID ProjID

Primary-, foreign keys and relationship model

For my homework, we need to create a sql database model on paper, but I'm stuck at the following subject:
We have different people, with some data assigned to them:
Person table:
person_id (Primary key)
person_name
person_number
.....
Now we have to create a talent table, for every person has some kind of talent (IT, Art, singing, ...) and it has to be linked somehow. The catch here is that a person can have multiple talents(IT and art together).
What's the best way to create this relationship between those 2 tables?
Create another table person_talent with
person_id
talent_id
Talent_table
1 talent_id talent_name
2 talent_id talent_name
3
4
.........
Person_table
person_id
...........
whenever person adds new talent insert talent_id and person_id into talent_person table
and delete the record whenever person removes that talent.
Hope it helps