mysqlsqldatabasenatural-join

What will happen if I natural join both these tables?


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?


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