-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject2_5MainScript.sql
More file actions
2215 lines (1824 loc) · 78.2 KB
/
Project2_5MainScript.sql
File metadata and controls
2215 lines (1824 loc) · 78.2 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- project 2.5 main sql script
/*
Step1: CREATE THE DATABASE
Instructions: run only lines 10 and 11 using the master databse
*/
-- CREATE DATABASE [PrestigeCars_9:15_Group1];
-- GO
/*
Step2: Create the Schemas & run all remaining code & procedures
Instructions: Run all remaining code under the [PrestigeCars_9:15_Group1] database
*/
--------------------- CREATE SCHEMAS -------------------------
DROP SCHEMA IF EXISTS [Sales];
GO
CREATE SCHEMA [Sales];
GO
DROP SCHEMA IF EXISTS [HumanResources];
GO
CREATE SCHEMA [HumanResources];
GO
DROP SCHEMA IF EXISTS [Production];
GO
CREATE SCHEMA [Production];
GO
DROP SCHEMA IF EXISTS [DbSecurity];
GO
CREATE SCHEMA [DbSecurity];
GO
DROP SCHEMA IF EXISTS [Process];
GO
CREATE SCHEMA [Process];
GO
DROP SCHEMA IF EXISTS [PkSequence];
GO
CREATE SCHEMA [PkSequence];
GO
DROP SCHEMA IF EXISTS [Project2.5];
GO
CREATE SCHEMA [Project2.5];
GO
-- Schema for views
DROP SCHEMA IF EXISTS [G9_1];
GO
CREATE SCHEMA [G9_1];
GO
------------------------- CREATE SEQUENCES ----------------------------
-- for automatically assigning keys in [DbSecurity].[UserAuthorization]
-- Aleks
CREATE SEQUENCE [PkSequence].[UserAuthorizationSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- for replacing identity key in [Process].[WorkflowSteps]
CREATE SEQUENCE [PkSequence].[WorkFlowStepsSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- Aryeh
-- for automatically assigning keys in [HumanResources].[Staff]
-- We need to ensure that the 13 staff members from the PrestigeCars database maintain the same StaffIDs so that the ManagerIDs would be acurate
-- Therefore we restart the sequence to start at 14 for future staff additions, since we already have the first 13 Staff members from the PrestigeCars database
CREATE SEQUENCE [PkSequence].[StaffSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
ALTER SEQUENCE [PkSequence].[StaffSequenceObject]
RESTART WITH 14;
-- for automatically assigning keys in [HumanResources].[Departments]
CREATE SEQUENCE [PkSequence].[DepartmentsSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- Edwin
-- for automatically assigning keys in [Sales].[Customers]
CREATE SEQUENCE [PkSequence].[CustomersSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- for automatically assigning keys in [Sales].[Orders]
CREATE SEQUENCE [PkSequence].[OrdersSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- for automatically assigning keys in [Sales].[OrderDetails]
CREATE SEQUENCE [PkSequence].[OrderDetailsSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- Sigi
--Assigning keys in [Sales].[MakeMarketing]
CREATE SEQUENCE [PkSequence].[MarketingSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
--Assigning keys in [Sales].[BudgetDelegations]
CREATE SEQUENCE [PkSequence].[DelegationSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
--Nicholas
-- for automatically assigning keys in [Production].[Make]
CREATE SEQUENCE [PkSequence].[MakeSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- for automatically assigning keys in [Production].[Model]
CREATE SEQUENCE [PkSequence].[ModelSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- for automatically assigning keys in [Production].[Stock]
CREATE SEQUENCE [PkSequence].[StockSequenceObject]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
GO
-- add more sequences as needed
------------------------- CREATE TABLES ---------------------------
-- UserAuthorization Table --
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [DbSecurity].[UserAuthorization]
GO
CREATE TABLE [DbSecurity].[UserAuthorization]
(
[UserAuthorizationKey] [int] NOT NULL,
[ClassTime] [nchar](5) NOT NULL,
[IndividualProject] [nvarchar](60) NULL,
[GroupMemberLastName] [nvarchar](35) NOT NULL,
[GroupMemberFirstName] [nvarchar](25) NOT NULL,
[GroupName] [nvarchar](20) NOT NULL,
[DateAdded] [datetime2](7) NULL,
PRIMARY KEY CLUSTERED
(
[UserAuthorizationKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/*
Table: Process.[WorkflowSteps]
Description:
This table is used for auditing and tracking the execution of various workflow steps within the system.
It records key information about each workflow step, including a description, the number of rows affected,
the start and end times of the step, and the user who executed the step.
Columns:
[WorkFlowStepKey] - The primary key for the table, uniquely identifying each workflow step.
[WorkFlowStepDescription] - A descriptive name or summary of the workflow step.
[WorkFlowStepTableRowCount] - The number of table rows that were affected or processed during the workflow step.
[StartingDateTime] - The date and time when the workflow step began.
[EndingDateTime] - The date and time when the workflow step ended.
[Class Time] - An optional field that could be used to record the time of a class or session during which the workflow step was executed.
[UserAuthorizationKey] - A foreign key linking to the UserAuthorization table to identify the user responsible for the workflow step.
Usage:
This table is populated by the 'usp_TrackWorkFlow' stored procedure, which is called at the beginning and end
of each workflow step to log its execution. It can be used for monitoring system activity, analyzing the performance
and duration of workflow steps, and ensuring that data processing is carried out by authorized users.
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Process].[WorkflowSteps]
GO
CREATE TABLE [Process].[WorkflowSteps]
(
[WorkFlowStepKey] [int] NOT NULL,
[WorkFlowStepDescription] [nvarchar](100) NOT NULL,
[WorkFlowStepTableRowCount] [int] NULL,
[StartingDateTime] [datetime2](7) NULL,
[EndingDateTime] [datetime2](7) NULL,
[QueryTime (ms)] [bigint] NULL,
[Class Time] [char](5) NULL,
[UserAuthorizationKey] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[WorkFlowStepKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
-- Aryeh
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [HumanResources].[Staff]
GO
CREATE TABLE [HumanResources].[Staff]
(
[StaffID] [int] NOT NULL,
[StaffName] [nvarchar](50) NOT NULL,
[ManagerID] [int] NULL,
[DepartmentKey] [int] NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[StaffID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [HumanResources].[Departments]
GO
CREATE TABLE [HumanResources].[Departments]
(
[DepartmentKey] [int] NOT NULL,
[Department] [nvarchar](50) NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[DepartmentKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
-- Edwin
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Sales].[Customers]
GO
CREATE TABLE [Sales].[Customers]
(
[CustomerID] [int] NOT NULL,
[CustomerName] [nvarchar](150) NULL,
[Address1] [nvarchar](50) NULL,
[Address2] [nvarchar](50) NULL,
[Town] [nvarchar](50) NULL,
[Country] [nvarchar](50) NULL,
[PostCode] [nvarchar](50) NULL,
[SpendCapacity] [nvarchar](25) NULL,
[IsReseller] [bit] NULL,
[IsCreditRisk] [bit] NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Sales].[Orders]
GO
CREATE TABLE [Sales].[Orders]
(
[OrderID] [int] NOT NULL,
[CustomerID] [int] NOT NULL,
[OrderDate] [datetime] NULL,
[InvoiceNumber] [char](8) NULL,
[TotalSalePrice] [numeric](18,2) NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Sales].[OrderDetails]
GO
CREATE TABLE [Sales].[OrderDetails]
(
[OrderDetailsID] [int] NOT NULL,
[OrderID] [int] NOT NULL,
[CustomerID] [int] NOT NULL,
[LineItemNumber] [tinyint] NULL,
[StockID] [nvarchar](50) NULL,
[SalesPrice] [numeric](18,2) NULL,
[LineItemDiscount] [numeric](18,2) NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[OrderDetailsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
-- Sigi
DROP TABLE IF EXISTS [Sales].[MakeMarketing]
GO
CREATE TABLE [Sales].[MakeMarketing]
(
[MarketingKey] [int] NOT NULL,
[MakeName] [nvarchar](50) NOT NULL,
[MarketingType] [nvarchar] (50) NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[MarketingKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
DROP TABLE IF EXISTS [Sales].[ColorBudget]
GO
CREATE TABLE [Sales].[ColorBudget]
(
[BudgetKey] [int] NOT NULL,
[BudgetValue] [money] NOT NULL,
[BudgetYear] [int] NOT NULL,
[Color] [nvarchar](20) NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[BudgetKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
DROP TABLE IF EXISTS [Sales].[SalesBudget]
GO
CREATE TABLE [Sales].[SalesBudget]
(
[BudgetKey] [int] NOT NULL,
[BudgetValue] [money] NOT NULL,
[BudgetDate] [Date] NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[BudgetKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
DROP TABLE IF EXISTS [Sales].[CountryBudget]
GO
CREATE TABLE [Sales].[CountryBudget]
(
[BudgetKey] [int] NOT NULL,
[BudgetValue] [money] NOT NULL,
[BudgetDate] [Date] NOT NULL,
[Country] [nvarchar](50) NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[BudgetKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
DROP TABLE IF EXISTS [Sales].[BudgetDelegations]
GO
CREATE TABLE [Sales].[BudgetDelegations]
(
[DelegationKey] [int] NOT NULL,
[BudgetArea] [nvarchar](20) NOT NULL,
[BudgetAmount] [money] NOT NULL,
[BudgetDate] [Date] NOT NULL,
[LastUpdated] [Date] NOT NULL,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[DelegationKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
--Nicholas
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Production].[Make]
GO
CREATE TABLE [Production].[Make]
(
[MakeName] NVARCHAR(100),
[MakeID] INT,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[MakeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
--Nicholas
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Production].[Model]
GO
CREATE TABLE [Production].[Model]
(
[ModelName] NVARCHAR(150),
[ModelVariant] NVARCHAR(150),
[MakeID] INT,
[ModelID] INT,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ModelID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
--Nicholas
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE IF EXISTS [Production].[Stock]
GO
CREATE TABLE [Production].[Stock]
(
StockCode NVARCHAR(50),
Cost MONEY,
RepairsCost MONEY,
PartsCost MONEY,
TransportInCost MONEY,
Color NVARCHAR(50),
DateBought DATE,
TimeBought TIME,
[ModelID] INT,
StockID INT,
[UserAuthorizationKey] [int] NOT NULL,
[DateAdded] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED
(
[StockID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
--------------------- Alter Tables To Update Defaults/Constraints -------------------
-- adding default values in the following format:
-- Aleks
ALTER TABLE [DbSecurity].[UserAuthorization] ADD DEFAULT (NEXT VALUE FOR PkSequence.[UserAuthorizationSequenceObject]) FOR [UserAuthorizationKey]
GO
ALTER TABLE [DbSecurity].[UserAuthorization] ADD DEFAULT ('9:15') FOR [ClassTime]
GO
ALTER TABLE [DbSecurity].[UserAuthorization] ADD DEFAULT ('PROJECT 2.5 NORMALIZE PRESTIGE CARS SCHEMA') FOR [IndividualProject]
GO
ALTER TABLE [DbSecurity].[UserAuthorization] ADD DEFAULT ('GROUP 1') FOR [GroupName]
GO
ALTER TABLE [DbSecurity].[UserAuthorization] ADD DEFAULT (sysdatetime()) FOR [DateAdded]
GO
ALTER TABLE [Process].[WorkflowSteps] ADD DEFAULT (NEXT VALUE FOR PkSequence.[WorkFlowStepsSequenceObject]) FOR [WorkFlowStepKey]
GO
ALTER TABLE [Process].[WorkflowSteps] ADD DEFAULT ((0)) FOR [WorkFlowStepTableRowCount]
GO
ALTER TABLE [Process].[WorkflowSteps] ADD DEFAULT (sysdatetime()) FOR [StartingDateTime]
GO
ALTER TABLE [Process].[WorkflowSteps] ADD DEFAULT (sysdatetime()) FOR [EndingDateTime]
GO
ALTER TABLE [Process].[WorkflowSteps] ADD DEFAULT ('9:15') FOR [Class Time]
GO
--Aryeh
ALTER TABLE [HumanResources].[Staff] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[StaffSequenceObject]) FOR [StaffID]
GO
ALTER TABLE [HumanResources].[Departments] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[DepartmentsSequenceObject]) FOR [DepartmentKey]
GO
-- Edwin
ALTER TABLE [Sales].[Customers] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[CustomersSequenceObject]) FOR [CustomerID]
GO
ALTER TABLE [Sales].[Orders] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[OrdersSequenceObject]) FOR [OrderID]
GO
ALTER TABLE [Sales].[OrderDetails] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[OrderDetailsSequenceObject]) FOR [OrderDetailsID]
GO
-- Sigi
ALTER TABLE [Sales].[MakeMarketing] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[MarketingSequenceObject]) FOR [MarketingKey]
GO
ALTER TABLE [Sales].[BudgetDelegations] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[DelegationSequenceObject]) FOR [DelegationKey]
GO
--Nicholas
ALTER TABLE [Production].[Make] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[MakeSequenceObject]) FOR [MakeID]
GO
ALTER TABLE [Production].[Model] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[ModelSequenceObject]) FOR [ModelID]
GO
ALTER TABLE [Production].[Stock] ADD DEFAULT (NEXT VALUE FOR [PkSequence].[StockSequenceObject]) FOR [StockID]
GO
-- add check constraints in the following format:
-- Aleks
ALTER TABLE [Process].[WorkflowSteps] WITH CHECK ADD CONSTRAINT [FK_WorkFlowSteps_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Process].[WorkflowSteps] CHECK CONSTRAINT [FK_WorkFlowSteps_UserAuthorization]
GO
-- Aryeh
ALTER TABLE [HumanResources].[Staff] WITH CHECK ADD CONSTRAINT [FK_Staff_Departments] FOREIGN KEY([DepartmentKey])
REFERENCES [HumanResources].[Departments] ([DepartmentKey])
GO
ALTER TABLE [HumanResources].[Staff] CHECK CONSTRAINT [FK_Staff_Departments]
GO
ALTER TABLE [HumanResources].[Staff] WITH CHECK ADD CONSTRAINT [FK_Staff_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [HumanResources].[Staff] CHECK CONSTRAINT [FK_Staff_UserAuthorization]
GO
ALTER TABLE [HumanResources].[Departments] WITH CHECK ADD CONSTRAINT [FK_Departments_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [HumanResources].[Departments] CHECK CONSTRAINT [FK_Departments_UserAuthorization]
GO
-- Edwin
ALTER TABLE [Sales].[Customers] WITH CHECK ADD CONSTRAINT [FK_Customers_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[Customers] CHECK CONSTRAINT [FK_Customers_UserAuthorization]
GO
ALTER TABLE [Sales].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Customers] FOREIGN KEY([CustomerID])
REFERENCES [Sales].[Customers] ([CustomerID])
GO
ALTER TABLE [Sales].[Orders] CHECK CONSTRAINT [FK_Orders_Customers]
GO
ALTER TABLE [Sales].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[Orders] CHECK CONSTRAINT [FK_Orders_UserAuthorization]
GO
ALTER TABLE [Sales].[OrderDetails] WITH CHECK ADD CONSTRAINT [FK_OrderDetails_Orders] FOREIGN KEY([OrderID])
REFERENCES [Sales].[Orders] ([OrderID])
GO
ALTER TABLE [Sales].[OrderDetails] CHECK CONSTRAINT [FK_OrderDetails_Orders]
GO
ALTER TABLE [Sales].[OrderDetails] WITH CHECK ADD CONSTRAINT [FK_OrderDetails_Customers] FOREIGN KEY([CustomerID])
REFERENCES [Sales].[Customers] ([CustomerID])
GO
ALTER TABLE [Sales].[OrderDetails] CHECK CONSTRAINT [FK_OrderDetails_Customers]
GO
ALTER TABLE [Sales].[OrderDetails] WITH CHECK ADD CONSTRAINT [FK_OrderDetails_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[OrderDetails] CHECK CONSTRAINT [FK_OrderDetails_UserAuthorization]
GO
-- Sigi
ALTER TABLE [Sales].[MakeMarketing] WITH CHECK ADD CONSTRAINT [FK_Marketing_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[MakeMarketing] CHECK CONSTRAINT [FK_Marketing_UserAuthorization]
GO
ALTER TABLE [Sales].[BudgetDelegations] WITH CHECK ADD CONSTRAINT [FK_Delegation_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[BudgetDelegations] CHECK CONSTRAINT [FK_Delegation_UserAuthorization]
GO
ALTER TABLE [Sales].[ColorBudget] WITH CHECK ADD CONSTRAINT [FK_Color_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[ColorBudget] CHECK CONSTRAINT [FK_Color_UserAuthorization]
GO
ALTER TABLE [Sales].[CountryBudget] WITH CHECK ADD CONSTRAINT [FK_Country_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[CountryBudget] CHECK CONSTRAINT [FK_Country_UserAuthorization]
GO
ALTER TABLE [Sales].[SalesBudget] WITH CHECK ADD CONSTRAINT [FK_SalesBudget_UserAuthorization] FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Sales].[SalesBudget] CHECK CONSTRAINT [FK_SalesBudget_UserAuthorization]
GO
--Nicholas
ALTER TABLE [Production].[Model] WITH CHECK ADD CONSTRAINT [FK_Model_Make] FOREIGN KEY([MakeID])
REFERENCES [Production].[Make] ([MakeID])
GO
ALTER TABLE [Production].[Model] CHECK CONSTRAINT [FK_Model_Make];
GO
ALTER TABLE [Production].[Stock] WITH CHECK ADD CONSTRAINT [FK_Stock_Model] FOREIGN KEY([ModelID])
REFERENCES [Production].[Model] ([ModelID])
GO
ALTER TABLE [Production].[Stock] CHECK CONSTRAINT [FK_Stock_Model];
GO
ALTER TABLE [Production].[Make] WITH CHECK ADD CONSTRAINT [FK_Make_Authorization] FOREIGN KEY([AuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Production].[Make] CHECK CONSTRAINT [FK_Make_Authorization]
GO
ALTER TABLE [Production].[Model] WITH CHECK ADD CONSTRAINT [FK_Model_Authorization] FOREIGN KEY([AuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Production].[Model] CHECK CONSTRAINT [FK_Model_Authorization]
GO
ALTER TABLE [Production].[Stock] WITH CHECK ADD CONSTRAINT [FK_Stock_Authorization] FOREIGN KEY([AuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey])
GO
ALTER TABLE [Production].[Stock] CHECK CONSTRAINT [FK_Stock_Authorization]
GO
------------------------- CREATE Table Valued Functions ---------------------------
-- =============================================
-- Author: Aryeh Richman
-- Create date: 11/27/23
-- Description: Input: StaffID
-- Output: Table of that staff member's managerial hierarchy
-- (the staff member, boss, boss's boss, etc.)
-- =============================================
DROP FUNCTION IF EXISTS [HumanResources].[GetStaffHierarchy]
GO
CREATE FUNCTION [HumanResources].[GetStaffHierarchy]
(@id AS INT) RETURNS TABLE
AS
RETURN
WITH HierarchyCTE AS (
SELECT S.StaffID, S.StaffName, S.ManagerID
FROM [HumanResources].[Staff] AS S
WHERE S.StaffID = @id
UNION ALL
SELECT E.StaffID, E.StaffName, E.ManagerID
FROM HierarchyCTE AS R
INNER JOIN [HumanResources].[Staff] AS E
ON R.ManagerID = E.StaffID
)
SELECT * FROM HierarchyCTE
GO
-------------- Create Stored Procedures --------------
/*
Stored Procedure: [Process].[usp_ShowWorkflowSteps]
Description:
This stored procedure is designed to retrieve and display all records from the Process.[WorkFlowSteps] table. It is intended to provide a comprehensive view of all workflow steps that have been logged in the system, offering insights into the various processes and their execution details.
Operations:
- The procedure sets NOCOUNT ON to prevent the return of the count of affected rows, thereby enhancing performance and reducing network traffic.
- A simple SELECT statement retrieves all records from the Process.[WorkFlowSteps] table, providing details such as the description of the workflow step, the start and end times, the number of rows affected, and the user authorization key associated with each step.
Usage:
This procedure is particularly useful for administrators and analysts who need to audit or review the history of workflow steps executed in the system. It allows for an easy overview of the entire workflow history, which can be crucial for process optimization, troubleshooting, and compliance purposes.
Example:
EXEC Process.[usp_ShowWorkflowSteps];
This example executes the stored procedure to retrieve and display all workflow steps from the Process.[WorkFlowSteps] table.
Note: The effectiveness of this procedure depends on the accurate and consistent logging of workflow steps in the Process.[WorkFlowSteps] table.
-- =============================================
-- Author: Aleksandra Georgievska
-- Create date: 11/13/23
-- Description: Show table of all workflow steps
-- =============================================
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER PROCEDURE [Process].[usp_ShowWorkflowSteps]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT *
FROM [Process].[WorkFlowSteps];
END
GO
/*
Stored Procedure: Process.[usp_TrackWorkFlow]
Description:
This stored procedure is designed to track and log each step of various workflows within the system. It inserts records into the [WorkflowSteps] table, capturing key details about each workflow step, such as its description, the number of table rows affected, and the start and end times. This procedure is instrumental in maintaining an audit trail and enhancing transparency in automated processes.
Parameters:
- @WorkflowDescription: NVARCHAR(100) describing the workflow step.
- @WorkFlowStepTableRowCount: INT indicating the number of rows affected or processed during the workflow step.
- @StartingDateTime: DATETIME2 marking when the workflow step began.
- @EndingDateTime: DATETIME2 marking when the workflow step ended.
- @UserAuthorizationKey: INT identifying the user who initiated or is responsible for the workflow step, crucial for auditing purposes.
Usage:
This procedure should be invoked at the start and end of each significant workflow step within automated processes or ETL jobs. It ensures that all workflow activities are logged, which aids in monitoring, troubleshooting, and analyzing process efficiency and user activity.
Example:
EXEC Process.[usp_TrackWorkFlow]
@WorkflowDescription = 'Data Load Step 1',
@WorkFlowStepTableRowCount = 100,
@StartingDateTime = '2023-11-13T08:00:00',
@EndingDateTime = '2023-11-13T08:30:00',
@UserAuthorizationKey = 5;
This example logs a workflow step described as 'Data Load Step 1', indicating that 100 rows were affected, starting at 8:00 AM and ending at 8:30 AM on November 13, 2023, performed by the user with authorization key 5.
Note: Proper usage of this stored procedure is essential for accurate and reliable workflow tracking. It should be consistently implemented across all relevant workflows for effective auditability and process analysis.
-- =============================================
-- Author: Aleksandra Georgievska
-- Create date: 11/13/23
-- Description: Keep track of all workflow steps
-- =============================================
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER PROCEDURE [Process].[usp_TrackWorkFlow]
-- Add the parameters for the stored procedure here
@WorkflowDescription NVARCHAR(100),
@WorkFlowStepTableRowCount INT,
@StartingDateTime DATETIME2,
@EndingDateTime DATETIME2,
@QueryTime BIGINT,
@UserAuthorizationKey INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO [Process].[WorkflowSteps]
(
WorkFlowStepDescription,
WorkFlowStepTableRowCount,
StartingDateTime,
EndingDateTime,
[QueryTime (ms)],
[Class Time],
UserAuthorizationKey
)
VALUES
(@WorkflowDescription, @WorkFlowStepTableRowCount, @StartingDateTime, @EndingDateTime, @QueryTime, '9:15',
@UserAuthorizationKey);
END;
GO
/*
Stored Procedure: Process.[Load_UserAuthorization]
Description: Prepopulating the UserAuthorization Table with the Group Names
-- =============================================
-- Author: Aleksandra Georgievska
-- Create date: 11/126/23
-- Description: Load the names & default values into the user authorization table
-- =============================================
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER PROCEDURE [Project2.5].[Load_UserAuthorization]
@UserAuthorizationKey INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @StartingDateTime DATETIME2 = SYSDATETIME();
INSERT INTO [DbSecurity].[UserAuthorization]
([GroupMemberLastName],[GroupMemberFirstName])
VALUES
('Georgievska','Aleksandra'),
('Yakubova','Sigalita'),
('Kong','Nicholas'),
('Wray','Edwin'),
('Ahmed','Ahnaf'),
('Richman','Aryeh');
DECLARE @WorkFlowStepTableRowCount INT;
SET @WorkFlowStepTableRowCount = 6;
DECLARE @EndingDateTime DATETIME2 = SYSDATETIME();
DECLARE @QueryTime BIGINT = CAST(DATEDIFF(MILLISECOND, @StartingDateTime, @EndingDateTime) AS bigint);
EXEC [Process].[usp_TrackWorkFlow] 'Add Users',
@WorkFlowStepTableRowCount,
@StartingDateTime,
@EndingDateTime,
@QueryTime,
@UserAuthorizationKey;
END;
GO
/*
Stored Procedure: [Project2.5].[AddForeignKeysToPrestigeCars]
Description:
This procedure is responsible for establishing foreign key relationships across various tables in the star schema database. It adds constraints to link fact and dimension tables to ensure referential integrity. The procedure also associates dimension tables with the UserAuthorization table, thereby establishing a traceable link between data records and the users responsible for their creation or updates.
Parameters:
- @UserAuthorizationKey: INT representing the user authorizing this operation, used for auditing purposes.
Operations:
1. Adds foreign key constraints to the [CH01-01-Fact].[Data] table, linking it to various dimension tables like DimCustomer, DimGender, DimMaritalStatus, etc.
2. Adds foreign key constraints to dimension tables, linking them to the [DbSecurity].[UserAuthorization] table.
3. Tracks the process using Process.[usp_TrackWorkFlow] to maintain an audit trail of the operation.
Usage:
This procedure should be executed when setting up the database schema or when modifications to the schema are required. It ensures data integrity across the star schema by enforcing appropriate foreign key relationships.
Example:
EXEC Project2.[AddForeignKeysToPrestigeCars] @UserAuthorizationKey = 5;
This example runs the procedure to add foreign keys across tables, authorized by the user with key 5.
Note: Proper execution of this procedure is critical to maintain data integrity and referential relationships in the star schema database. It should be executed with caution, ensuring that no data inconsistencies exist that could be affected by the new constraints.
-- =============================================
-- Author: Aleksandra Georgievska
-- Create date: 11/13/23
-- Description: Add the foreign keys to the start Schema database
-- =============================================
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER PROCEDURE [Project2.5].[AddForeignKeysToPrestigeCars]
@UserAuthorizationKey INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @StartingDateTime DATETIME2 = SYSDATETIME();
-- Aleks
ALTER TABLE [Process].[WorkflowSteps]
ADD CONSTRAINT FK_WorkFlowSteps_UserAuthorization
FOREIGN KEY (UserAuthorizationKey)
REFERENCES [DbSecurity].[UserAuthorization] (UserAuthorizationKey);
-- Aryeh
ALTER TABLE [HumanResources].[Staff]
ADD CONSTRAINT FK_Staff_Departments
FOREIGN KEY([DepartmentKey])
REFERENCES [HumanResources].[Departments] ([DepartmentKey]);
ALTER TABLE [HumanResources].[Staff]
ADD CONSTRAINT FK_Staff_UserAuthorization
FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey]);
ALTER TABLE [HumanResources].[Departments]
ADD CONSTRAINT FK_Departments_UserAuthorization
FOREIGN KEY([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey]);
-- Edwin
ALTER TABLE [Sales].[Customers]
ADD CONSTRAINT FK_Customers_UserAuthorization
FOREIGN KEY ([UserAuthorizationKey])
REFERENCES [DbSecurity].[UserAuthorization] ([UserAuthorizationKey]);