Table Relationships
Explore how to create and manage table relationships in SQL using foreign key constraints. Learn to distinguish between one-to-one, one-to-many, and many-to-many relationships to efficiently query multiple tables and maintain data integrity.
We'll cover the following...
A relationship is when one column of one table references another column in a different table. For example, we can have a table called Orders that contains a column ItemId that references the Id column of the Items table.
A table that references another table is called a child table. The table that is being referenced is called a parent table. In our example above, Items is the parent table referenced by the Orders table.
Foreign keys
To create a relationship, we use the concept of foreign keys, a term used to describe a column or a combination of columns from the table we would like to reference. Foreign keys are often (but are not always) the primary keys of tables we want to reference. For example, in the diagram above, the ItemId column is a foreign key that references the Items ...