Learning navigation

Code with Animation

Relational Model & Tables

Learn how a relational database keeps data in tables, rows and columns, and read a CREATE TABLE statement.

SQL4 min readConcept 1 of 44

A table is rows and columns

A relational database keeps its data in tables. A table is a named collection of rows. Picture a spreadsheet with one strict rule: every row has the same columns.

A row is one thing you store, such as one customer. A column is one kind of fact, such as city. A cell is the place where one row and one column meet.

Each column has a data type, such as integer or text. The type decides which values the column accepts.

Why the same shape matters

Because every row has the same shape, the database can answer questions about thousands of rows in one way: find them, sort them, count them and combine them.

Without that shape you would search through loose text by hand. With it, you describe what you want in SQL and the database does the searching.

Read a CREATE TABLE statement

This statement creates a customers table with four columns. Each column has a name and a type:

CREATE TABLE customers (
  id integer,
  name text,
  city text,
  age integer
);

The list inside the brackets is the schema: the column names and their types. It describes the shape of the data, not the data itself. Later, INSERT adds rows that fit this shape.

SQL does not promise any order for the rows. If you want them in a certain order, you must ask for it in the query. You will learn how in a later lesson.

Light up the parts of a table

Tap Table, Row, Column, Cell and Schema to see each part of the customers table. Press Add a row to watch an INSERT add one.

CREATE TABLE customers (
id integer,
name text,
city text,
age integer
);
 
-- one INSERT adds one row
INSERT INTO customers VALUES (2, 'Ben', 'Leeds', 17);
customers (5 rows, 4 columns)
idintegernametextcitytextageinteger
1AshaPune31
2BenLeeds17
3ChenOsloNULL
4DaraLima24
5EliRomeNULL

SQL does not promise any row order unless a query asks for one.

A table is a named collection of rows. Here it is customers.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1In the `customers` table, what is one row?
  2. 2Which statement creates a table called `users` with a text column called `nickname`?
  3. 3You run a query that does not sort. In what order does PostgreSQL promise to return the rows?

Remember this

  • A table is a named collection of rows, and every row has the same columns.
  • A row is one thing you store, a column is one kind of fact, and a cell is where they meet.
  • Each column has a data type, and the list of columns and types is called the schema.
  • SQL returns rows in no promised order unless the query asks for sorting.

Done with this concept?

Mark it complete to track your progress. No login needed.