Databases

B+ Trees

How data is stored - Relational Databases

In an SQL database, data is stored inside tables. Tables are a way to organize data where each row contains information about a single primary key. A primary key uniquely identifies each record, where a record is a row.

If we go back to the phone numbers example, let's say we want to uniquely identify each person by their phone number and store their name in a table called People. We must declare the structure of the table first before inserting any records. In SQL, it can be defined as the following:

CREATE TABLE People (
    PhoneNumber int PRIMARY KEY,
    Name varchar(100)
);

varchar, (sometimes pronounced var-car) is a data type for variable length strings. This can inlcude numbers, letters and special characters.

If we wanted to have another table which would associate each PhoneNumber in our People table with an address, indicating that each person must have an address, we can do so in a table called HOMES. We want to ensure that no PhoneNumber that does not exist in People can be inserted into Homes. This is an example of a FOREIGN KEY constraint. This now means that each row in Homes table is associated with a row in the People table via the PhoneNumber.