-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_database.sql
1993 lines (1756 loc) · 60.1 KB
/
create_database.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
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
-- TABLES --
CREATE TABLE Companies
(
CompanyID int NOT NULL,
CustomerID int NOT NULL,
CompanyName varchar(64) NOT NULL,
NIP char(10) NOT NULL,
CONSTRAINT Companies_pk PRIMARY KEY (CompanyID)
);
CREATE TABLE CompanyEmployees
(
CompanyEmployeeID int NOT NULL,
PersonID int NOT NULL,
CompanyID int NOT NULL,
CONSTRAINT CompanyEmployees_pk PRIMARY KEY (CompanyEmployeeID)
);
CREATE TABLE CompanyReservationParticipants
(
ReservationID int NOT NULL,
CompanyEmployeeID int NOT NULL,
);
CREATE TABLE CustomersPersonalData
(
PersonID int NOT NULL,
FirstName varchar(64) NOT NULL,
LastName varchar(64) NOT NULL,
CONSTRAINT FirstName_CustomersPersonalData_c CHECK (FirstName LIKE '[A-Z]%'),
CONSTRAINT LastName_CustomersPersonalData_c CHECK (LastName LIKE '[A-Z]%'),
CONSTRAINT CustomersPersonalData_pk PRIMARY KEY (PersonID)
);
CREATE TABLE Customers
(
CustomerID int NOT NULL,
Street varchar(64) NOT NULL,
Country varchar(64) NOT NULL,
City varchar(64) NOT NULL,
PostCode varchar(16) NOT NULL,
Phone varchar(16) NOT NULL,
Email varchar(64) NOT NULL,
CONSTRAINT Country_Customers_c CHECK (Country LIKE '[A-Z]%'),
CONSTRAINT City_Customers_c CHECK (City LIKE '[A-Z]%'),
CONSTRAINT Street_Customers_c CHECK (Street LIKE '[A-Z]%'),
CONSTRAINT Customers_pk PRIMARY KEY (CustomerID)
);
CREATE TABLE IndividualCustomers
(
CustomerID int NOT NULL,
PersonID int NOT NULL,
CONSTRAINT IndividualCustomers_pk PRIMARY KEY (CustomerID)
);
CREATE TABLE DiningTables
(
DiningTableID int NOT NULL,
NumberOfSeats int NOT NULL,
CONSTRAINT NumberOfSeats_DiningTables_c CHECK (NumberOfSeats > 0),
CONSTRAINT DiningTables_pk PRIMARY KEY (DiningTableID)
);
CREATE TABLE Invoices
(
InvoiceID int NOT NULL,
OrderID int NOT NULL,
CONSTRAINT Invoices_pk PRIMARY KEY (InvoiceID)
);
CREATE TABLE Menu
(
MenuID int NOT NULL,
MenuName varchar(64) NOT NULL,
FromTime datetime NOT NULL,
ToTime datetime NULL DEFAULT NULL,
CONSTRAINT Proper_Dates_Menu_c CHECK (FromTime <= ToTime OR ToTime IS NULL),
CONSTRAINT Menu_pk PRIMARY KEY (MenuID)
);
CREATE TABLE MenuDetails
(
MenuID int NOT NULL,
ProductID int NOT NULL,
);
CREATE TABLE OrderDetails
(
OrderID int NOT NULL,
ProductID int NOT NULL,
Quantity int NOT NULL,
CONSTRAINT Quantity_OrderDetails_c CHECK (Quantity >= 0),
);
CREATE TABLE Orders
(
OrderID int NOT NULL,
CustomerID int NOT NULL,
OrderDate datetime NOT NULL,
CollectDate datetime NULL,
PaymentDate datetime NULL DEFAULT NULL,
PayVia int NULL,
OrderStatus varchar(64) NOT NULL,
RestaurantEmployeeID int NOT NULL,
DiscountPercent int NOT NULL DEFAULT 0,
CONSTRAINT Orders_pk PRIMARY KEY (OrderID)
);
CREATE TABLE PaymentMethod
(
PaymentID int NOT NULL,
PaymentName varchar(64) NOT NULL,
CONSTRAINT PaymentMethod_pk PRIMARY KEY (PaymentID)
)
CREATE TABLE ProductIngredients
(
ProductID int NOT NULL,
IngredientID int NOT NULL,
);
CREATE TABLE IngredientsWarehouse
(
IngredientID int NOT NULL,
IngredientName varchar(64) NOT NULL,
QuantityLeft int NOT NULL,
CONSTRAINT QuantityLeft_IngredientsWarehouse_c CHECK (QuantityLeft >= 0),
CONSTRAINT IngredientsWarehouse_pk PRIMARY KEY (IngredientID)
);
CREATE TABLE Products
(
ProductID int NOT NULL,
ProductName varchar(64) NOT NULL,
CategoryID int NOT NULL,
CONSTRAINT Products_pk PRIMARY KEY (ProductID)
);
CREATE TABLE ProductPrices
(
ProductID int NOT NULL,
FromTime datetime NOT NULL,
ToTime datetime NULL DEFAULT NULL,
UnitPrice int NOT NULL,
CONSTRAINT Proper_Dates_ProductPrices_c CHECK (FromTime <= ToTime OR ToTime IS NULL),
CONSTRAINT UnitPrice_ProductPrices_c CHECK (UnitPrice >= 0),
);
CREATE TABLE Categories
(
CategoryID int NOT NULL,
CategoryName varchar(64) NOT NULL,
CONSTRAINT Categories_pk PRIMARY KEY (CategoryID)
);
CREATE TABLE Reservation
(
ReservationID int NOT NULL,
FromTime datetime NOT NULL,
ToTime datetime NOT NULL,
Seats int NOT NULL,
DiningTableID int NOT NULL,
OrderID int NOT NULL,
CONSTRAINT Seats_Reservation_c CHECK (Seats <= 40 AND Seats > 0),
CONSTRAINT Proper_Dates_Reservation_c CHECK (FromTime < ToTime),
CONSTRAINT Reservation_pk PRIMARY KEY (ReservationID)
);
CREATE TABLE RestaurantEmployees
(
RestaurantEmployeeID int NOT NULL,
FirstName varchar(64) NOT NULL,
LastName varchar(64) NOT NULL,
Occupation varchar(64) NOT NULL,
Street varchar(64) NOT NULL,
Country varchar(64) NOT NULL,
City varchar(64) NOT NULL,
PostCode varchar(16) NOT NULL,
Phone char(9) NOT NULL,
Email varchar(64) NOT NULL,
CONSTRAINT City_RestaurantEmployees_c CHECK ((City LIKE '[A-Z]%')),
CONSTRAINT Country_RestaurantEmployees_c CHECK ((Country LIKE '[A-Z]%')),
CONSTRAINT Street_RestaurantEmployees_c CHECK ((Street LIKE '[A-Z]%')),
CONSTRAINT Name_Validation_RestaurantEmployees_c CHECK ((FirstName LIKE '[A-Z]%') AND (LastName LIKE '[A-Z]%')),
CONSTRAINT RestaurantEmployees_pk PRIMARY KEY (RestaurantEmployeeID)
);
CREATE TABLE EmployeesSalary
(
RestaurantEmployeeID int NOT NULL,
FromTime datetime NOT NULL,
ToTime datetime NULL DEFAULT NULL,
Salary int NOT NULL,
CONSTRAINT Salary_EmployeesSalary_c CHECK (Salary >= 0),
CONSTRAINT Proper_Dates_EmployeesSalary_c CHECK (FromTime <= ToTime OR ToTime IS NULL),
);
CREATE TABLE Takeaway
(
OrderID int NOT NULL,
PickupDate datetime NULL DEFAULT NULL,
CONSTRAINT Takeaway_pk PRIMARY KEY (OrderID)
);
CREATE TABLE VariablesData
(
FromTime datetime NOT NULL,
ToTime datetime NULL DEFAULT NULL,
VariableType varchar(3) NOT NULL,
VariableValue int NOT NULL,
CONSTRAINT Proper_Dates_VariablesData_c CHECK (FromTime <= ToTime OR ToTime IS NULL),
CONSTRAINT VariableValue_VariablesData_c CHECK (VariablesData.VariableValue >= 0)
);
CREATE TABLE TempDiscount
(
CustomerID int NOT NULL,
FromTime datetime NOT NULL,
ToTime datetime NULL DEFAULT NULL,
DiscountPercent int NOT NULL DEFAULT 0,
CONSTRAINT Proper_Dates_TempDiscount_c CHECK (FromTime <= ToTime OR ToTime IS NULL),
CONSTRAINT TempDiscount_DiscountPercent_c CHECK (TempDiscount.DiscountPercent >= 0)
);
-- Foreign Keys
ALTER TABLE Products
ADD CONSTRAINT Categories_Products
FOREIGN KEY (CategoryID)
REFERENCES Categories (CategoryID);
ALTER TABLE Companies
ADD CONSTRAINT Companies_Customers
FOREIGN KEY (CustomerID)
REFERENCES Customers (CustomerID);
ALTER TABLE CompanyEmployees
ADD CONSTRAINT CompanyEmployees_Companies
FOREIGN KEY (CompanyID)
REFERENCES Companies (CompanyID);
ALTER TABLE EmployeesSalary
ADD CONSTRAINT EmployeesSalary_RestaurantEmployee
FOREIGN KEY (RestaurantEmployeeID)
REFERENCES RestaurantEmployees (RestaurantEmployeeID);
ALTER TABLE CompanyReservationParticipants
ADD CONSTRAINT CompanyEmployees_CompanyReservationParticipants
FOREIGN KEY (CompanyEmployeeID)
REFERENCES CompanyEmployees (CompanyEmployeeID);
ALTER TABLE CompanyReservationParticipants
ADD CONSTRAINT CompanyReservationParticipants_Reservation
FOREIGN KEY (ReservationID)
REFERENCES Reservation (ReservationID);
ALTER TABLE CompanyEmployees
ADD CONSTRAINT CustomerPersonalData_CompanyEmployees
FOREIGN KEY (PersonID)
REFERENCES CustomersPersonalData (PersonID);
ALTER TABLE IndividualCustomers
ADD CONSTRAINT CustomerPersonalData_IndividualCustomers
FOREIGN KEY (PersonID)
REFERENCES CustomersPersonalData (PersonID);
ALTER TABLE Orders
ADD CONSTRAINT Customers_Orders
FOREIGN KEY (CustomerID)
REFERENCES Customers (CustomerID);
ALTER TABLE IndividualCustomers
ADD CONSTRAINT IndividualCustomers_Customers
FOREIGN KEY (CustomerID)
REFERENCES Customers (CustomerID);
ALTER TABLE ProductIngredients
ADD CONSTRAINT IngredientsWarehouse_ProductIngredients
FOREIGN KEY (IngredientID)
REFERENCES IngredientsWarehouse (IngredientID);
ALTER TABLE MenuDetails
ADD CONSTRAINT MenuDetails_Products
FOREIGN KEY (ProductID)
REFERENCES Products (ProductID);
ALTER TABLE MenuDetails
ADD CONSTRAINT Menu_MenuDetails
FOREIGN KEY (MenuID)
REFERENCES Menu (MenuID);
ALTER TABLE Invoices
ADD CONSTRAINT Orders_Invoices
FOREIGN KEY (OrderID)
REFERENCES Orders (OrderID);
ALTER TABLE OrderDetails
ADD CONSTRAINT Orders_OrderDetails
FOREIGN KEY (OrderID)
REFERENCES Orders (OrderID);
ALTER TABLE Reservation
ADD CONSTRAINT Orders_Reservation
FOREIGN KEY (OrderID)
REFERENCES Orders (OrderID);
ALTER TABLE Orders
ADD CONSTRAINT Orders_RestaurantEmployees
FOREIGN KEY (RestaurantEmployeeID)
REFERENCES RestaurantEmployees (RestaurantEmployeeID);
ALTER TABLE Takeaway
ADD CONSTRAINT Orders_Takeaway
FOREIGN KEY (OrderID)
REFERENCES Orders (OrderID);
ALTER TABLE OrderDetails
ADD CONSTRAINT Products_OrderDetails
FOREIGN KEY (ProductID)
REFERENCES Products (ProductID);
ALTER TABLE ProductIngredients
ADD CONSTRAINT Products_ProductIngredients
FOREIGN KEY (ProductID)
REFERENCES Products (ProductID);
ALTER TABLE Reservation
ADD CONSTRAINT Reservation_DiningTables
FOREIGN KEY (DiningTableID)
REFERENCES DiningTables (DiningTableID);
ALTER TABLE ProductPrices
ADD CONSTRAINT ProductPrices_Products
FOREIGN KEY (ProductID)
REFERENCES Products (ProductID);
ALTER TABLE Orders
ADD CONSTRAINT Orders_PaymentMethod
FOREIGN KEY (PayVia)
REFERENCES PaymentMethod (PaymentID);
ALTER TABLE TempDiscount
ADD CONSTRAINT TempDiscount_Customers
FOREIGN KEY (CustomerID)
REFERENCES Customers (CustomerID);
GO
-- VIEWS --
CREATE VIEW Current_Menu_View AS
SELECT Products.ProductID ,Products.ProductName, ProductPrices.UnitPrice
FROM MenuDetails
JOIN Products ON Products.ProductID = MenuDetails.ProductID
JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE MenuID = (SELECT MenuID
FROM Menu
WHERE ToTime IS NULL)
AND ProductPrices.ToTime IS NULL
GO
-- Available_Products_View
CREATE VIEW Available_Products_View AS
SELECT Products.ProductID, ProductName
from ProductIngredients
JOIN IngredientsWarehouse ON ProductIngredients.IngredientID = IngredientsWarehouse.IngredientID
JOIN Products ON ProductIngredients.ProductID = Products.ProductID
GROUP BY Products.ProductID, ProductName
HAVING MIN(QuantityLeft) > 0
GO
-- Not_Available_Products_View
CREATE VIEW Not_Available_Products_View AS
SELECT Products.ProductID, ProductName
from ProductIngredients
JOIN IngredientsWarehouse ON ProductIngredients.IngredientID = IngredientsWarehouse.IngredientID
JOIN Products ON ProductIngredients.ProductID = Products.ProductID
GROUP BY Products.ProductID, ProductName
HAVING MIN(QuantityLeft) = 0
GO
-- Not_Available_Ingredients_View
CREATE VIEW Not_Available_Ingredients_View AS
SELECT IngredientID, IngredientName
from IngredientsWarehouse
WHERE QuantityLeft = 0
GO
--Not_Paid_Orders_View
CREATE VIEW Not_Paid_Orders_View AS
SELECT CustomerID, RestaurantEmployeeID, OrderID
FROM Orders
WHERE OrderStatus like 'awaiting payment'
GO
--Today_Reservations_View
CREATE VIEW Today_Reservations_View AS
SELECT FromTime, ToTime, Seats, Orders.PaymentDate, CustomersPersonalData.FirstName, CustomersPersonalData.LastName
FROM Reservation
JOIN Orders ON Orders.OrderID = Reservation.OrderID
JOIN Customers ON Customers.CustomerID = Orders.CustomerID
JOIN IndividualCustomers ON IndividualCustomers.CustomerID = Customers.CustomerID
JOIN CustomersPersonalData ON CustomersPersonalData.PersonID = IndividualCustomers.PersonID
WHERE FromTime = (SELECT CAST( GETDATE() AS Date ))
GO
--Orders_Pending_For_Confirmation_View
CREATE VIEW Orders_Pending_For_Confirmation_View AS
SELECT CustomerID, RestaurantEmployeeID, OrderID
FROM Orders
WHERE OrderStatus like '%not_confirmed%'
GO
--Order_Details_View
CREATE VIEW Order_Details_View
AS
SELECT Customers.CustomerID,CustomersPersonalData.FirstName, CustomersPersonalData.LastName,Orders.OrderID, OrderDetails.Quantity,
ProductPrices.UnitPrice, OrderDate, PaymentDate, PaymentMethod.PaymentName, OrderStatus, RestaurantEmployees.RestaurantEmployeeID
FROM Orders
JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
JOIN Products ON Products.ProductID = OrderDetails.ProductID
JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
JOIN PaymentMethod ON PaymentMethod.PaymentId = Orders.PayVia
JOIN RestaurantEmployees ON RestaurantEmployees.RestaurantEmployeeID = Orders.RestaurantEmployeeID
JOIN Customers ON Customers.CustomerID = Orders.CustomerID
JOIN IndividualCustomers ON IndividualCustomers.CustomerID = Customers.CustomerID
JOIN CustomersPersonalData ON CustomersPersonalData.PersonID = IndividualCustomers.PersonID
GO
--Report_Of_Total_Orders_Products_Price_View
CREATE VIEW Total_Orders_Products_Prices_Report_View AS
SELECT TOP 1
(SELECT COUNT(OrderID) FROM Orders
WHERE MONTH(Orders.OrderDate) = MONTH(GETDATE())
AND YEAR(Orders.OrderDate) = YEAR(GETDATE())) AS [total number of orders for the last month],
(SELECT COUNT(OrderID) FROM Orders
WHERE DATEPART(WEEK,Orders.OrderDate) = DATEPART(WEEK,GETDATE()) AND YEAR(Orders.OrderDate) = YEAR(GETDATE()))
AS [total number of orders for the last week],
(SELECT SUM(Quantity) FROM OrderDetails INNER JOIN Orders ON Orders.OrderID = OrderDetails.OrderID
WHERE MONTH(Orders.OrderDate) = MONTH(GETDATE())
AND YEAR(Orders.OrderDate) = YEAR(GETDATE())) AS [total number of sold products for the last month],
(SELECT SUM(Quantity) FROM OrderDetails INNER JOIN Orders ON Orders.OrderID = OrderDetails.OrderID
WHERE DATEPART(WEEK,Orders.OrderDate) = DATEPART(WEEK,GETDATE()) AND YEAR(Orders.OrderDate) = YEAR(GETDATE()))
AS [total number of sold products for the last week],
(SELECT SUM(table2.calkowitaSuma) FROM (SELECT Orders.OrderID,SUM(OrderDetails.Quantity*ProductPrices.UnitPrice*(1-(Orders.DiscountPercent/100.0))) as calkowitaSuma
FROM Orders INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON Products.ProductID = ProductPrices.ProductID
WHERE MONTH(Orders.OrderDate) = MONTH(GETDATE())
AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
AND Orders.OrderDate >= ProductPrices.FromTime AND (ProductPrices.ToTime is NULL OR ProductPrices.ToTime >= Orders.OrderDate)
GROUP BY Orders.OrderID) AS table2 ) AS [total order price for the last month],
(SELECT SUM(table2.calkowitaSuma) FROM (SELECT Orders.OrderID,SUM(OrderDetails.Quantity*ProductPrices.UnitPrice*(1-(Orders.DiscountPercent/100.0))) as calkowitaSuma
FROM Orders INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON Products.ProductID = OrderDetails.ProductID
INNER JOIN ProductPrices ON Products.ProductID = ProductPrices.ProductID
WHERE DATEPART(WEEK,Orders.OrderDate) = DATEPART(WEEK,GETDATE()) AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
AND Orders.OrderDate >= ProductPrices.FromTime AND (ProductPrices.ToTime is NULL OR ProductPrices.ToTime >= Orders.OrderDate)
GROUP BY Orders.OrderID) AS table2 ) AS [total order price for the last week]
FROM Orders
GO
--Average_Salary_Of_Restaurant_Employee_View
CREATE VIEW Average_Salary_Of_Restaurant_Employee_View AS
SELECT RestaurantEmployees.RestaurantEmployeeID ,FirstName,LastName, ROUND(AVG(Salary),2) as [srednie zarobki]
FROM RestaurantEmployees INNER JOIN EmployeesSalary ON RestaurantEmployees.RestaurantEmployeeID = EmployeesSalary.RestaurantEmployeeID
GROUP BY RestaurantEmployees.RestaurantEmployeeID ,FirstName,LastName
GO
--Five_Best_Employees_View
CREATE VIEW Five_Best_Employees_View AS
SELECT TOP 5 RestaurantEmployees.RestaurantEmployeeID, FirstName,LastName FROM RestaurantEmployees
LEFT JOIN Orders ON Orders.RestaurantEmployeeID = RestaurantEmployees.RestaurantEmployeeID
GROUP BY RestaurantEmployees.RestaurantEmployeeID, FirstName,LastName
ORDER BY COUNT(OrderID) DESC
GO
--Total_Products_Sales_View
CREATE VIEW Total_Products_Sales_View AS
SELECT ProductName, SUM(Quantity) AS TotalOrders
FROM OrderDetails
JOIN Products ON Products.ProductID = OrderDetails.ProductID
GROUP BY ProductName
GO
--Total_Categories_Sales_View
CREATE VIEW Total_Categories_Sales_View AS
SELECT Categories.CategoryName, SUM(Quantity) AS TotalOrders
FROM OrderDetails
JOIN Products ON Products.ProductID = OrderDetails.ProductID
JOIN Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY Categories.CategoryName
GO
--Available_Tables_View
CREATE VIEW Available_Tables_View AS
SELECT DISTINCT DiningTables.DiningTableID, DiningTables.NumberOfSeats
FROM Reservation
RIGHT JOIN DiningTables ON DiningTables.DiningTableID = Reservation.DiningTableID
WHERE ToTime < GETDATE() OR ToTime IS NULL
GO
--Total_Reservation_Report_for_Customers_View
CREATE VIEW Total_Reservation_Report_for_Customers_View AS
SELECT
(SELECT COUNT(*) FROM Reservation
INNER JOIN Orders ON Reservation.OrderID = Orders.OrderID
INNER JOIN Customers ON Customers.CustomerID = Reservation.ReservationID
INNER JOIN IndividualCustomers ON IndividualCustomers.CustomerID = Customers.CustomerID
WHERE MONTH(Orders.OrderDate) = MONTH(GETDATE())
AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
) as [ilość dokonanych rezerwacji prywatnie w tym miesiącu],
(SELECT COUNT(*) FROM Reservation
INNER JOIN Orders ON Reservation.OrderID = Orders.OrderID
INNER JOIN Customers ON Customers.CustomerID = Reservation.ReservationID
INNER JOIN IndividualCustomers ON IndividualCustomers.CustomerID = Customers.CustomerID
WHERE DATEPART(WEEK,Orders.OrderDate) = DATEPART(WEEK,GETDATE()) AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
) as [ilosc dokonanych rezerwacji prywatnie w tym tygodniu],
(SELECT COUNT(*) FROM Reservation
INNER JOIN Orders ON Reservation.OrderID = Orders.OrderID
INNER JOIN Customers ON Customers.CustomerID = Reservation.ReservationID
INNER JOIN Companies ON Companies.CustomerID = Customers.CustomerID
WHERE MONTH(Orders.OrderDate) = MONTH(GETDATE())
AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
) as [ilosc dokonanych rezerwacji na firmę w tym miesiacu],
(SELECT COUNT(*) FROM Reservation
INNER JOIN Orders ON Reservation.OrderID = Orders.OrderID
INNER JOIN Customers ON Customers.CustomerID = Reservation.ReservationID
INNER JOIN Companies ON Companies.CustomerID = Customers.CustomerID
WHERE DATEPART(WEEK,Orders.OrderDate) = DATEPART(WEEK,GETDATE()) AND YEAR(Orders.OrderDate) = YEAR(GETDATE())
) as [ilosc dokonanych rezerwacji na firmę w tym tygodniu]
GO
-- CurrentMenuSalesStatsView
CREATE VIEW CurrentMenuSalesStatsView
AS
SELECT Products.ProductName, COUNT(Products.ProductName) AS Total
FROM Products
LEFT JOIN OrderDetails ON OrderDetails.ProductID = Products.ProductID
LEFT JOIN Orders ON Orders.OrderID = OrderDetails.ProductID
WHERE (OrderDate > (SELECT FromTime
FROM Menu
WHERE ToTime IS NULL)
OR OrderDate IS NULL)
AND Products.ProductID IN (SELECT ProductID
FROM MenuDetails
WHERE MenuID = (SELECT MenuID
FROM Menu
WHERE ToTime IS NULL))
GROUP BY Products.ProductName
GO
-- TotalCustomersDiscountsView
CREATE VIEW TotalCustomersDiscountsView
AS
SELECT Customers.CustomerID, SUM(ISNULL((DiscountPercent / 100.0) * (UnitPrice * Quantity), 0)) AS TotalDisocunt
FROM Customers
LEFT JOIN Orders ON Orders.CustomerID = Customers.CustomerID
LEFT JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
LEFT JOIN ProductPrices ON OrderDetails.ProductID = ProductPrices.ProductID
WHERE ProductPrices.ToTime IS NULL
GROUP BY Customers.CustomerID
GO
-- OrderStatisticsView
CREATE VIEW OrderStatisticsView
AS
SELECT
(SELECT COUNT(*) FROM Orders) as [całkowita liczba zamówień],
(SELECT SUM(ProductPrices.UnitPrice*Quantity*(1- (DiscountPercent/100.0))) FROM Orders
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
INNER JOIN ProductPrices ON ProductPrices.ProductID = Products.ProductID
WHERE ProductPrices.FromTime < Orders.OrderDate AND (ProductPrices.ToTime IS NULL OR ProductPrices.ToTime > Orders.OrderDate)
) as [całkowita cena zrealizowanych zamówień],
(SELECT COUNT(*) FROM Orders
INNER JOIN Customers ON Customers.CustomerID = Orders.CustomerID
INNER JOIN IndividualCustomers ON IndividualCustomers.CustomerID = Customers.CustomerID
) as [ilość zamowien dla klientow indywidualnych],
(SELECT COUNT(*) FROM Orders
INNER JOIN Customers ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Companies ON Companies.CustomerID = Customers.CustomerID
) as [ilość zamowien dla klientów firmowych],
(SELECT COUNT(*) FROM Orders WHERE PaymentDate IS NULL) as [ilość zamówień nieopłaconych],
(SELECT COUNT(*) FROM Orders WHERE CollectDate IS NULL) as [ilość zamówień nieodebranych],
(SELECT TOP 1 OrderDate FROM Orders ORDER BY OrderDate DESC) as [data ostatnio zrealizowanego zamówienia]
GO
-- PROCEDURES --
CREATE PROCEDURE AddCategoryProcedure
@CategoryName varchar(64)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM Categories
WHERE @CategoryName = CategoryName
)
BEGIN
;
THROW 52000, 'Kategoria jest już dodana', 1 -- nwm o co chodzi z tymi numerami
end
DECLARE @CategoryID INT
SELECT @CategoryID = ISNULL(MAX(CategoryID), 0) + 1
FROM Categories
INSERT INTO Categories(CategoryID, CategoryName)
VALUES(@CategoryID, @CategoryName);
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd dodawania kategorii: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- usuwanie kategorii
CREATE PROCEDURE RemoveCategoryProcedure
@CategoryName varchar(64)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM Categories
WHERE @CategoryName = CategoryName
)
BEGIN
;
DELETE FROM Categories WHERE @CategoryName = CategoryName
end
ELSE
THROW 52000, 'Kategoria jest już dodana', 1 -- nwm o co chodzi z tymi numerami
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd usuwania kategorii: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- dodawanie produktu
CREATE PROCEDURE AddProductProcedure
@CategoryName varchar(64),
@Name varchar(64)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM Products
WHERE ProductName = @Name
)
BEGIN
;
THROW 52000, 'Potrawa jest już w bazie', 1
END
IF NOT EXISTS(
SELECT *
FROM Categories
WHERE CategoryName = @CategoryName
)
BEGIN
;
THROW 52000, 'Brak kategorii', 1
END
DECLARE @CategoryID INT
SELECT @CategoryID = CategoryID
FROM Categories
WHERE CategoryName = @CategoryName
DECLARE @ProductID INT
SELECT @ProductID = ISNULL(MAX(ProductID), 0) + 1
FROM Products
INSERT INTO Products(ProductID, ProductName, CategoryID)
VALUES (@ProductID, @Name, @CategoryID);
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd dodawania produktu kategorii: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- usunięcie produktu
CREATE PROCEDURE RemoveProductProcedure
@CategoryID int,
@Name varchar(64)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM Products
WHERE ProductName = @Name
)
BEGIN
;
DELETE FROM Products WHERE @CategoryID = Products.CategoryID AND @Name=ProductName
END
IF NOT EXISTS(
SELECT *
FROM Categories
WHERE @CategoryID = CategoryID
)
BEGIN
;
THROW 52000, 'Brak kategorii', 1
END
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd usuwania produktu: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
--dodawanie stolika do restauracji
CREATE PROCEDURE AddTableProcedure
@capacity int
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
DECLARE @DinigTableID INT
SELECT @DinigTableID = ISNULL(MAX(DiningTables.DiningTableID), 0) + 1
FROM DiningTables
INSERT INTO DiningTables(DiningTableID, NumberOfSeats)
VALUES(@DinigTableID, @capacity);
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd dodawania stolika: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
--usuwanie stolika z restauracji
CREATE PROCEDURE RemoveTableProcedure
@tableID int
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM DiningTables
WHERE DiningTableID = @tableID
)
BEGIN
;
DELETE FROM DiningTables WHERE @tableID = DiningTableID
END
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd usuwania stolika: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- dodawanie pracownika
CREATE PROCEDURE AddEmployeeProcedure
@FirstName varchar(64),
@LastName varchar(64),
@Occupation varchar(64),
@Street varchar(64),
@Country varchar(64),
@City varchar(64),
@PostCode varchar(16),
@Phone char(9),
@Email varchar(64)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM RestaurantEmployees
WHERE FirstName = @FirstName AND LastName = @LastName AND Occupation = @Occupation AND Street=@Street AND Country = @Country AND City = @City AND PostCode=@PostCode AND Phone = @Phone AND Email=@Email
)
BEGIN
;
THROW 52000, 'Pracownik jest już w bazie', 1
END
DECLARE @RestaurantEmployeeID INT
SELECT @RestaurantEmployeeID = ISNULL(MAX(RestaurantEmployeeID), 0) + 1
FROM RestaurantEmployees
INSERT INTO RestaurantEmployees(RestaurantEmployeeID,FirstName, LastName, Occupation, Street, Country, City, PostCode,Phone,Email)
VALUES (@RestaurantEmployeeID, @FirstName, @LastName, @Occupation, @Street, @Country, @City, @PostCode,@Phone,@Email);
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd dodawania pracownika: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- usunięcie pracownika
CREATE PROCEDURE RemoveEmployeeProcedure
@RestaurantEmployeeID INT
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM RestaurantEmployees
WHERE RestaurantEmployeeID = @RestaurantEmployeeID
)
BEGIN
;
DELETE FROM RestaurantEmployees
WHERE @RestaurantEmployeeID = RestaurantEmployeeID
END
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Błąd usuwania pracownika: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- AddMenuProcedure
CREATE PROCEDURE AddMenuProcedure
@MenuName varchar(64),
@FromTime datetime,
@ToTime datetime
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM Menu
WHERE MenuName = @MenuName
)
BEGIN
;
THROW 52000, 'Menu with provided name is already in the database', 1
END
DECLARE @MenuID INT
SELECT @MenuID = ISNULL(MAX(MenuID), 0) + 1
FROM Menu
INSERT INTO Menu(MenuID, MenuName, FromTime, ToTime)
VALUES (@MenuID, @MenuName, @FromTime, @ToTime);
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Error adding menu: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- RemoveMenuProcedure
CREATE PROCEDURE RemoveMenuProcedure
@MenuID int
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM Menu
WHERE MenuID = @MenuID
)
BEGIN
;
DELETE FROM Menu WHERE @MenuID = MenuID
END
ELSE
BEGIN
;
THROW 52000, 'Menu with provided ID does not exist', 1
END
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Error removing menu: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- AddIngredientToWarehouse
CREATE PROCEDURE AddIngredientToWarehouse
@IngredientName varchar(64),
@QuantityLeft int
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM IngredientsWarehouse
WHERE IngredientName = @IngredientName
)
BEGIN
;
THROW 52000, 'Ingredient with provided name is already in the database', 1
END
DECLARE @IngredientID INT
SELECT @IngredientID = ISNULL(MAX(IngredientID), 0) + 1
FROM IngredientsWarehouse
INSERT INTO IngredientsWarehouse(IngredientID, IngredientName, QuantityLeft)
VALUES (@IngredientID, @IngredientName, @QuantityLeft);
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Error adding ingredient: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- RemoveIngredientFromWarehouse
CREATE PROCEDURE RemoveIngredientFromWarehouse
@IngredientID int
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS(
SELECT *
FROM IngredientsWarehouse
WHERE IngredientID = @IngredientID
)
BEGIN
;
DELETE FROM IngredientsWarehouse WHERE IngredientID = @IngredientID
END
ELSE
BEGIN
;
THROW 52000, 'Ingredient with provided ID does not exist', 1
END
END TRY
BEGIN CATCH
DECLARE @errormsg nvarchar(2048) =
'Error removing Ingredient: ' + ERROR_MESSAGE();
THROW 52000, @errormsg, 1;
END CATCH
END
go
-- AddIngredientToProduct
CREATE PROCEDURE AddIngredientToProduct
@ProductID int,
@IngredientID int
AS
BEGIN