Tables, Rows and Columns

Updated · 14 min read
easyPostgreSQL 18sqlrelational-modeltableskeysinterview

A relational database organizes its data into tables, where each table represents one kind of information, and the tables connect through keys. A table looks like a spreadsheet sheet: a name at the top, columns across, and one row for each thing being stored. The words table, row, column, value and key appear in every lesson after this one, so this lesson makes sure each one is clear.

In One Minute

  • A table holds one kind of thing: students, courses or payments.
  • A row is one of those things (one student). A column is one fact about it (the student's city).
  • A value is what sits where a row and a column meet: Dallas. A missing value is NULL.
  • Every row in a table has the same columns, and each column holds one type of data.
  • A primary key identifies each row; a foreign key points to a row in another table. The schema is the design of all the tables.

A Table Up Close

Here is the courses table from the Fee Tracker database. Run it and look at the shape of the result:

SQL▶ Try it
SELECT course_id, title, technology, fee_usd
FROM courses
ORDER BY course_id;
Output
 course_id |       title       | technology | fee_usd
-----------+-------------------+------------+---------
         1 | Java Basics       | Java       |  450.00
         2 | Java OOP          | Java       |  500.00
         3 | Java Collections  | Java       |  550.00
         4 | SQL Basics        | SQL        |  400.00
         5 | JavaScript Basics | JavaScript |  450.00
         6 | Git Basics        | Git        |  250.00
(6 rows)
Table, row, column, value and primary key, on the real courses table

Each part of that result has a name:

WordMeaningIn the result above
TableA named collection of one kind of thingcourses
Column (also called a field or attribute)One fact that every row has, with a name and a typetitle, fee_usd
Row (also called a record)One thing: here, one courseJava OOP and its facts
ValueThe single piece of data where a row and a column meet500.00

The line of dashes separates the column names from the rows, and (6 rows) tells you how many rows came back. You'll see this layout every time you run a query.

Every Row Has the Same Shape

In a spreadsheet, you can type anything anywhere. One row can have five cells filled in and the next row twelve. A database table is stricter, and that's a good thing:

  • Every row has exactly the same columns. A course can't have an extra "teacher" cell unless the table has a teacher column.
  • Each column has one data type. fee_usd holds money amounts, title holds text, and a date column holds dates. The database checks that every value fits the column's type. Some values convert safely, such as the text '400' into the number 400, but a value that can't be read as that type is refused.
  • Each value is a single piece of data. You don't put three phone numbers in one cell. If a student could have many phone numbers, that would become its own table. This is a design principle (database people call such values atomic), not a hard limit: PostgreSQL does offer arrays and JSON for special cases, which you'll meet later.

Watch the database refuse a row that breaks the rules. This statement tries to put the text 'four hundred' into the fee_usd column:

SQL▶ Try it
INSERT INTO courses (title, technology, level, fee_usd)
VALUES ('Python Basics', 'Python', 'Beginner', 'four hundred');
Output
ERROR:  invalid input syntax for type numeric: "four hundred"

The database doesn't guess what you meant. It stops, explains the problem, and leaves the table unchanged. Rules like this are why the data in a database stays trustworthy.

Same columns in every row, one type per column, and a wrong value refused

When a Value Is Missing: NULL

Sometimes a value is unavailable, unknown or doesn't apply. SQL represents that absence with NULL. Two of the students have no email address on file. The database doesn't store an empty string or a zero for them; it stores NULL, which means "no value here":

SQL▶ Try it
SELECT first_name, email, phone
FROM students
ORDER BY student_id;
Output
 first_name |             email              |     phone
------------+--------------------------------+----------------
 Srihitha   | srihitha.anamaneni@example.com | (415) 555-0142
 Advitha    | NULL                           | (415) 555-0178
 Navika     | NULL                           | NULL
(3 rows)

Try it prints the word NULL so you can tell it apart from empty text. NULL behaves in surprising ways when you filter and calculate, and it gets a whole lesson of its own later in the course, NULL Values.

NULL is the absence of a value: not 0 and not empty text

Keys at a Glance

A key is a column (or a group of columns) that identifies rows. Keys are how one row is told apart from another, and how tables connect to each other. Here is a first look; later lessons cover them in depth.

The Primary Key

Most application tables should have a primary key: a column, or a combination of columns, that uniquely identifies each row. PostgreSQL doesn't force every table to have one, but almost every table you design should. Two students could one day share a first name, a last name and even a city, so names can't be trusted to tell them apart. Instead, each student gets a student_id number that no other student has. That column is the students table's primary key:

  • It is unique: no two rows share the same value. The database enforces this.
  • It is never missing: every row has one. The database enforces this too.
  • It should stay stable over time, so other tables can rely on it. This is a design choice: the database would let you change a key value, but a good key rarely or never changes.

You can see the first rule at work. Adding a second student with the id 101 fails, because 101 is already Srihitha's:

SQL▶ Try it
INSERT INTO students (student_id, first_name, last_name, city, state, grade, joined_on)
VALUES (101, 'Test', 'Student', 'Dallas', 'TX', 9, '2026-01-05');
Output
ERROR:  duplicate key value violates unique constraint "students_pkey"
DETAIL:  Key (student_id)=(101) already exists.

The Foreign Key

A foreign key is a column, or a group of columns, whose values must match a primary key or another unique key in another table, or in the same table. In the students table, mentor_id holds the student_id of the student who mentors this one. Srihitha mentors Advitha, and Advitha mentors Navika:

SQL▶ Try it
SELECT student_id, first_name, mentor_id
FROM students
ORDER BY student_id;
Output
 student_id | first_name | mentor_id
------------+------------+-----------
        101 | Srihitha   |      NULL
        102 | Advitha    |       101
        103 | Navika     |       102
(3 rows)

Advitha's mentor_id is 101, which is Srihitha's student_id. That shared value is the whole connection; there are no hidden links. This is the core idea of a relational database: tables connect through matching values. The payments table connects to students the same way, through its own student_id column.

The primary key tells rows apart; the foreign key must match one

The Schema

The schema is the design of the database: which tables exist, which columns each one has, the type of each column, and the keys and rules that connect them. It's the blueprint; the rows are the building's contents. The schema changes rarely, while rows are added and changed all day.

Every table, column, type and key of the Fee Tracker, read from the database

The database keeps its own description of the schema, and you can ask for it with SQL. This query lists the columns of the courses table and their types. It names public, the schema (a folder-like container) where the Fee Tracker tables live, because two schemas in one database can each have a table called courses:

SQL▶ Try it
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name = 'courses'
ORDER BY ordinal_position;
Output
   column_name   |     data_type     | is_nullable
-----------------+-------------------+-------------
 course_id       | integer           | NO
 title           | character varying | NO
 technology      | character varying | NO
 level           | character varying | NO
 fee_usd         | numeric           | NO
 prerequisite_id | integer           | YES
(6 rows)

is_nullable says whether the column may hold NULL. A course must always have a title and a fee, but prerequisite_id may be empty, because not every course has a prerequisite.

A Table Is Not a Spreadsheet Sheet

A spreadsheet sheetA database table
Any cell can hold anythingEach column has one data type
Rows can have different numbers of filled cellsEvery row has exactly the same columns
Row 5 is always row 5Rows have no order until you ask for one with ORDER BY
Rows are told apart by their positionRows are told apart by their primary key
Links between sheets are formulas that can breakLinks are foreign keys the database checks

The third line surprises many beginners: a relational table has no guaranteed row order. If you want a particular order, you always ask for it, which is why every example in this course that returns several rows ends with ORDER BY. One more difference from the textbook idea of a "set": in SQL, a table can hold two identical rows unless a rule such as a primary key prevents it. You'll learn to find and remove duplicates in later lessons.

Common Mistakes

Putting several values in one column. A phones column holding "(415) 555-0142, (415) 555-0178" can't be searched or checked properly. One value per column; many values become many rows.

Using a name as the identifier. Names change and repeat. Use a primary key such as student_id, and keep the name as an ordinary column.

Expecting rows to come back in the order they were added. Without ORDER BY, the database may return rows in any order, and the order can change as the table grows.

Interview Corner

What is a relational database?

A database that organizes data into tables (relations) of rows and columns, where every row in a table has the same columns, and tables connect to each other through matching key values. Data is read and changed with SQL.

What is the difference between a primary key and a foreign key?

A primary key uniquely identifies each row in its own table: the database makes sure it's unique and never NULL, and a good one also stays stable. A foreign key is a column whose values must match a primary key (or unique key) in another table, or the same table; it creates the link between the two and the database rejects values that point to nothing.

What is a schema?

The structure of a database: its tables, their columns and data types, and the keys and constraints that connect them. In PostgreSQL the word also names a folder-like container of tables inside one database.

Do rows in a table have an order?

No. The database may return rows in any order, and that order can change. Only an ORDER BY clause guarantees the order of a query's result. (And unlike a mathematical set, a SQL table can contain duplicate rows unless a constraint prevents them.)

Practice

Challenge: Write a query that shows each student's student_id, first_name, city and state, sorted by student_id. Then answer: which column in your result is the primary key?

Show solution
SQL▶ Try it
SELECT student_id, first_name, city, state
FROM students
ORDER BY student_id;
Output
 student_id | first_name |     city      | state
------------+------------+---------------+-------
        101 | Srihitha   | San Francisco | CA
        102 | Advitha    | San Francisco | CA
        103 | Navika     | Dallas        | TX
(3 rows)

student_id is the primary key: every student has one, and no two students share it.

Quick check: Tables, rows and columns

3 questions
1In the courses table, what is one row?
2What does NULL mean?
3What makes a column a good primary key?

Key Takeaways

TermMeaningFee Tracker example
TableA collection of one kind of thingcourses
RowOne thingThe Java OOP course
ColumnOne fact every row hasfee_usd
ValueOne piece of data500.00
NULLNo valueAdvitha's email
Primary keyUniquely identifies a rowstudents.student_id
Foreign keyMust match a primary or unique keystudents.mentor_id
SchemaThe design of all the tablesThe Fee Tracker's five tables (cities, students, courses, enrollments, payments) and their rules

Was this tutorial helpful?

Lesson 3 of 115