Question

I have two tables. Employee and Manager. Employee table stores the information of all the employees of the company. Manager table shows which employees work under which manager. Manager is also an employee of the company. The tables are given below.

employee(name, city, street)

manager(name, employee_name)

Bold means they are the primary key. Now the thing is that, in the manager table, both name and employee name is a foreign key referencing employee. So what will happen if I natural join them. Wouldn't there be any problem? After natural joining them, whose city and street will I see in the final table? manager's or employee's?

Was it helpful?

Solution

You'd most likely nothing if you're lucky, or junk if you're unlucky. Natural join would do something like this:

SELECT * FROM managers LEFT JOIN employees
         WHERE managers.name = employees.name

Natural join attempts to use COMMONLY NAMED COLUMNS.

When what I assume you want something more like this:

SELECT * FROM managers LEFT JOIN employees
         WHERE managers.employee_name = employees.name

OTHER TIPS

You will get a list of all the managers city, street, and employee_name

Name, City, Street, Employee_Name

Otherwise the join stuff, I suggest to review the arquitecture of your DB. If in the manager table, the name is PK, you can't have more then one employee with the same manager..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top