Tables, keys and relationships · Tables, clés et relations
More than one table
Real databases split data across several tables. This lesson uses a small shop with two of them:
customer— one row per customer (id,name,city)orders— one row per order (id,customer_id,order_date,total)
Storing the customer's name on every order would repeat it again and again. Keeping customers in their own table stores each one once.
Plus d'une table
Les bases de données réelles partagent les données entre plusieurs tables. Cette leçon utilise une petite boutique avec deux d'entre elles :
customer— une ligne par client (id,name,city)orders— une ligne par commande (id,customer_id,order_date,total)
Stocker le nom du client sur chaque commande le répéterait encore et encore. Garder les clients dans leur propre table stocke chacun une seule fois.
Primary keys
A primary key is the column that uniquely identifies each row. It is never blank and never repeated.
customer.idis the primary key ofcustomer.orders.idis the primary key oforders.
Given a primary key value, you can always find exactly one row.
Clés primaires
Une clé primaire est la colonne qui identifie de manière unique chaque ligne. Elle n'est jamais vide et ne se répète jamais.
customer.idest la clé primaire decustomer.orders.idest la clé primaire deorders.
Étant donné une valeur de clé primaire, vous pouvez toujours trouver exactement une ligne.
Foreign keys
A foreign key is a column that holds the primary key of another table, linking the two.
orders.customer_id holds a value from customer.id — it says which customer each order belongs to.
Referential integrity means every foreign-key value must match a real primary key: an order can't belong to a customer who doesn't exist.
Clés étrangères
Une clé étrangère est une colonne qui contient la clé primaire d'autre table, liant ainsi les deux.
orders.customer_id contient une valeur provenant de customer.id — cela indique lequel client chaque commande appartient.
L'intégrité référentielle signifie que toute valeur de clé étrangère doit correspondre à une véritable clé primaire : une commande ne peut appartenir à un client qui n'existe pas.
Common mistakes
- A PRIMARY KEY is unique and cannot be NULL.
- A foreign key points to another table's primary key.
Erreurs courantes
- Une PRIMARY KEY est unique et ne peut pas être NULL.
- Une clé étrangère pointe vers la clé primaire d'une autre table.
Keys link two tables · Les clés lient deux tables
A shared key · clé lets a row in one table point to a row in another. · Une clé partagée permet à une ligne d'une table de pointer vers une ligne d'une autre.
Show the whole customer table so you can see the rows and their primary key id. · Affichez toute la table customer afin de voir les lignes et sa clé primaire id.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Show each order's id, its customer_id (the foreign key), and its total, ordered by id. · Affichez la id de chaque commande, sa customer_id (la clé étrangère), et sa total, triée par id.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.