How can I relate the two tables?-VBForums
Results 1 to 4 of 4

Thread: How can I relate the two tables?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Question How can I relate the two tables?

    Hi I'm using sql express,and I have two tables Employer and Employee,how can I relate the two tables so to differentiate between who works for who?

    Employee table has EmployerID as a field and assigned both F_K and P_K to it .
    Employer table also has EmployerID field and has only P_K assigned to it.
    how can I relate the two tables ?

  2. #2
    PowerPoster
    Join Date
    May 2002
    Posts
    25,739

    Re: How can I relate the two tables?

    Ummm... either it's been designed wrong, or you're description of it is wrong... or I'm reading it wrong...

    You should have an Employer table... it should have an ID field ... and that field should be marked as the Primary Key ...
    You should also have an Employee table, it should also have an ID field, and it too should be marked as the Primary Key... that SAME table should also have ANOTHER field, EmployerID ... which should then be marked as a Foreign Key to the ID of the Employer table...

    That foreign key then makes the two tables related... to pull employees and their employers, you simply join them in the select...

    select * from Employee
    inner join Employer on Employee.EmployerID = Employer.ID

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: How can I relate the two tables?

    I have Employee table IDEmployer as a field and assigned Foreign Key to it(and Allow Null :Checked) .
    I hope this is right.(as shown in the screenshot)
    Name:  F_K Employee.jpg
Views: 56
Size:  64.9 KB
    Now I just have to use the select statement,and write the select code in form load,and show the data in datagridview...is that possible?
    Last edited by chdboy; Feb 13th, 2013 at 09:36 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: How can I relate the two tables?

    Now this works as my datagrid shows two table columns one after another ,but with no value inside the tables?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.