Skip to main content

Creating Tables

Updated Oct 05, 2019 ·

Overview

Relational databases are great for organizing real-world things like customers and products into tables. Each table focuses on one type of thing, which helps avoid repeating the same information. You can also define how different things relate to each other, like a customer purchasing multiple products or a product being bought by several customers.

  • Each table focuses on one type of entity.
  • Defines relationships between different entities.

Sample Tables

Here is the schema for the sample World table. This tables we'll be used for the examples in this guide.

To download the actual files, you can get them from my Github repository.

Building a Database

We can begin with a single table and expand it by adding rules like constraints and keys to keep our data accurate and organized.

  • Start small and expand our database.
  • Use constraints and keys to keep data accurate.

An example of database is PostgreSQL. To explore a PostgreSQL database, we need to use SQL queries. The information_schema database has lots of useful details about our database’s setup and works in systems like MySQL and SQL Server.

Table Columns

To check out table columns, use the information_schema database. The "columns" table shows you a table’s column details once you know its name, like how the "products" table might hold columns for product name and price.

SELECT table_schema, table_name
FROM information_schema.tables

The output would look something like this. Notice that there's are differnt table schemas here: world, cinema, public, and pg_catalog.

Table SchemaTable Name
worldeconomies
worldlanguages
worldpopulations
worldeconomies2010
worldcountries_plus
worldcurrencies
cinemadescriptions
publicbooks
pg_catalogpg_type
worldprime_ministers
worldstates
Some records are not shown.

To look at tables with specific schemas, use the WHERE command:

SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema = 'public'; -- specify the schema here

To list columns of a specific table (e.g., products):

SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'states';

Output:

Column NameData Type
namecharacter varying
continentcharacter varying
indep_yearinteger
fert_ratereal
women_parli_percreal

Checking the Schema

The command below can be used to check the schema of a given table.

SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'add_table_name_here';

Redundancies

In the economies.csv file, you might notice repeated information about economic data and their related attributes.

  • Economic data and related attributes are repeated in the same table.
  • Indicates redundancy due to mixing different types of data.

For instance, economic metrics such as GDP per capita and inflation rate are stored together, leading to potential redundancy. This can become evident when sorting or querying the data, showing repeated entries for the same country across different years.

We can see this when we sort by year. The output below shows economic data for a specific year but there are many more records in this table.

SELECT * FROM economies
ORDER BY year;
Econ IDCodeYearIncome GroupGDP per CapitaGross SavingsInflation RateTotal InvestmentUnemployment RateExportsImports
191LBN2010Upper middle income8,755.853.8473.983NULLNULL-18.267-1.825
97ECU2010Upper middle income4,633.2525.7573.55228.0375.0191.10914.283
193LBR2010Low income341.985NULL7.291NULLNULL-0.39810.189
5ALB2010Upper middle income4,098.1320.0113.60531.3051410.645-8.013
195LBY2010Upper middle income12,149.59NULL2.45839.086NULL-0.8389.906
99EGY2010Lower middle income2,921.7619.42111.6921.2989.213.62811.663
197LCA2010Upper middle income7,491.6611.7343.2528.06NULL5.42318.427
51BTN2010Lower middle income1,998.7544.6745.72666.9063.3-0.57925.991
199LKA2010Lower middle income2,779.7428.4576.21830.352513.79116.52
101ERI2010Low income395.645-9.25711.2289.299NULL18.08511.854

Entity Types

The economies table currently stores various types of economic information in a single table, which may lead to some redundancy. For example, it includes gdp_percapita, gross_savings, and other economic indicators all mixed in one table.

Currently, the database holds different types of data (economic indicators, trade data) in the same table. To reduce redundancy, we can separate these into different tables, ensuring each table focuses on a specific aspect of economic data.

A better approach is to divide the data into separate tables:

  • economic_summary

    Column NameData Type
    econ_idinteger
    codecharacter varying
    yearinteger
    income_groupcharacter varying
  • economic_indicators

    Column NameData Type
    econ_idinteger
    gdp_percapitareal
    gross_savingsreal
    inflation_ratereal
    total_investmentreal
    unemployment_ratereal
  • trade_data

    Column NameData Type
    econ_idinteger
    exportsreal
    importsreal

This will help in organizing the data more efficiently and reduce redundancy by storing each type of information in its respective table. The new database model could look like this:

Creating New Tables

The CREATE TABLE command helps define new tables with appropriate columns and data types. For more information, please see Specifying Types upon Table Creation.

CREATE TABLE table_name (
column_a data_type,
column_b data_type,
column_c data_type
);

To create the three new databases:

CREATE TABLE economic_summary (
econ_id integer,
code character varying,
year integer,
income_group character varying
);


SELECT * FROM economic_summary;

Output:

econ_idcodeyearincome_group
CREATE TABLE economic_indicators (
econ_id integer,
gdp_percapita real,
gross_savings real,
inflation_rate real,
total_investment real,
unemployment_rate real
);

SELECT * FROM economic_indicators;

Output:

econ_idgdp_percapitagross_savingsinflation_ratetotal_investmentunemployment_rate

CREATE TABLE trade_data (
econ_id integer,
exports real,
imports real
);

SELECT * FROM trade_data;

Output:

econ_idexportsimports

Column operations

  • To add a new column to an existing table:

    ALTER TABLE table_name
    ADD COLUMN column_name data_type;
  • To rename columns:

    ALTER TABLE table_name
    RENAME COLUMN old_name TO new_name;
  • To delete columns:

    ALTER TABLE table_name
    DROP COLUMN column_name;

INSERT INTO

After changing the database structure, the next step is to migrate the records from the old table to the new table. To do this, we use the INSERT_INTO command, followed by the target table. We then use the SELECT DISTINCT command followed by the columns or attributes that we want to migrate over.

INSERT INTO new_table 
SELECT DISTINCT
column_1_to_be_moved,
column_2_to_be_moved,
FROM old_table

It should return an output like the one below. The "1892" specify the number of records inserted to the new table.

Output: INSERT 0 1892 

Note that if we don't use DISTINCT, duplicate records will be migrated as well.

INSERT INTO new_table 
SELECT
column_1_to_be_moved,
column_2_to_be_moved,
FROM old_table
Output: INSERT 0 2105 

The other way to write is by inserting manually. You can insert new columns as well as new values for those columns.

INSERT INTO new_table (column_a, column_b) 
VALUES ("values_a", "values_b",)

DROP TABLE

Note before dropping any table:

  • Ensure you have a recent backup of the table data.
  • Verify if the table is referenced by other tables (foreign key constraints).
  • Verify if the table is used in views, stored procedures, or functions.
  • Confirm you have the necessary permissions to drop the table.
  • Double-check the table to ensure you are dropping the correct one.
  • Inform team members or stakeholders about the table drop.
  • Document the reason for dropping the table and the steps to be taken.
  • If possible, test the drop command in a development or staging environment.

To drop tables and its data, use the command below.

DROP TABLE table_name; 

Note that it will return an error if the table doesn't exist.

table "table_name" does not exist 

Use this command instead to drop table if it already exists.

DROP TABLE IF EXISTS table_name;