-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_init_database.sql
More file actions
114 lines (98 loc) · 2.46 KB
/
00_init_database.sql
File metadata and controls
114 lines (98 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
=============================================================
Create Database and Schemas
=============================================================
Script Purpose:
This script creates a new database named 'DataWarehouseAnalytics' after checking if it already exists.
If the database exists, it is dropped and recreated. Additionally, this script creates a schema called gold
WARNING:
Running this script will drop the entire 'DataWarehouseAnalytics' database if it exists.
All data in the database will be permanently deleted. Proceed with caution
and ensure you have proper backups before running this script.
*/
USE master;
GO
-- Drop and recreate the 'DataWarehouseAnalytics' database
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'DataWarehouseAnalytics')
BEGIN
ALTER DATABASE DataWarehouseAnalytics SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE DataWarehouseAnalytics;
END;
GO
-- Create the 'DataWarehouseAnalytics' database
CREATE DATABASE DataWarehouseAnalytics;
GO
USE DataWarehouseAnalytics;
GO
-- Create Schemas
CREATE SCHEMA gold;
GO
CREATE TABLE gold.dim_customers(
customer_key int,
customer_id int,
customer_number nvarchar(50),
first_name nvarchar(50),
last_name nvarchar(50),
country nvarchar(50),
marital_status nvarchar(50),
gender nvarchar(50),
birthdate date,
create_date date
);
GO
CREATE TABLE gold.dim_products(
product_key int ,
product_id int ,
product_number nvarchar(50) ,
product_name nvarchar(50) ,
category_id nvarchar(50) ,
category nvarchar(50) ,
subcategory nvarchar(50) ,
maintenance nvarchar(50) ,
cost int,
product_line nvarchar(50),
start_date date
);
GO
CREATE TABLE gold.fact_sales(
order_number nvarchar(50),
product_key int,
customer_key int,
order_date date,
shipping_date date,
due_date date,
sales_amount int,
quantity tinyint,
price int
);
GO
TRUNCATE TABLE gold.dim_customers;
GO
BULK INSERT gold.dim_customers
FROM 'C:\sql\sql-data-analytics-project\datasets\csv-files\gold.dim_customers.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
TABLOCK
);
GO
TRUNCATE TABLE gold.dim_products;
GO
BULK INSERT gold.dim_products
FROM 'C:\sql\sql-data-analytics-project\datasets\csv-files\gold.dim_products.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
TABLOCK
);
GO
TRUNCATE TABLE gold.fact_sales;
GO
BULK INSERT gold.fact_sales
FROM 'C:\sql\sql-data-analytics-project\datasets\csv-files\gold.fact_sales.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
TABLOCK
);
GO