Database Basics · Lesson 1 of 112

Database Basics: What Is a Database

What you will learn

  • The difference between data and information
  • What a database is
  • Why we need databases when we already have files
  • The five promises a database makes
  • A first taste of SQL

1. Data and information

Data is raw facts: the number 25, the word Dhaka, a date, a photo. By itself it says little.

Information is data that has meaning in context. 25 is data; "Rina is 25 years old and lives in Dhaka" is information.

Data Information
25, Dhaka, Rina Rina is 25 and lives in Dhaka
1200, 1500, 1350 monthly sales rose in March and fell in April

Programs turn data into information by storing it, finding the right pieces and combining them. Almost every program does this: a shop remembers products and orders, a bank remembers accounts, a school remembers students and results, a phone remembers contacts.

2. What is a database?

A database is an organised collection of data, stored so that it can be found, changed and protected easily, even when it is large and many people use it at once.

The key word is organised. A shoebox of receipts is a collection of data. A ledger in which every receipt has a date, a shop and an amount, written in the same place every time, is a database, because you can answer questions from it.

The software that looks after the data is called a database management system (DBMS), covered in lesson 1.3. People often say "database" for both the data and the software.

Quick check: is a folder of unlabelled photos a database? Why?

Not really: nothing is organised, so you cannot ask it questions. Adding a table with a date, place and person for each photo would turn it into one.

3. Why not just use files?

You already know how to store data in a file (Python lesson 15.1). Take five people saved in a plain text file, one per line (name,age,city). The data is deliberately a little untidy: Amy's age is missing, and Joe's city is written in lower case.

text = """Rina,25,Dhaka
Sam,22,Khulna
Lee,30,Dhaka
Amy,,Sylhet
Joe,31,dhaka"""

To answer "who lives in Dhaka and is older than 24?", you write a program that reads the lines, splits them, converts the ages and tests each person:

matches = []
for line in text.splitlines():
    name, age, city = line.split(",")
    if city == "Dhaka" and age and int(age) > 24:
        matches.append(name)
print(matches)
['Rina', 'Lee']

That works. Now a similar question asked of a database. This lesson comes with three sample databases (Shop, Bank and School, each with hundreds or thousands of made-up rows); use the Example data picker at the top to choose one. Press Run under a query to see the answer, in your browser. The question is written in SQL (Structured Query Language), the standard language for asking databases questions:

Which customers live in Dhaka and joined in 2026 or later?

SELECT first_name, last_name, joined_on
FROM customers
WHERE city = 'Dhaka' AND joined_on >= '2026-01-01'
ORDER BY customer_id
LIMIT 5;
first_name  last_name  joined_on
----------  ---------  ----------
Elena       Kumar      2026-05-31
Anil        Costa      2026-03-16
Bruno       Meyer      2026-02-07
Sven        Sato       2026-07-15
Liam        Hassan     2026-03-11
(5 rows)

Which customers live in Lima and were born before 1970?

SELECT full_name, birth_date
FROM customers
WHERE city = 'Lima' AND birth_date < '1970-01-01'
ORDER BY customer_id
LIMIT 5;
full_name       birth_date
--------------  ----------
Chen Park       1956-03-20
Grace Kamau     1962-01-19
Kofi Nair       1956-05-14
Carlos Alvarez  1966-02-27
Ana Cohen       1955-12-07
(5 rows)

Which students enrolled in 2024 and have no guardian name on file?

SELECT first_name, last_name, enrolled_year
FROM students
WHERE enrolled_year = 2024 AND guardian_name IS NULL
ORDER BY student_id
LIMIT 5;
first_name  last_name  enrolled_year
----------  ---------  -------------
Ibrahim     Mahmud              2024
Omar        Haque               2024
Yusuf       Reyes               2024
Uma         Sato                2024
Leo         Meyer               2024
(5 rows)

In the file version you described how to find the people (loop, split, convert, test, collect). In the database version you described what you want (SELECT ... WHERE ... AND ...) and the database worked out how.

The difference grows with every new question. Each of these is one sentence:

SELECT city, COUNT(*) AS customers FROM customers GROUP BY city ORDER BY customers DESC, city LIMIT 3;
SELECT name, price FROM products ORDER BY price DESC LIMIT 2;
SELECT ROUND(AVG(price), 2) AS average_price FROM products;
city          customers
------------  ---------
Leeds                35
Istanbul             31
Porto Alegre         29
(3 rows)

name          price
------------  -----
Water bottle   86.9
Tea kettle     79.0
(2 rows)

average_price
-------------
        29.16
(1 row)
SELECT city, COUNT(*) AS customers FROM customers GROUP BY city ORDER BY customers DESC, city LIMIT 3;
SELECT account_id, balance_cents FROM accounts ORDER BY balance_cents DESC LIMIT 2;
SELECT ROUND(AVG(balance_cents) / 100.0, 2) AS average_balance FROM accounts;
city   customers
-----  ---------
Lima          25
Osaka         24
Hanoi         23
(3 rows)

account_id  balance_cents
----------  -------------
       398         502080
       243         432800
(2 rows)

average_balance
---------------
         1382.2
(1 row)
SELECT enrolled_year, COUNT(*) AS students FROM students GROUP BY enrolled_year ORDER BY students DESC, enrolled_year LIMIT 3;
SELECT code, title, credits FROM courses ORDER BY credits DESC, code LIMIT 2;
SELECT ROUND(AVG(grade), 1) AS average_grade FROM enrollments;
enrolled_year  students
-------------  --------
         2024       102
         2026        89
         2025        86
(3 rows)

code   title        credits
-----  -----------  -------
CS201  Programming        4
CS301  Databases          4
(2 rows)

average_grade
-------------
         71.3
