Before you start, you need
Define the attributes of a new row
To store shipments, you first decide what a row represents: one shipment. Then you choose its attributes, such as the id, the postcode and the number of units. CREATE TABLE creates that empty structure.
Inside the parentheses, write each column name and its type, separated by commas. The type says what kind of value you expect: INTEGER for integers, REAL for numbers with decimals and TEXT for text.
Choose the type by what the value means. Units are a quantity you can add up. A postcode such as 00127 is best stored as text to keep its leading zeros; it is not a quantity to do sums with.
In practice you will also see VARCHAR(120), another way to declare a text column. Creating a table does not add any rows: you do that with INSERT. Later you will learn the rules for identity and allowed values.
CREATE TABLE name (
column_1 type,
column_2 type
);See it in action
We want to store the postcode and the units of each shipment.
CREATE TABLE shipments (
id INTEGER,
postcode TEXT,
units INTEGER
);Result in the lab: shipments created.
- Each row will represent one shipment. id and units are integers; postcode is text.
- A code such as 00127 must keep its leading zeros. We will not treat it as a quantity to add up.
- CREATE TABLE creates the empty structure. Adding shipments would require INSERT; this exercise checks the definition.
- Separate definitions with commas and enclose the list in parentheses. Identity rules are introduced in the next lesson.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Define a courses table
Write the SQL statement that creates a courses table with the columns id INTEGER, title VARCHAR(120) and hours INTEGER.
The requested structure is checked.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Each row represents a course: an integer id, a text title and an integer duration.
Open parentheses for the columns.
Separate the three definitions with commas.
Solution explained
CREATE TABLE courses (id INTEGER, title VARCHAR(120), hours INTEGER);The structure says that hours is numeric and title is text.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Model another table
Create members with id INTEGER, phone TEXT and age INTEGER, in that order. Do not add constraints in this activity.
The requested structure is checked.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Decide for each property whether it is a quantity you would do arithmetic with.
A phone number such as +44 7700 900000 is not a number you add up.
phone must be TEXT and age INTEGER.
Solution explained
CREATE TABLE members (id INTEGER, phone TEXT, age INTEGER);The phone number is stored as text: it can start with + or 0 and is not used for calculations. Age, on the other hand, is a whole-number quantity.
Key takeaways
- CREATE TABLE defines a structure.
- Each column has a name and a type.
- The type is chosen according to what the value means.
- Creating the table does not insert any rows.
Go deeper
In SQLite, VARCHAR(120) declares text but does not itself enforce a maximum of 120 characters. Other engines may apply that limit. The exercises in this lesson check the structure; validation rules are taught later.