Skip to content

Instantly share code, notes, and snippets.

@rdmershon
Created June 10, 2025 01:21
Show Gist options
  • Select an option

  • Save rdmershon/819fa7f5a65c5905f99e808cc5bdd1d1 to your computer and use it in GitHub Desktop.

Select an option

Save rdmershon/819fa7f5a65c5905f99e808cc5bdd1d1 to your computer and use it in GitHub Desktop.
SQL I can't remember, relationships between two tables pt.2
Foreign keys are used to establish relationships between tables. Typically, we use a primary key in one table and a foreign key in another table to create a one-to-one or a one-to-many relationship between those two tables.
In a one-to-one relationship, one table serves as a parent table and the other table serves as a child table. With a foreign key constraint, a record must exist in the parent table before a related record can be added to the child table. In other words, a record in the child table must have a related record in the parent table.
@rdmershon
Copy link
Author

Foreign keys are referred to as FK when referring to the foreign key in the primary table. ex:

Employee
employee_id
name
email
department_id (FK)

Department
department_id
name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment