Sub topic 1
What a Database Is and Why Applications Need One
Prerequisites
This chapter assumes you have used spreadsheets, can read short SQL statements, and understand that applications store data. You do not need to have written SQL before.
Why Applications Need a Database
An application often needs to remember information between runs, such as user accounts, orders, or messages. A database is an organized store for that data. It lets many users read and write the same information safely, and it can find matching records quickly.
The Relational Model
A relational database uses the relational model, a way of organizing data into relations. In everyday terms, a relation is a table. Each row in the table is a tuple, which is one record. Each column is an attribute, which is one property of every record, such as name or email.
RDBMS Software and SQL
An RDBMS (relational database management system) is the software that stores relational data and runs queries against it. Examples include PostgreSQL, MySQL, and SQLite. You talk to an RDBMS using SQL (Structured Query Language), a standard language for creating tables, adding rows, and reading data.
Tables, Rows, Columns, and the Schema
The schema is the blueprint that defines every table, its columns, and the data type of each column. The schema is stored in the database itself, so the RDBMS can reject data that does not fit it.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL
);
INSERT INTO customers (customer_id, name, email)
VALUES (1, 'Ada Lovelace', '[email protected]');
SELECT name, email
FROM customers
WHERE customer_id = 1;The CREATE TABLE statement defines the schema. The INSERT statement adds one row, and the SELECT statement reads back the matching row.
Exercise
In the customers table above, what is a tuple, and what is an attribute?
Show answer
A tuple is one row, such as the record for Ada Lovelace. An attribute is one column, such as email, which describes a property of every row.