Module 7 · Designing tables

Create tables and choose data types

By the end you will be able to create a table and choose the type for each column.

About 12 min

0 of 52 lessons

Before you start, you need

Understand tables, rows and columns

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.

Minimal syntax
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.

SQL
CREATE TABLE shipments (
  id INTEGER,
  postcode TEXT,
  units INTEGER
);

Result in the lab: shipments created.

  1. Each row will represent one shipment. id and units are integers; postcode is text.
  2. A code such as 00127 must keep its leading zeros. We will not treat it as a quantity to add up.
  3. CREATE TABLE creates the empty structure. Adding shipments would require INSERT; this exercise checks the definition.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Model another table

Not started

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.

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.

Next: Declare primary and foreign keys

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.