Skip to content

Commit 4160aff

Browse files
committed
Added sql_load files
1 parent 9c8f003 commit 4160aff

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

sql_load/1_create_database.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE DATABASE sql_course;
2+
3+
-- DROP DATABASE IF EXISTS sql_course;

sql_load/2_create_tables.sql

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Create company_dim table with primary key
2+
CREATE TABLE public.company_dim
3+
(
4+
company_id INT PRIMARY KEY,
5+
name TEXT,
6+
link TEXT,
7+
link_google TEXT,
8+
thumbnail TEXT
9+
);
10+
11+
-- Create skills_dim table with primary key
12+
CREATE TABLE public.skills_dim
13+
(
14+
skill_id INT PRIMARY KEY,
15+
skills TEXT,
16+
type TEXT
17+
);
18+
19+
-- Create job_postings_fact table with primary key
20+
CREATE TABLE public.job_postings_fact
21+
(
22+
job_id INT PRIMARY KEY,
23+
company_id INT,
24+
job_title_short VARCHAR(255),
25+
job_title TEXT,
26+
job_location TEXT,
27+
job_via TEXT,
28+
job_schedule_type TEXT,
29+
job_work_from_home BOOLEAN,
30+
search_location TEXT,
31+
job_posted_date TIMESTAMP,
32+
job_no_degree_mention BOOLEAN,
33+
job_health_insurance BOOLEAN,
34+
job_country TEXT,
35+
salary_rate TEXT,
36+
salary_year_avg NUMERIC,
37+
salary_hour_avg NUMERIC,
38+
FOREIGN KEY (company_id) REFERENCES public.company_dim (company_id)
39+
);
40+
41+
-- Create skills_job_dim table with a composite primary key and foreign keys
42+
CREATE TABLE public.skills_job_dim
43+
(
44+
job_id INT,
45+
skill_id INT,
46+
PRIMARY KEY (job_id, skill_id),
47+
FOREIGN KEY (job_id) REFERENCES public.job_postings_fact (job_id),
48+
FOREIGN KEY (skill_id) REFERENCES public.skills_dim (skill_id)
49+
);
50+
51+
-- Set ownership of the tables to the postgres user
52+
ALTER TABLE public.company_dim OWNER to postgres;
53+
ALTER TABLE public.skills_dim OWNER to postgres;
54+
ALTER TABLE public.job_postings_fact OWNER to postgres;
55+
ALTER TABLE public.skills_job_dim OWNER to postgres;
56+
57+
-- Create indexes on foreign key columns for better performance
58+
CREATE INDEX idx_company_id ON public.job_postings_fact (company_id);
59+
CREATE INDEX idx_skill_id ON public.skills_job_dim (skill_id);
60+
CREATE INDEX idx_job_id ON public.skills_job_dim (job_id);

sql_load/3_modify_tables.sql

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
2+
Database Load Issues (follow if receiving permission denied when running SQL code below)
3+
4+
NOTE: If you are having issues with permissions. And you get error:
5+
6+
'could not open file "[your file path]\job_postings_fact.csv" for reading: Permission denied.'
7+
8+
1. Open pgAdmin
9+
2. In Object Explorer (left-hand pane), navigate to `sql_course` database
10+
3. Right-click `sql_course` and select `PSQL Tool`
11+
- This opens a terminal window to write the following code
12+
4. Get the absolute file path of your csv files
13+
1. Find path by right-clicking a CSV file in VS Code and selecting “Copy Path”
14+
5. Paste the following into `PSQL Tool`, (with the CORRECT file path)
15+
16+
\copy company_dim FROM '[Insert File Path]/company_dim.csv' WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
17+
18+
\copy skills_dim FROM '[Insert File Path]/skills_dim.csv' WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
19+
20+
\copy job_postings_fact FROM '[Insert File Path]/job_postings_fact.csv' WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
21+
22+
\copy skills_job_dim FROM '[Insert File Path]/skills_job_dim.csv' WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
23+
24+
*/
25+
26+
-- NOTE: This has been updated from the video to fix issues with encoding
27+
28+
COPY company_dim
29+
FROM 'C:\Program Files\PostgreSQL\16\data\Datasets\sql_course\company_dim.csv'
30+
WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
31+
32+
COPY skills_dim
33+
FROM 'C:\Program Files\PostgreSQL\16\data\Datasets\sql_course\skills_dim.csv'
34+
WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
35+
36+
COPY job_postings_fact
37+
FROM 'C:\Program Files\PostgreSQL\16\data\Datasets\sql_course\job_postings_fact.csv'
38+
WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');
39+
40+
COPY skills_job_dim
41+
FROM 'C:\Program Files\PostgreSQL\16\data\Datasets\sql_course\skills_job_dim.csv'
42+
WITH (FORMAT csv, HEADER true, DELIMITER ',', ENCODING 'UTF8');

0 commit comments

Comments
 (0)