Skip to main content
September 25, 2023
Question

Checking if record already exists

  • September 25, 2023
  • 3 replies
  • 0 views

Hi all,

I'm trying to see what is the best way to check if a record already exists in the DB, specifically in a relationship table.

I have 3 tables: manager, employess, managers_employess_relationship

each manager can have multiple employees.

Manager table :

ID Name

1   Juan

Employees Table

ID Name

4   Liza

5  Emily

managers_employess_relationship table ( has the foreign keys from both tables)

ID   Manager_Id    Employee_Id

1         1                        4

2          1                        5

when a manager is adding its employees in a form what is the best way to check if its adding an already assigned employee, for example if manager juan tried to add Liza again, I want to make sure it gives him some type of validation saying that liza is already assigned to him. to avoid having duplicates in the managers_employess_relationship table.

what do you all recommend?

thanks in advance

                 

3 replies

mikes0011
Brainy
September 25, 2023
if manager juan tried to add Liza again, I want to make sure it gives him some type of validation saying that liza is already assigned to him

When adding assignees, I assume you're choosing assignees who were already added to the appropriate assignee table.  When adding to the manager, you can do a simple query to see whether there's already a relationship between that manager and employee, and use that to trigger a validation.

September 25, 2023

can you give me more of an example of this? because what I'm thinking is checking the managers_employess_relationship table right?

mikes0011
Brainy
September 25, 2023

Yes, you'd query for an active entry at that table with the current manager's ID and the current employee's ID.  If your query results in more than 0 rows (preferably there would only be 1 row but we'd usually say "more than zero" just to be safe), display a validation message.