Vocabulary:
It really works like an Excel file...
Unique ID: every record on a table of a database will have a unique ID. The first record created will have the ID 1, the second one 2 etc... And ID 1 will ALWAYS be assigned to this first record. IDs are unique and can never be assigned to another record. Even if you delete the first record, ID 1 will disapear but never be re-assigned to a new record. This unique ID is also called primary key. Databases (like PostgreSQL) will always create and manage primary keys automatically for you.
1 to Many relation (1:n): for example a "City" / "Inhabitant" relation is a 1 to many relation:
May to Many relation (n:n): for example a "Doctor" / "Patient" relation is a many to many relation:
SQL = Structured Query Language. It is the language of databases.
Give me all patient first names and last names
Give me all patient attributes (will select the whole table)
Give me all patients of age 21
Give me all surgery doctors
Give me all cardiac surgery doctors named Steve
Give me all cardiologists or dentists
Give me all patients ordered by age
How many doctors do I have?
Count all doctors per specialty
Join: give me all the inhabitants from Paris
To query data from more than one table I need to create a join between my tables and specify the nature of this join. Here I'm saying line by line:
Once you joined 2 tables, in all the following SQL queries you will need to specify what table you are working on. For example we could add to the previous query: AND inhabitants.age >= 18
When you have a n:n relation, you need to join both tables to the join table
CRUD with SQL:
First we will need to create a table:
CREATE
READ
UPDATE
DELETE