Creating tables · Création de tables
Two kinds of SQL command
SQL has two halves:
- DDL (Data Definition Language) changes the structure — the tables themselves.
- DML (Data Manipulation Language) works with the data inside the tables — reading it with
SELECT, and adding, changing, or removing it withINSERT/UPDATE/DELETE. Everything so far (SELECT) is DML.
CREATE TABLE is the main DDL command. It lists each column with a data type and marks the primary key:
Deux types de commandes SQL
SQL possède deux moitiés :
- DDL (Data Definition Language) modifie la structure — les tables elles-mêmes.
- Le DML (Data Manipulation Language) travaille avec les données à l'intérieur des tables — les lire avec
SELECT, et les ajouter, modifier ou supprimer avecINSERT/UPDATE/DELETE. Tout ce qui précède (SELECT) est du DML.
CREATE TABLE est la principale commande DDL. Elle liste chaque colonne avec un type de données et marque la clé primaire :
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name VARCHAR(20),
city VARCHAR(15)
);
Data types and foreign keys
Common data types:
INTEGER— whole numbersVARCHAR(n)/CHAR(n)— text, up to / exactlyncharactersDECIMAL(p, s)— a number withsdecimal placesDATE,BOOLEAN
To link to another table, add a FOREIGN KEY:
The checker reads back your table's structure to see which columns you created.
Types de données et clés étrangères
Types de données courants :
INTEGER— nombres entiersVARCHAR(n)/CHAR(n)— texte, jusqu'à / exactementncaractèresDECIMAL(p, s)— un nombre avecsdécimalesDATE,BOOLEAN
Pour se lier à une autre table, ajoutez une FOREIGN KEY :
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id)
);
Le vérificateur lit à nouveau la structure de votre table pour voir quelles colonnes vous avez créées.
Common mistakes
- Every column needs a data type (e.g.
INTEGER,TEXT). - Declare the PRIMARY KEY when you create the table.
Erreurs courantes
- Chaque colonne nécessite un type de données (ex.
INTEGER,TEXT). - Déclarez la PRIMARY KEY lors de la création de la table.
Create a table called teacher with three columns: id (INTEGER, the primary key), name (VARCHAR(20)), and subject (VARCHAR(15)). · Créez une table appelée teacher avec trois colonnes : id (INTEGER, la clé primaire), name (VARCHAR(20)), et subject (VARCHAR(15)).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Design another table. Create event with id (INTEGER PRIMARY KEY), title (VARCHAR(30)), and price (DECIMAL(6,2) — a money column with 2 decimal places). · Concevez une autre table. Créez event avec id (INTEGER PRIMARY KEY), title (VARCHAR(30)), et price (DECIMAL(6,2) — une colonne monétaire avec 2 décimales).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.