Creating tables · Creación de tablas
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:
Dos tipos de comandos SQL
SQL tiene dos mitades:
- DDL (Data Definition Language) cambia la estructura — las propias tablas.
- DML (Data Manipulation Language) trabaja con los datos dentro de las tablas: leyéndolos con
SELECT, y agregando, modificando o eliminándolos conINSERT/UPDATE/DELETE. Todo lo visto hasta ahora (SELECT) es DML.
CREATE TABLE es el comando principal de DDL. Enumera cada columna con un tipo de dato y marca la clave primaria:
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.
Tipos de datos y claves foráneas
Tipos de datos comunes:
INTEGER— números enterosVARCHAR(n)/CHAR(n)— texto, hasta / exactamentencaracteresDECIMAL(p, s)— un número consdecimalesDATE,BOOLEAN
Para vincularse a otra tabla, se añade una FOREIGN KEY:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id)
);
El validador lee de nuevo la estructura de tu tabla para ver qué columnas creaste.
Common mistakes
- Every column needs a data type (e.g.
INTEGER,TEXT). - Declare the PRIMARY KEY when you create the table.
Errores comunes
- Cada columna necesita un tipo de dato (p. ej.,
INTEGER,TEXT). - Declara la PRIMARY KEY cuando crees la tabla.
Create a table called teacher with three columns: id (INTEGER, the primary key), name (VARCHAR(20)), and subject (VARCHAR(15)). · Cree una tabla llamada teacher con tres columnas: id (INTEGER, la clave primaria), name (VARCHAR(20)) y subject (VARCHAR(15)).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
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). · Diseñe otra tabla. Cree event con id (INTEGER PRIMARY KEY), title (VARCHAR(30)) y price (DECIMAL(6,2) — una columna monetaria con 2 decimales).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.