Creating tables · Criando tabelas
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:
Dois tipos de comando SQL
SQL tem duas metades:
- DDL (Data Definition Language) altera a estrutura — as próprias tabelas.
- DML (Data Manipulation Language) lida com os dados dentro das tabelas — lendo-os com
SELECT, e adicionando, alterando ou removendo-os comINSERT/UPDATE/DELETE. Tudo até agora (SELECT) é DML.
CREATE TABLE é o principal comando DDL. Ele lista cada coluna com um tipo de dado e marca a chave primária:
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 dados e chaves estrangeiras
Tipos de dados comuns:
INTEGER— números inteirosVARCHAR(n)/CHAR(n)— texto, até / exatamentencaracteresDECIMAL(p, s)— um número comscasas decimaisDATE,BOOLEAN
Para linkar a outra tabela, adicione uma FOREIGN KEY:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id)
);
O verificador lê de volta a estrutura da sua tabela para ver quais colunas você criou.
Common mistakes
- Every column needs a data type (e.g.
INTEGER,TEXT). - Declare the PRIMARY KEY when you create the table.
Erros comuns
- Toda coluna precisa de um tipo de dado (ex.
INTEGER,TEXT). - Declare a PRIMARY KEY ao criar a tabela.
Create a table called teacher with three columns: id (INTEGER, the primary key), name (VARCHAR(20)), and subject (VARCHAR(15)). · Crie uma tabela chamada teacher com três colunas: id (INTEGER, a chave primária), name (VARCHAR(20)) e subject (VARCHAR(15)).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
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). · Desenhe outra tabela. Crie event com id (INTEGER PRIMARY KEY), title (VARCHAR(30)) e price (DECIMAL(6,2) — uma coluna monetária com 2 casas decimais).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.