All documentation

Docs ยท Guide

Using Tables to Structure Data

View on GitHubSuggest an edit

A table is a data structure that allows you to organize and manage information within your application. Think of it as a spreadsheet within your project, where data is arranged in rows and columns. Each row represents a record, and each column represents a specific attribute or field.

Imagine you want to store some data about your users, we want to store:

  • Their name
  • Email address
  • Profile picture
  • Date of Birth

We need to create a table with a column for each of these properties and we can leverage proper data types for each one. So for a name and email address we can use the Text type in those columns, then for storing the profile picture we'll use an Image type and lastly for the date of birth, we can leverage the Date type. This allows us to store our data for each user in it's most usable form and means we don't have to go and convert any data to store it in the table.

So, our table will then look like the following:

IdNameEmail addressProfile pictureDate of birth
1Alan Turingalan@computers.inc๐Ÿ’ป1912-06-12
2Tim Berners-Leetim@worldwideweb.com๐ŸŒ1955-06-08
3Linus Torvaldslinus@linux.net๐Ÿง1969-12-28

Tables in the real world

This is great, but in a real world application, the different data we store tends to be related. Think of our table of users, we might also want to store their address, their contact information and their previous orders.

We could create one table that contains all of this data, but that could end up being an incredibly big table which becomes very difficult to manage and maintain over time. Also, how would we represent many previous orders against a single user if it all lived in one table? It would result in an unwiedly table.

IdNameEmail addressProfile pictureDate of birthAddress Line 1Address Line 2CityPostCode / ZipCodeOrder IDProductCategoryAmount (GBP)
1Alan Turingalan@computers.inc๐Ÿ’ป1912-06-12Bletchley ParkMilton KeynesBuckinghamshireMK3 6EB987Engima Cracking KitElectronics99.99
1Alan Turingalan@computers.inc๐Ÿ’ป1912-06-12Bletchley ParkMilton KeynesBuckinghamshireMK3 6EB988Turing Machine PartsElectronics25.50
2Tim Berners-Leetim@worldwideweb.com๐ŸŒ1955-06-08CERN 1211Esplanade des Particules 1Geneva1217546Guide to the InternetBooks74.99
2Tim Berners-Leetim@worldwideweb.com๐ŸŒ1955-06-08CERN 1211Esplanade des Particules 1Geneva1217547How to build your first websiteBooks24.99
2Tim Berners-Leetim@worldwideweb.com๐ŸŒ1955-06-08CERN 1211Esplanade des Particules 1Geneva1217548Server for websiteComputing Hardware24.99
3Linus Torvaldslinus@linux.net๐Ÿง1969-12-28University of HelsinkiYliopistonkatu 4Helsinki00100321Guide to Microsoft WindowsBooks0.99
3Linus Torvaldslinus@linux.net๐Ÿง1969-12-28University of HelsinkiYliopistonkatu 4Helsinki00100322Guide to MacOSBooks1.99
3Linus Torvaldslinus@linux.net๐Ÿง1969-12-28University of HelsinkiYliopistonkatu 4Helsinki00100322How to build an Operating SystemBooks5.99

We can see how storing all of our data for our project in a single table leads to duplicated data, a large table and it doesn't actually represent how we think about this data in real life.

Better ways of storing the data

It would be much better if we could store the data about our users, their addresses, their previous orders and their contact information in separate tables and then just link them to each other. Fortunately, this is where Table Relationships and Views come in handy! These features allow us to express our data in a way that actually makes sense and mirrors the real life representation of data as closely as possible. This means we don't have to perform data transforming gymnastics just to view our table data or to update it.

Let's remodel our data, but using separate tables to store information about our users, their addresses and their orders.

Remodelled Tables to use Table Relationships

Following the above diagram, our tables now look like this:

Users Table

IdNameEmail addressProfile pictureDate of birthAssociated Address
1Alan Turingalan@computers.inc๐Ÿ’ป1912-06-1250
2Tim Berners-Leetim@worldwideweb.com๐ŸŒ1955-06-0842
3Linus Torvaldslinus@linux.net๐Ÿง1969-12-2864

Address Table

IdAddress Line 1Address Line 2CityPostCode / ZipCode
50Bletchley ParkMilton KeynesBuckinghamshireMK3 6EB
42CERN 1211Esplanade des Particules 1Geneva1217
64University of HelsinkiYliopistonkatu 4Helsinki00100

Orders Table

IdProductCategoryAmount (GBP)Associated User
987Engima Cracking KitElectronics99.991
988Turing Machine PartsElectronics25.501
546Guide to the InternetBooks74.992
547How to build your first websiteBooks24.992
548Server for websiteComputing Hardware24.992
321Guide to Microsoft WindowsBooks0.993
322Guide to MacOSBooks1.993
322How to build an Operating SystemBooks5.993

We can see that our data is much easier to work with, we don't have duplicate data appearing and the table structures are much more straight forward.

In the next few pages we'll take a look at how we would actually implement something like the above in Toca!