Unix Technical Forum

How do I make sure that an employee and supervisor belong to the same company?

This is a discussion on How do I make sure that an employee and supervisor belong to the same company? within the Pgsql General forums, part of the PostgreSQL category; --> I have an employees table and one column in the employees table is "supervisor_id" which is an FK to ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-15-2008, 09:34 PM
Matthew Wilson
 
Posts: n/a
Default How do I make sure that an employee and supervisor belong to the same company?

I have an employees table and one column in the employees table is
"supervisor_id" which is an FK to the id column.

I have employees from numerous companies all in the same table. I have
a column called company_id that indicates the company.

I want to make sure that an employee chooses a supervisor from the same
company. I have a column called company_ID. How do I make sure that
the employee company ID matches the supervisor's company ID?

Do I need to use a trigger or is there a way I can do this with foreign
keys?

TIA

Matt


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-15-2008, 09:34 PM
Kev
 
Posts: n/a
Default Re: How do I make sure that an employee and supervisor belong to thesame company?

When presenting the list of supervisors, you'd write something like
this:

select * from supervisors where supervisors.company_id = ?

....and then pass in the current employee's company_id value. If you
don't have that on hand you could do:

select * from supervisors where supervisors.company_id = (select
company_id from employees where employee_id = ?)

....passing the current employee's id (or whatever you called it).

What does this choice mean, though? If you're saying the employee's
supervisor_id is being filled in by each employee, and you want to
make sure at the database end that they pick a supervisor from their
company, you could add a check constraint like

ALTER TABLE employees
ADD CONSTRAINT "supervisor_from_company_of_employee" CHECK
(reasonable_supervisor(employee_id));

....assuming of course that you already had some function
reasonable_supervisor() that would look something like:

CREATE OR REPLACE FUNCTION reasonable_supervisor(emp integer)
RETURNS boolean AS
$BODY$
select (company_id = (select company_id from supervisors where
supervisors.supervisor_id = employees.supervisor_id)) from employees
where employee_id = $1;

$BODY$
LANGUAGE 'sql' VOLATILE

Hope that helps,
Kev

On Apr 14, 1:36 pm, m...@tplus1.com (Matthew Wilson) wrote:
> I have an employees table and one column in the employees table is
> "supervisor_id" which is an FK to the id column.
>
> I have employees from numerous companies all in the same table. I have
> a column called company_id that indicates the company.
>
> I want to make sure that an employee chooses a supervisor from the same
> company. I have a column called company_ID. How do I make sure that
> the employee company ID matches the supervisor's company ID?
>
> Do I need to use a trigger or is there a way I can do this with foreign
> keys?
>
> TIA
>
> Matt
>
> --
> Sent via pgsql-general mailing list (pgsql-gene...@postgresql.org)
> To make changes to your subscription:http://www.postgresql.org/mailpref/pgsql-general


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:58 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com