Results 1 to 4 of 4

Thread: How can I relate the two tables?

  1. #1
    Lively Member
    Join Date
    Sep 12
    Posts
    99

    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 techgnome's Avatar
    Join Date
    May 02
    Posts
    21,659

    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 subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * 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??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    Lively Member
    Join Date
    Sep 12
    Posts
    99

    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: 27
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 08:36 AM.

  4. #4
    Lively Member
    Join Date
    Sep 12
    Posts
    99

    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
  •