Level 2 of 13 · Building the kitchen
SQL CREATE TABLE and INSERT INTO Explained, with Practice
How to build a table from nothing: column names, data types, NOT NULL and DEFAULT, then filling it with INSERT INTO.
Covers: CREATE TABLE · data types · NOT NULL · DEFAULT · INSERT INTO · DESCRIBE
The lesson
Every example runs against the same small pizza shop database: pizzas, customers, orders and staff.
# Create a new table You need it whenever a project has something new to store, like delivery drivers. Every table you have queried so far began with a CREATE TABLE. CREATE TABLE makes a new, empty table. Give it a name, then list its columns in brackets: each column's name and the type of data it holds. - `CREATE TABLE drivers` make a table called drivers - `(id INT, name VARCHAR(50), …)` its columns, separated by commas - `INT` `VARCHAR` `DECIMAL` data types: the next slide explains them - `;` ends one statement, so the next one can start - `DESCRIBE drivers` lists the table's columns and their types > Use lower-case names with underscores, like added_on. Spaces or capitals in a name force you to quote it in every query.
CREATE TABLE drivers ( id INT, name VARCHAR(50), rating DECIMAL(3,1)); DESCRIBE drivers
# Data types Every column holds one type of data. - `INT` whole numbers: 1, 2, 3 - `VARCHAR(30)` text, up to 30 characters - `DECIMAL(4,2)` exact numbers with decimals: 4 digits, 2 after the point, like 11.25 - `DATE` a day, like '2026-10-23' - `BOOLEAN` yes or no; MySQL stores it as TINYINT(1), 0 or 1 > Use DECIMAL for money, never FLOAT. FLOAT stores approximations, so 0.1 + 0.2 doesn't come out as exactly 0.3.
CREATE TABLE toppings ( id INT, name VARCHAR(30), price DECIMAL(4,2), added_on DATE, vegan BOOLEAN ); DESCRIBE toppings
# Required columns and defaults - `NOT NULL` this column must always have a value - `DEFAULT 5.0` the value used when an INSERT leaves the column out Leave a NOT NULL column empty and the database refuses with "NOT NULL constraint failed". A constraint is a rule the database enforces. > Make a column NOT NULL whenever a row makes no sense without it. It's far easier than cleaning up empty values later.
CREATE TABLE drivers ( id INT, name VARCHAR(50) NOT NULL, rating DECIMAL(3,1) DEFAULT 5.0 ); DESCRIBE drivers
# Add rows: INSERT INTO - `INSERT INTO drivers (id, name, rating)` the table, then the columns you are filling - `VALUES (1, 'Zoe', 4.8), (2, 'Max', 4.2)` the values in the same order, one pair of brackets per row A column you leave out gets its DEFAULT, or NULL. You can skip the column list, INSERT INTO drivers VALUES (4, 'Bo', 4.9), but then you must give every column, in the table's order. > Always list the columns. If someone adds a column to the table later, an INSERT without a column list breaks.
CREATE TABLE drivers ( id INT, name VARCHAR(50) NOT NULL, rating DECIMAL(3,1) DEFAULT 5.0 ); INSERT INTO drivers (id, name, rating) VALUES (1, 'Zoe', 4.8), (2, 'Max', 4.2); INSERT INTO drivers (id, name) VALUES (3, 'Ali'); SELECT * FROM drivers
# INSERT into an existing table INSERT works the same way on any table. This one adds a new pizza to the menu. > After an INSERT, SELECT the new row by its id to check what went in.
INSERT INTO pizzas (id, name, price, spicy, veg) VALUES (9, 'Truffle Shuffle', 15, 0, 1); SELECT * FROM pizzas WHERE id = 9
Appian corner
When you create a record type with a new database table, Appian writes this CREATE TABLE for you from the fields you define. Knowing the SQL lets you read what it made, design tables yourself, and write the database scripts you deploy alongside your application packages.
Practice questions
Try each one before opening the answer. To write and run them against the database in your browser, practise the level.
1. Which data type suits a price like 11.25?
- INT
- VARCHAR(10)
- DECIMAL(5,2)
- DATE
Show the answer
DECIMAL(5,2). DECIMAL keeps the exact pennies. INT would drop them, and text cannot be added up.
2. Create a toppings table: an id, and a name of up to 30 characters.
CREATE TABLE toppings ( id INT, name ___(30))
Show the answer
CREATE TABLE toppings ( id INT, name VARCHAR(30))
The text type with a maximum length.
3. Create a table drivers: id (whole number), name (text up to 50, required) and rating (a decimal like 4.8).
Show the answer
CREATE TABLE drivers ( id INT, name VARCHAR(50) NOT NULL, rating DECIMAL(3,1))
CREATE TABLE drivers (id INT, name VARCHAR(50) NOT NULL, rating DECIMAL(3,1))
4. This fails: name is NOT NULL, and the INSERT leaves it out. Fix the INSERT so topping 1 goes in with the name Olives.
CREATE TABLE toppings ( id INT, name VARCHAR(20) NOT NULL); INSERT INTO toppings (id) VALUES (1);
Show the answer
CREATE TABLE toppings ( id INT, name VARCHAR(20) NOT NULL); INSERT INTO toppings (id, name) VALUES (1, 'Olives');
List both columns and both values: (id, name) VALUES (1, 'Olives')
5. Add a new pizza: id 9, 'Truffle Shuffle', price 15, not spicy (0), veggie (1).
Show the answer
INSERT INTO pizzas (id, name, price, spicy, veg) VALUES (9, 'Truffle Shuffle', 15, 0, 1)
INSERT INTO pizzas (id, name, price, spicy, veg) VALUES (9, 'Truffle Shuffle', 15, 0, 1)
6. Add two customers in one statement, without a nickname: (8, 'Hana Mori', 'Leeds', 'hana@mail.com') and (9, 'Ian Cole', 'York', 'ian@mail.com').
Show the answer
INSERT INTO customers (id, name, city, email) VALUES (8, 'Hana Mori', 'Leeds', 'hana@mail.com'), (9, 'Ian Cole', 'York', 'ian@mail.com')
List the four columns, then two sets of VALUES separated by a comma.
7. Boss question! Create a table reviews: id (whole number), pizza_id (whole number), stars (whole number, required, default 5) and comment (text up to 200). Then add one review: id 1, pizza 2, 4 stars, 'Spicy and great'.
Show the answer
CREATE TABLE reviews ( id INT, pizza_id INT, stars INT NOT NULL DEFAULT 5, comment VARCHAR(200)); INSERT INTO reviews (id, pizza_id, stars, comment) VALUES (1, 2, 4, 'Spicy and great')
stars INT NOT NULL DEFAULT 5. Then an INSERT INTO reviews after a semicolon.