Primary Key and Foreign Key
Chapter 5: SQL Joins and Keys
In relational databases, joins are used to combine rows from two or more tables based on a related column between them. There are several types of joins, each designed for specific purposes. Joins are a way to combine data from multiple tables to create a single result set.
Types of Joins
There are several types of joins, each designed for specific purposes. Here are some common types of joins:
- Inner Join: Used to combine rows from two tables based on a related column between them. It returns only the rows that have matching values in both tables.
Only rows that have matching values in both tables are returned.
- Left Join: Used to combine rows from two tables based on a related column between them. It returns all rows from the left table, and the matching rows from the right table.
If there are no matching rows in the right table, NULL values are returned for the columns from the right table.
- Right Join: Used to combine rows from two tables based on a related column between them. It returns all rows from the right table, and the matching rows from the left table.
If there are no matching rows in the left table, NULL values are returned for the columns from the left table.
- Full Join: Used to combine rows from two tables based on a related column between them. It returns all rows from both tables, and the matching rows from both tables.
If there are no matching rows in either table, NULL values are returned for the columns from the other table.
- Cross Join: Used to combine rows from two tables based on a related column between them. It returns all possible combinations of rows from both tables.
If there are no matching rows in either table, NULL values are returned for the columns from the other table.
Practice Exercise
Code setup in SQL
We will create 4 tables for a chai store so that we can practice joins. The tables will be:
- customers
- chai_store
- orders
- order_items
customers table
Let’s add some sample data to the customers table:
chai_store table
Let’s add some sample data to the chai_store table:
orders table
Let’s add some sample data to the orders table:
order_items table
Let’s add some sample data to the order_items table:
This is our sample setup for the chai store database. This consists of four tables: customers, chai_store, orders, and order_items. Tables are storing data about customers, chai varieties, orders, and the items in each order. Tables are linked together using foreign keys.
Primary keys are used to uniquely identify each row in a table. Foreign keys are used to establish relationships between tables. Foreign keys are used to link tables based on a related column between them.
Summary
In this chapter, we have learned about the basics of SQL Joins. Also we have learned about Primary Key and Foreign Key. By the end of this chapter, you should have a good understanding of how to use SQL Joins and Keys to manage and manipulate relational databases effectively.