Creating 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:
SQL 명령의 두 가지 유형
SQL에는 두部分组成이 있습니다:
- DDL (Data Definition Language)은 structure — 테이블 자체를 변경합니다.
- DML (Data Manipulation Language)은 테이블 안의 data와 작업합니다 —
SELECT로 읽어들이고,INSERT/UPDATE/DELETE로 추가, 수정, 또는 제거합니다. 지금까지의 모든 것(SELECT)은 DML입니다.
CREATE TABLE은 주요 DDL 명령입니다. 각 열을 나열하고 데이터 타입과 primary key를 지정합니다:
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.
데이터 타입 및 외래 키
일반적인 데이터 타입:
INTEGER— 정수VARCHAR(n)/CHAR(n)— 텍스트, 최대 / 정확히n문자DECIMAL(p, s)—s소수점을 가진 숫자DATE,BOOLEAN
다른 테이블과 연결하려면 FOREIGN KEY를 추가합니다:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id)
);
체크러는 당신이 만든 열을 확인하기 위해 테이블 구조를 다시 읽습니다.
Common mistakes
- Every column needs a data type (e.g.
INTEGER,TEXT). - Declare the PRIMARY KEY when you create the table.
흔한 실수
- 모든 열은 데이터 타입을 가져야 합니다(예:
INTEGER,TEXT). - 테이블 생성 시 PRIMARY KEY를 선언합니다.
Create a table called teacher with three columns: id (INTEGER, the primary key), name (VARCHAR(20)), and subject (VARCHAR(15)).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
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).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.