(1 row)
  • The first query counts rows per group, most common first.
  • The second gives the two biggest values.
  • The third gives an average. (AVG ignores missing values, so the school's average uses only the grades that have been given.)

With files, each of these is a new little program to write, test and maintain. With a database, each is one sentence.

A database does not fix untidy data by magic. Look for it in the data:

Some customers have a city written in lower case, so the database counts it as a different city:

SELECT city, COUNT(*) AS customers FROM customers WHERE LOWER(city) = 'istanbul' GROUP BY city ORDER BY city;
city      customers
--------  ---------
Istanbul         31
istanbul          1
(2 rows)

Not every record is complete. Some rows of the audit log have no note (NULL, lesson 2.3):

SELECT COUNT(*) AS rows_without_note FROM audit_log WHERE note IS NULL;
rows_without_note
-----------------
               29
(1 row)

Not every record is complete. Some students have no guardian name (NULL, lesson 2.3):

SELECT COUNT(*) AS students_without_guardian FROM students WHERE guardian_name IS NULL;
students_without_guardian
-------------------------
                       38
(1 row)

The database stores exactly what it is given and compares text exactly as stored. It gives you tools to prevent untidy data (chapters 2 and 3).

4. The five promises of a database

Promise Meaning
Organised data lives in a clear structure (tables, documents...) described in advance
Lasting the data is saved on disk and survives the program ending or the computer restarting
Searchable you can ask questions and get answers quickly, even from millions of records
Correct the database can refuse bad data, using rules you define
Shared and safe many people and programs can use it at the same time without damaging each other's work, and it can be backed up and protected

A plain file can give you the second promise. The others are what a database adds. Here is the fourth, correct, in action. We create a new table with rules in your copy of the sample database, then try to put bad data into it. The database refuses it by itself:

CREATE TABLE gift_cards (
    code    TEXT NOT NULL,
    balance REAL CHECK (balance BETWEEN 0 AND 500)
);
INSERT INTO gift_cards VALUES ('GC-001', 25.0);
SELECT code, balance FROM gift_cards;
code    balance
------  -------
GC-001     25.0
(1 row)
INSERT INTO gift_cards VALUES (NULL, 30.0);
IntegrityError: NOT NULL constraint failed: gift_cards.code
INSERT INTO gift_cards VALUES ('GC-002', 900.0);
IntegrityError: CHECK constraint failed: balance BETWEEN 0 AND 500

The table says "a code is required" and "a balance must be between 0 and 500".

CREATE TABLE payees (
    name              TEXT NOT NULL,
    daily_limit_cents INTEGER CHECK (daily_limit_cents BETWEEN 0 AND 100000)
);
INSERT INTO payees VALUES ('City Water', 50000);
SELECT name, daily_limit_cents FROM payees;
name        daily_limit_cents
----------  -----------------
City Water              50000
(1 row)
INSERT INTO payees VALUES (NULL, 20000);
IntegrityError: NOT NULL constraint failed: payees.name
INSERT INTO payees VALUES ('Big Spender', 900000);
IntegrityError: CHECK constraint failed: daily_limit_cents BETWEEN 0 AND 100000

The table says "a name is required" and "a daily limit must be between 0 and 100000 cents".

CREATE TABLE clubs (
    name    TEXT NOT NULL,
    members INTEGER CHECK (members BETWEEN 0 AND 60)
);
INSERT INTO clubs VALUES ('Chess', 18);
SELECT name, members FROM clubs;
name   members
-----  -------
Chess       18
(1 row)
INSERT INTO clubs VALUES (NULL, 12);
IntegrityError: NOT NULL constraint failed: clubs.name
INSERT INTO clubs VALUES ('Everyone', 400);
IntegrityError: CHECK constraint failed: members BETWEEN 0 AND 60

The table says "a name is required" and "members must be between 0 and 60".

The database enforced both rules itself, no matter which program tried to insert the data. A text file would have accepted every line.

Worked example: deciding whether you need a database

A small tailor shop wants to keep track of customers, their measurements and their orders. Think through the five promises:

Question Answer for the tailor
Is the data organised in a repeating pattern? yes: every customer has a name, phone and measurements; every order has a date and price
Must it be lasting? yes: orders must be there next month
Will they search it? yes: "orders due this week", "all of Rina's measurements"
Does it need to be correct? yes: a missing phone number or a negative price is a mistake
Is it shared? yes: the shop owner and an assistant both add orders

All five answers are "yes", so a database is a good fit. A shop that only needs a single list with no searching or sharing might do fine with a spreadsheet (lesson 1.2).

The names and shop are made up.

Common mistakes

  • Thinking a database is "just a big file". A file is only where data lives; a database also enforces rules, answers questions and shares data safely.
  • Thinking a database makes data tidy automatically. It enforces the rules you define; untidy data goes in unless a rule refuses it.
  • Confusing data with information. Data becomes information when it is put in context by a question.
  • Using a database when one list would do, or a list when several people need to share and check the data.

Key takeaways

  • Data is raw facts; information is data with meaning.
  • A database is an organised collection of data, managed by software, that is stored for good, searchable, correct, and shared safely.
  • With a database you describe what you want (in SQL); the database works out how.
  • A database can refuse bad data using rules, which a plain file cannot.
  • Use a database when data is repeated in a pattern, must last, must be searched, must be correct and may be shared.

Practice

1. Is the number 1500 data or information? Rewrite it as information.

Show answer

Data. As information: "Sales in March were 1,500 taka" (or any statement that says what it measures and when).

2. What is the main difference between searching a text file and asking a database?

Show answer

With a file you write a program that says how to find the answer; with a database you state what you want and the database finds it.

3. Name the five promises of a database.

Show answer

Organised, lasting, searchable, correct, and shared and safe.

4. Why does a database count Dhaka and dhaka as two different cities?

Show answer

The database compares text exactly as stored, and nothing told it that the two spellings mean the same place. Rules and good design (chapters 2 and 3) prevent such untidy data.

5. What happens if you try to insert a row that breaks a rule of the new table from section 4?

Show answer

INSERT INTO gift_cards VALUES ('GC-002', 900.0); is refused with CHECK constraint failed: balance BETWEEN 0 AND 500, and a row with no code is refused with NOT NULL constraint failed: gift_cards.code.

Show answer

INSERT INTO payees VALUES ('Big Spender', 900000); is refused with a CHECK constraint failed message, and a row with no name is refused with NOT NULL constraint failed: payees.name.

Show answer

INSERT INTO clubs VALUES ('Everyone', 400); is refused with a CHECK constraint failed message, and a row with no name is refused with NOT NULL constraint failed: clubs.name.

6. Give an example from daily life of something that is a database, and something that is only a collection of data.

Show answer

For example, a library catalogue (every book has a title, author and shelf code, and you can search) is a database; a drawer of loose notes in no order is only a collection of data.