forked from LinkedInLearning/practice-it-sql-joins-3085459
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tables.sql
355 lines (335 loc) · 11.8 KB
/
create_tables.sql
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
CREATE TABLE DimCurrency(
CurrencyKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
CurrencyAlternateKey nchar(3) NOT NULL,
CurrencyName nvarchar(50) NOT NULL
);
CREATE TABLE DimCustomer(
CustomerKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
GeographyKey int NULL,
CustomerAlternateKey nvarchar(15) NOT NULL,
Title nvarchar(8) NULL,
FirstName nvarchar(50) NULL,
MiddleName nvarchar(50) NULL,
LastName nvarchar(50) NULL,
NameStyle bit NULL,
BirthDate date NULL,
MaritalStatus nchar(1) NULL,
Suffix nvarchar(10) NULL,
Gender nvarchar(1) NULL,
EmailAddress nvarchar(50) NULL,
YearlyIncome money NULL,
TotalChildren tinyint NULL,
NumberChildrenAtHome tinyint NULL,
EnglishEducation nvarchar(40) NULL,
SpanishEducation nvarchar(40) NULL,
FrenchEducation nvarchar(40) NULL,
EnglishOccupation nvarchar(100) NULL,
SpanishOccupation nvarchar(100) NULL,
FrenchOccupation nvarchar(100) NULL,
HouseOwnerFlag nchar(1) NULL,
NumberCarsOwned tinyint NULL,
AddressLine1 nvarchar(120) NULL,
AddressLine2 nvarchar(120) NULL,
Phone nvarchar(20) NULL,
DateFirstPurchase date NULL,
CommuteDistance nvarchar(15) NULL,
FOREIGN KEY (GeographyKey) REFERENCES DimGeography(GeographyKey)
);
CREATE TABLE DimDate(
DateKey int NOT NULL PRIMARY KEY,
FullDateAlternateKey date NOT NULL,
DayNumberOfWeek tinyint NOT NULL,
EnglishDayNameOfWeek nvarchar(10) NOT NULL,
SpanishDayNameOfWeek nvarchar(10) NOT NULL,
FrenchDayNameOfWeek nvarchar(10) NOT NULL,
DayNumberOfMonth tinyint NOT NULL,
DayNumberOfYear smallint NOT NULL,
WeekNumberOfYear tinyint NOT NULL,
EnglishMonthName nvarchar(10) NOT NULL,
SpanishMonthName nvarchar(10) NOT NULL,
FrenchMonthName nvarchar(10) NOT NULL,
MonthNumberOfYear tinyint NOT NULL,
CalendarQuarter tinyint NOT NULL,
CalendarYear smallint NOT NULL,
CalendarSemester tinyint NOT NULL,
FiscalQuarter tinyint NOT NULL,
FiscalYear smallint NOT NULL,
FiscalSemester tinyint NOT NULL
);
CREATE TABLE DimDepartmentGroup(
DepartmentGroupKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ParentDepartmentGroupKey int NULL,
DepartmentGroupName nvarchar(50) NULL
);
CREATE TABLE DimSalesTerritory(
SalesTerritoryKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
SalesTerritoryAlternateKey int NULL,
SalesTerritoryRegion nvarchar(50) NOT NULL,
SalesTerritoryCountry nvarchar(50) NOT NULL,
SalesTerritoryGroup nvarchar(50) NULL,
SalesTerritoryImage varbinary(255) NULL
);
CREATE TABLE DimEmployee(
EmployeeKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ParentEmployeeKey int NULL,
EmployeeNationalIDAlternateKey nvarchar(15) NULL,
ParentEmployeeNationalIDAlternateKey nvarchar(15) NULL,
SalesTerritoryKey int NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
MiddleName nvarchar(50) NULL,
NameStyle bit NOT NULL,
Title nvarchar(50) NULL,
HireDate date NULL,
BirthDate date NULL,
LoginID nvarchar(256) NULL,
EmailAddress nvarchar(50) NULL,
Phone nvarchar(25) NULL,
MaritalStatus nchar(1) NULL,
EmergencyContactName nvarchar(50) NULL,
EmergencyContactPhone nvarchar(25) NULL,
SalariedFlag bit NULL,
Gender nchar(1) NULL,
PayFrequency tinyint NULL,
BaseRate money NULL,
VacationHours smallint NULL,
SickLeaveHours smallint NULL,
CurrentFlag bit NOT NULL,
SalesPersonFlag bit NOT NULL,
DepartmentName nvarchar(50) NULL,
StartDate date NULL,
EndDate date NULL,
Status nvarchar(50) NULL,
EmployeePhoto varbinary NULL,
FOREIGN KEY (SalesTerritoryKey) REFERENCES DimSalesTerritory(SalesTerritoryKey)
);
CREATE TABLE DimGeography(
GeographyKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
City nvarchar(30) NULL,
StateProvinceCode nvarchar(3) NULL,
StateProvinceName nvarchar(50) NULL,
CountryRegionCode nvarchar(3) NULL,
EnglishCountryRegionName nvarchar(50) NULL,
SpanishCountryRegionName nvarchar(50) NULL,
FrenchCountryRegionName nvarchar(50) NULL,
PostalCode nvarchar(15) NULL,
SalesTerritoryKey int NULL,
IpAddressLocator nvarchar(15) NULL,
FOREIGN KEY (SalesTerritoryKey) REFERENCES DimSalesTerritory(SalesTerritoryKey)
);
CREATE TABLE DimOrganization(
OrganizationKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ParentOrganizationKey int NULL,
PercentageOfOwnership nvarchar(16) NULL,
OrganizationName nvarchar(50) NULL,
CurrencyKey int NULL,
FOREIGN KEY (CurrencyKey) REFERENCES DimCurrency(CurrencyKey)
);
CREATE TABLE DimProductCategory(
ProductCategoryKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ProductCategoryAlternateKey int NULL,
EnglishProductCategoryName nvarchar(50) NOT NULL,
SpanishProductCategoryName nvarchar(50) NOT NULL,
FrenchProductCategoryName nvarchar(50) NOT NULL
);
CREATE TABLE DimProductSubcategory(
ProductSubcategoryKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ProductSubcategoryAlternateKey int NULL,
EnglishProductSubcategoryName nvarchar(50) NOT NULL,
SpanishProductSubcategoryName nvarchar(50) NOT NULL,
FrenchProductSubcategoryName nvarchar(50) NOT NULL,
ProductCategoryKey int NULL,
FOREIGN KEY (ProductCategoryKey) REFERENCES DimProductCategory(ProductCategoryKey)
);
CREATE TABLE DimProduct(
ProductKey int IDENTITY(1,1) PRIMARY KEY,
ProductAlternateKey nvarchar(25) NULL,
ProductSubcategoryKey int NULL,
WeightUnitMeasureCode nchar(3) NULL,
SizeUnitMeasureCode nchar(3) NULL,
EnglishProductName nvarchar(50) NOT NULL,
SpanishProductName nvarchar(50) NOT NULL,
FrenchProductName nvarchar(50) NOT NULL,
StandardCost money NULL,
FinishedGoodsFlag bit NOT NULL,
Color nvarchar(15) NOT NULL,
SafetyStockLevel smallint NULL,
ReorderPoint smallint NULL,
ListPrice money NULL,
Size nvarchar(50) NULL,
SizeRange nvarchar(50) NULL,
Weight float NULL,
DaysToManufacture int NULL,
ProductLine nchar(2) NULL,
DealerPrice money NULL,
Class nchar(2) NULL,
Style nchar(2) NULL,
ModelName nvarchar(50) NULL,
LargePhoto varbinary NULL,
EnglishDescription nvarchar(400) NULL,
FrenchDescription nvarchar(400) NULL,
ChineseDescription nvarchar(400) NULL,
ArabicDescription nvarchar(400) NULL,
HebrewDescription nvarchar(400) NULL,
ThaiDescription nvarchar(400) NULL,
GermanDescription nvarchar(400) NULL,
JapaneseDescription nvarchar(400) NULL,
TurkishDescription nvarchar(400) NULL,
StartDate datetime NULL,
EndDate datetime NULL,
Status nvarchar(7) NULL,
FOREIGN KEY (ProductSubCategoryKey) REFERENCES DimProductSubCategory(ProductSubCategoryKey)
);
CREATE TABLE DimPromotion(
PromotionKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
PromotionAlternateKey int NULL,
EnglishPromotionName nvarchar(255) NULL,
SpanishPromotionName nvarchar(255) NULL,
FrenchPromotionName nvarchar(255) NULL,
DiscountPct float NULL,
EnglishPromotionType nvarchar(50) NULL,
SpanishPromotionType nvarchar(50) NULL,
FrenchPromotionType nvarchar(50) NULL,
EnglishPromotionCategory nvarchar(50) NULL,
SpanishPromotionCategory nvarchar(50) NULL,
FrenchPromotionCategory nvarchar(50) NULL,
StartDate datetime NOT NULL,
EndDate datetime NULL,
MinQty int NULL,
MaxQty int NULL
);
CREATE TABLE DimSalesReason(
SalesReasonKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
SalesReasonAlternateKey int NOT NULL,
SalesReasonName nvarchar(50) NOT NULL,
SalesReasonReasonType nvarchar(50) NOT NULL
);
CREATE TABLE FactCurrencyRate(
CurrencyKey int NOT NULL,
DateKey int NOT NULL,
AverageRate float NOT NULL,
EndOfDayRate float NOT NULL,
Date datetime NULL,
PRIMARY KEY (CurrencyKey,DateKey),
FOREIGN KEY(DateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY(CurrencyKey) REFERENCES DimCurrency(CurrencyKey)
);
CREATE TABLE FactInternetSales(
ProductKey int NOT NULL,
OrderDateKey int NOT NULL,
DueDateKey int NOT NULL,
ShipDateKey int NOT NULL,
CustomerKey int NOT NULL,
PromotionKey int NOT NULL,
CurrencyKey int NOT NULL,
SalesTerritoryKey int NOT NULL,
SalesOrderNumber nvarchar(20) NOT NULL,
SalesOrderLineNumber tinyint NOT NULL,
RevisionNumber tinyint NOT NULL,
OrderQuantity smallint NOT NULL,
UnitPrice money NOT NULL,
ExtendedAmount money NOT NULL,
UnitPriceDiscountPct float NOT NULL,
DiscountAmount float NOT NULL,
ProductStandardCost money NOT NULL,
TotalProductCost money NOT NULL,
SalesAmount money NOT NULL,
TaxAmt money NOT NULL,
Freight money NOT NULL,
CarrierTrackingNumber nvarchar(25) NULL,
CustomerPONumber nvarchar(25) NULL,
OrderDate datetime NULL,
DueDate datetime NULL,
ShipDate datetime NULL,
PRIMARY KEY(SalesOrderNumber,SalesOrderLineNumber),
FOREIGN KEY(CurrencyKey) REFERENCES DimCurrency(CurrencyKey),
FOREIGN KEY(CustomerKey) REFERENCES DimCustomer(CustomerKey),
FOREIGN KEY(OrderDateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY(DueDateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY(ShipDateKey) REFERENCES DimDate(DateKey),
--FOREIGN KEY(ProductKey) REFERENCES DimProduct(ProductKey)
FOREIGN KEY(PromotionKey) REFERENCES DimPromotion(PromotionKey),
FOREIGN KEY(SalesTerritoryKey) REFERENCES DimSalesTerritory(SalesTerritoryKey)
);
CREATE TABLE FactInternetSalesReason(
SalesOrderNumber nvarchar(20) NOT NULL,
SalesOrderLineNumber tinyint NOT NULL,
SalesReasonKey int NOT NULL,
PRIMARY KEY (SalesOrderNumber,SalesOrderLineNumber,SalesReasonKey),
FOREIGN KEY(SalesOrderNumber,SalesOrderLineNumber) REFERENCES FactInternetSales(SalesOrderNumber, SalesOrderLineNumber),
FOREIGN KEY(SalesReasonKey) REFERENCES DimSalesReason(SalesReasonKey)
);
CREATE TABLE FactSurveyResponse(
SurveyResponseKey int IDENTITY(1,1) NOT NULL PRIMARY KEY,
DateKey int NOT NULL,
CustomerKey int NOT NULL,
ProductCategoryKey int NOT NULL,
EnglishProductCategoryName nvarchar(50) NOT NULL,
ProductSubcategoryKey int NOT NULL,
EnglishProductSubcategoryName nvarchar(50) NOT NULL,
Date datetime NULL,
FOREIGN KEY (DateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY (CustomerKey) REFERENCES DimCustomer(CustomerKey)
);
CREATE TABLE [DimReseller](
[ResellerKey] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[GeographyKey] [int] NULL,
[ResellerAlternateKey] [nvarchar](15) NULL,
[Phone] [nvarchar](25) NULL,
[BusinessType] [varchar](20) NOT NULL,
[ResellerName] [nvarchar](50) NOT NULL,
[NumberEmployees] [int] NULL,
[OrderFrequency] [char](1) NULL,
[OrderMonth] [tinyint] NULL,
[FirstOrderYear] [int] NULL,
[LastOrderYear] [int] NULL,
[ProductLine] [nvarchar](50) NULL,
[AddressLine1] [nvarchar](60) NULL,
[AddressLine2] [nvarchar](60) NULL,
[AnnualSales] [money] NULL,
[BankName] [nvarchar](50) NULL,
[MinPaymentType] [tinyint] NULL,
[MinPaymentAmount] [money] NULL,
[AnnualRevenue] [money] NULL,
[YearOpened] [int] NULL,
FOREIGN KEY (GeographyKey) REFERENCES DimGeography(GeographyKey)
);
CREATE TABLE [FactResellerSales](
[ProductKey] [int] NOT NULL,
[OrderDateKey] [int] NOT NULL,
[DueDateKey] [int] NOT NULL,
[ShipDateKey] [int] NOT NULL,
[ResellerKey] [int] NOT NULL,
[EmployeeKey] [int] NOT NULL,
[PromotionKey] [int] NOT NULL,
[CurrencyKey] [int] NOT NULL,
[SalesTerritoryKey] [int] NOT NULL,
[SalesOrderNumber] [nvarchar](20) NOT NULL,
[SalesOrderLineNumber] [tinyint] NOT NULL,
[RevisionNumber] [tinyint] NULL,
[OrderQuantity] [smallint] NULL,
[UnitPrice] [money] NULL,
[ExtendedAmount] [money] NULL,
[UnitPriceDiscountPct] [float] NULL,
[DiscountAmount] [float] NULL,
[ProductStandardCost] [money] NULL,
[TotalProductCost] [money] NULL,
[SalesAmount] [money] NULL,
[TaxAmt] [money] NULL,
[Freight] [money] NULL,
[CarrierTrackingNumber] [nvarchar](25) NULL,
[CustomerPONumber] [nvarchar](25) NULL,
[OrderDate] [datetime] NULL,
[DueDate] [datetime] NULL,
[ShipDate] [datetime] NULL,
PRIMARY KEY (SalesOrderNumber, SalesOrderLineNumber),
FOREIGN KEY(CurrencyKey) REFERENCES DimCurrency(CurrencyKey),
FOREIGN KEY(OrderDateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY(DueDateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY(ShipDateKey) REFERENCES DimDate(DateKey),
FOREIGN KEY(EmployeeKey) REFERENCES DimEmployee(EmployeeKey),
FOREIGN KEY(ProductKey) REFERENCES DimProduct(ProductKey),
FOREIGN KEY(PromotionKey) REFERENCES DimPromotion(PromotionKey),
FOREIGN KEY(ResellerKey) REFERENCES DimReseller(ResellerKey),
FOREIGN KEY(SalesTerritoryKey) REFERENCES DimSalesTerritory(SalesTerritoryKey)
);