CREATE TABLE softcartDimDate (
dateid INT PRIMARY KEY,
date DATE NOT NULL,
year INT NOT NULL,
quarter INT NOT NULL,2) NOT NULL,
quarterName VARCHAR(
month INT NOT NULL,9) NOT NULL,
monthname VARCHAR(
day INT NOT NULL,
weekday INT NOT NULL,9) NOT NULL
weekdayName VARCHAR(; )
Create PostgreSQL DB
Objectives
I’ll be working this project at times on the cloud and at times on the local machine. We’ll start with these steps to create a DataWarehouse using PostgreSQL and mostly pgAdmin
- Design a Data Warehouse using the pgAdmin ERD design tool.
- Create the schema in the Data Warehouse
As well as being able to create reports like:
- total sales per year per country
- total sales per month per category
- total sales per quarter per country
- total sales per category per country
Design DW
Data
Design Star Schema
In order to design a Star Schema for the warehouse by identifying the columns for the various dimension and fact tables in the schema we need to
- Create a PostgreSQL Instance
- Save password:
- In left panel > Servers>PostgreSQL>Databases>
- Create a new db and name it: softcart
- After creating the db above we need to create Tables in the db, so in the left panel make sure you are in the softcart db
- Scroll down to Schemas tab > expand
- Scroll down to Tables tab and
- Right click > SQL query and enter the SQL code to create the tables
softcartDimDate
- I’ll be using the ERD design tool design the table softcartDimDate.
- The company is looking at a granularity of a day. Which means they would like to have the ability to generate the report on
- yearly
- monthly
- daily, and
- weekday basis.
Here is a list of the fields in DimDate I find useful:
- dateid
- date
- year
- quarter
- quartername
- month
- monthname
- day
- weekday
- weekdayname
softcartDimCategory
Here is the list:
- categoryid
- categoryname
CREATE TABLE softcartDimCategory (
categoryid INT PRIMARY KEY,15) NOT NULL
categoryname VARCHAR(; )
softcartDimItem
ERD Tool
Using the ERD design tool to design the table softcartDimItem.
- Right click on database name
- Choose ERD for db
- CLick on + ADD table
- General Tab > Name it
- Columns > + two columns one for each field as shown below
- itemid as PM integer
- itemname as varchar(15) NOT NULL
Here is the list:
- itemid
- itemname
CREATE TABLE IF NOT EXISTS softcartDimItem (
itemid INT PRIMARY KEY,15) NOT NULL
itemname VARCHAR(; )
softcartDimCountry
Using the ERD design tool design the table softcartDimCountry.
Here is the list:
- countryid
- countryname
CREATE TABLE softcartDimCountry (
countryid INT PRIMARY KEY,255) NOT NULL
countryname VARCHAR(; )
softcartFactSales
Using the ERD design tool design the table softcartFactSales.
Here is the list:
- orderid
- price
- itemid
- categoryid
- coutryid
- dateid
CREATE TABLE softcartFactSales(255) PRIMARY KEY,
orderid VARCHAR(10, 2) NOT NULL,
price DECIMAL(
itemid INT NOT NULL,
categoryid INT NOT NULL,
countryid INT NOT NULL,
dateid INT NOT NULL; )
Design Relationships
- In the ERD screen > 1M - 1 to many relationship tool
Create Schema
- We will download the schema from the ERD into an sql file named: shema.sql
- Using the downloaded schema we’ll create a db named: staging
- Click on db name
- Choose ERD for database (these steps were already done above)
- This step was done already: once the ERD is open design it (since we designed it all above we just save it)
Create SQL for Schema
- Now that the ERD is saved and open
- Click on the THIRD icon in the toolbar
- SQL optionS > generate SQL > save as > name it schema.sql
- The SQL code is here below
-- This script was generated by the ERD tool in pgAdmin 4.
-- Please log an issue at https://github.com/pgadmin-org/pgadmin4/issues/new/choose if you find any bugs, including reproduction steps.
;
BEGIN
CREATE TABLE IF NOT EXISTS public.softcartdimcategory
(
categoryid integer NOT NULL,15) COLLATE pg_catalog."default" NOT NULL,
categoryname character varying(
CONSTRAINT softcartdimcategory_pkey PRIMARY KEY (categoryid);
)
CREATE TABLE IF NOT EXISTS public.softcartdimcountry
(
countryid integer NOT NULL,255) COLLATE pg_catalog."default" NOT NULL,
countryname character varying(
CONSTRAINT softcartdimcountry_pkey PRIMARY KEY (countryid);
)
CREATE TABLE IF NOT EXISTS public.softcartdimdate
(
dateid integer NOT NULL,
date date NOT NULL,
year integer NOT NULL,
quarter integer NOT NULL,2) COLLATE pg_catalog."default" NOT NULL,
quartername character varying(
month integer NOT NULL,9) COLLATE pg_catalog."default" NOT NULL,
monthname character varying(
day integer NOT NULL,
weekday integer NOT NULL,9) COLLATE pg_catalog."default" NOT NULL,
weekdayname character varying(
CONSTRAINT softcartdimdate_pkey PRIMARY KEY (dateid);
)
CREATE TABLE IF NOT EXISTS public.softcartdimitem
(
itemid integer NOT NULL,15) COLLATE pg_catalog."default" NOT NULL,
itemname character varying(
CONSTRAINT softcartdimitem_pkey PRIMARY KEY (itemid);
)
CREATE TABLE IF NOT EXISTS public.softcartfactsales
(255) COLLATE pg_catalog."default" NOT NULL,
orderid character varying(10, 2) NOT NULL,
price numeric(
itemid integer NOT NULL,
categoryid integer NOT NULL,
countryid integer NOT NULL,
dateid integer NOT NULL,
CONSTRAINT softcartfactsales_pkey PRIMARY KEY (orderid);
)
ALTER TABLE IF EXISTS public.softcartfactsales
ADD FOREIGN KEY (itemid)
REFERENCES public.softcartdimitem (itemid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;
NOT VALID
ALTER TABLE IF EXISTS public.softcartfactsales
ADD FOREIGN KEY (categoryid)
REFERENCES public.softcartdimcategory (categoryid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;
NOT VALID
ALTER TABLE IF EXISTS public.softcartfactsales
ADD FOREIGN KEY (categoryid)
REFERENCES public.softcartdimcategory (categoryid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;
NOT VALID
ALTER TABLE IF EXISTS public.softcartfactsales
ADD FOREIGN KEY (countryid)
REFERENCES public.softcartdimcountry (countryid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;
NOT VALID
ALTER TABLE IF EXISTS public.softcartfactsales
ADD FOREIGN KEY (dateid)
REFERENCES public.softcartdimdate (dateid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;
NOT VALID
; END
Create New DB
- name it staging
Import Schema
- Now that we have a new db staging we’ll import the schema we just created above
- Choose the db from left tree pane
- Scroll down to Schemas
- Right click > Query Tool
- When that opens > Click on the first icon Folder icon
- Load the sql file as shown in image below
- When sql loads, Run code
- Now we have all the tables in the db