-
Feb 13th, 2013, 08:49 AM
#1
Thread Starter
Lively Member
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 ?
-
Feb 13th, 2013, 08:56 AM
#2
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
-
Feb 13th, 2013, 09:22 AM
#3
Thread Starter
Lively Member
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)
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.
-
Feb 13th, 2013, 09:31 AM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|