How To Create Fact And Dimension Tables In Mysql

Mysql is a popular relational database management system. Fact and dimension tables are essential for creating a data warehouse or a business intelligence system. In this tutorial, we will learn how to create Fact and Dimension Tables in Mysql.

Step 1: Create a Database and Tables

First, we need to create a database and tables. We can use any tool or Mysql shell to execute these commands. In this tutorial, we will use the Mysql shell to create tables.

Creating Database

To create a database, use the following command:

Creating Fact Table

To create a Fact Table, use the following command:

Creating Dimension Table

To create a Dimension Table, use the following command:

Step 2: Insert Data

After creating tables, we need to insert data into these tables. We can use the following commands to insert data into Fact and Dimension tables:

Inserting Data into Fact Table

Inserting Data into Dimension Table

Step 3: Query Data

After inserting data, we can query data from these tables. We can use the following command to join Fact and Dimension tables:

This will give us the following output:

+------------+-------------+-----------+----------+-------+
| date | product_name | category | quantity | price |
+------------+-------------+-----------+----------+-------+
| 2021-01-01 | Product A | Category 1 | 10 | 100 |
| 2021-01-01 | Product B | Category 1 | 20 | 200 |
| 2021-01-02 | Product A | Category 1 | 5 | 100 |
| 2021-01-02 | Product C | Category 2 | 15 | 300 |
+------------+-------------+-----------+----------+-------+

Now, we have successfully created Fact and Dimension Tables in Mysql.

At the end of the post, the full code is given below: