Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key is needed for inner join in SQL?

INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY)
VALUES(3, 'sin', 21, 'bangalore', 10000);

INSERT INTO orders (orderid, orderno)
VALUES (3, 21);

Here ID is the primary key in the customer table, orderid is the primary key in the orders table.

I would like to know whether it is mandatory to add id as foreign key in orders table for performing SQL join?

like image 729
Sinoj Raphel Avatar asked Jun 30 '26 02:06

Sinoj Raphel


2 Answers

It is not necessary to establish a foreign key in order to perform an inner join of the customers and orders table. However, the question arises of what the significance of such a join operation would be without a foreign key.

Presumably the goal is to model some sort of relationship between customers and orders. Assuming the attributes listed comprise all of the attributes in the two tables, there is nothing establishing a relationship between customers and orders in the way the tables are defined. Adding a customerID field as a foreign key in the orders table would establish that relationship. Then, an inner join on the condition customers.ID = orders.customerID would associate the order information with the appropriate customer's information in the joined table.

like image 61
Noah Sherrill Avatar answered Jul 01 '26 16:07

Noah Sherrill


My thought here that when you asking whether FK is needed or not, you accept by default that both working within the same domain, and this is not a valid assumption, in brief

The inner join is used with the reading (query) of data, whereas FK exists to maintain the integrity of data during a different kind of operations such as insert, update and delete.


In my opinion, the correct answer should be that both FK and inner join are not relevant.

I will use the below tables to explain the difference between both

Customers 
| id          | name     | 
|:------------|---------:|
| 1           |  Gabriel |
| 2           |  John    | 
| 3           |  Smith   | 


Orders 
| order_id   |customer_id| 
|:-----------|---------: |
| 1          |  1        |
| 2          |  1        | 
| 3          |  1        | 
| 4          |  2        | 

Inner Join,

is used during query for example according to the above, let's say you want to query all orders made by a specific customer.

select * from customers, orders
where customer.id=orders.customer_id
and customer.id=2

according to our tables, you will end up with one order number (4) for the customer (John)

Foreign Key

While the inner join used during the query, the Foreign Key is used to apply policies over the major DML operations (Insert, update and delete).

below are examples of operations that will fail for violating the FK constraint

Insert into orders(order_id,customer_id) values(5,7)

this operation will fail because no customer exists with id (7) in the Customer table, the same will apply for the update operation.

Also if the FK enabled the On delete cascade or On update cascade this will delete or update child rows when trying to delete or update the master table, example deleting the customer Gabriel will delete orders 1,2 and 3.

like image 28
Feras Avatar answered Jul 01 '26 16:07

Feras