-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarShowroom.sql
413 lines (337 loc) · 13.7 KB
/
carShowroom.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
create database carshowroom
use carshowroom
CREATE TABLE Inventory (
VINs VARCHAR(17) Primary key,
LastUpdated DATE
--FOREIGN KEY (VIN) REFERENCES Car(VIN) --removve one field in cd
);
CREATE TABLE Car (
VIN VARCHAR(17) PRIMARY KEY,
Make VARCHAR(50),
Model VARCHAR(50),
Price INT,
Year INT,
LastServiceDate DATE,
Owner INT, --give customer id here when bought other zero
Color VARCHAR(20),
Mileage INT, --change in class diagram
AvailabilityStatus VARCHAR(20),
FOREIGN KEY (VIN) REFERENCES Inventory(VINs)
);
-- Base User table
CREATE TABLE Users ( --change user to users
UserID INT PRIMARY KEY,
Name VARCHAR(100),
EmailID VARCHAR (255) UNIQUE,
Password VARCHAR(255),
ContactInformation VARCHAR(50)
);
-- Customer table (inherits from Users)
CREATE TABLE Customer (
ownedCars varchar(17),
CustomerID INT PRIMARY KEY,
numberOfCarsOwned INT,
FOREIGN KEY (CustomerID) REFERENCES Users(UserID),
foreign key (ownedCars) references Car(VIN)
);
-- Manager table (inherits from Users)
CREATE TABLE Manager (
ManagerID INT PRIMARY KEY,
Department VARCHAR(50), --change in cd
FOREIGN KEY (ManagerID) REFERENCES Users(UserID)
);
CREATE TABLE Staff (
StaffID INT PRIMARY KEY, --change name in cd
Designation VARCHAR(50),
FOREIGN KEY (StaffID) REFERENCES Users(UserID)
);
CREATE TABLE ServiceSchedule (
VIN VARCHAR(17),
ServiceType VARCHAR(50),
Date DATE,
Status VARCHAR(20),
Comments TEXT,
PRIMARY KEY (VIN, Date),
FOREIGN KEY (VIN) REFERENCES Inventory(VINs)
);
CREATE TABLE TestDrive (
VIN VARCHAR(17),
CustomerID INT,
Date DATE,
Time TIME,
Status VARCHAR(20),
Comments TEXT,
PRIMARY KEY (VIN, CustomerID),
FOREIGN KEY (VIN) REFERENCES Inventory(VINs),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);
CREATE TABLE PurchaseOrder (
OrderID INT PRIMARY KEY,
CustomerID INT, --remove customer details
Date DATE,
VIN VARCHAR(17),
Comments TEXT,
Status VARCHAR(20),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
FOREIGN KEY (VIN) REFERENCES Inventory(VINs)
);
CREATE TABLE Invoice (
InvoiceID INT PRIMARY KEY,
OrderID INT,
CustomerID INT,
--Tax DECIMAL(10, 2),
Tax INT,
--Price DECIMAL(10, 2),
Price INT,
--TotalAmount DECIMAL(10, 2),
TotalAmount Float,
Date DATE,
TermsAndConditions TEXT,
FOREIGN KEY (OrderID) REFERENCES PurchaseOrder(OrderID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);
--CREATE TABLE Payment (
-- TransactionID INT PRIMARY KEY,
-- InvoiceID INT,
-- CustomerID INT,
-- Amount DECIMAL(10, 2),
-- Status VARCHAR(20),
-- PaymentDate DATE,
-- PaymentMethod VARCHAR(50),
-- FOREIGN KEY (InvoiceID) REFERENCES Invoice(InvoiceID),
-- FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
--);
-- Common Payment table
CREATE TABLE Payment (
TransactionID INT PRIMARY KEY,
InvoiceID INT, --change order in cd
CustomerID INT,
Amount Float,
Status VARCHAR(20) default 'unpaid',
PaymentDate DATE,
FOREIGN KEY (InvoiceID) REFERENCES Invoice(InvoiceID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);
-- CashPayment table (inherits from Payment)
CREATE TABLE Cash (
TransactionID INT PRIMARY KEY,
AccountNumber VARCHAR(20),
PIN INT,
Bank VARCHAR(50),
FOREIGN KEY (TransactionID) REFERENCES Payment(TransactionID)
);
-- CreditCardPayment table (inherits from Payment)
CREATE TABLE CreditCard (
TransactionID INT PRIMARY KEY,
CreditCardNumber VARCHAR(16),
Code int,
FOREIGN KEY (TransactionID) REFERENCES Payment(TransactionID)
);
--change sales class in cd!!
CREATE TABLE Sales (
SaleID INT PRIMARY KEY,
SaleDate DATE,
carSold VARCHAR(17),
foreign key (carSold) references Inventory(VINs)
);
CREATE TABLE Report (
ReportID INT PRIMARY KEY, --change in cd
Name VARCHAR(100),
Type VARCHAR(50),
DateGenerated DATE,
FilterProperty VARCHAR(255)
);
-- Select all rows from Users
SELECT * FROM Users;
-- Select all rows from Inventory
SELECT * FROM Inventory;
-- Select all rows from Car
SELECT * FROM Car;
-- Select all rows from Customer
SELECT * FROM Customer;
-- Select all rows from Manager
SELECT * FROM Manager;
-- Select all rows from Staff
SELECT * FROM Staff;
-- Select all rows from ServiceSchedule
SELECT * FROM ServiceSchedule;
-- Select all rows from TestDrive
SELECT * FROM TestDrive;
-- Select all rows from PurchaseOrder
SELECT * FROM PurchaseOrder;
-- Select all rows from Invoice
SELECT * FROM Invoice;
-- Select all rows from Payment
SELECT * FROM Payment;
-- Select all rows from Cash
SELECT * FROM Cash;
-- Select all rows from CreditCard
SELECT * FROM CreditCard;
-- Select all rows from Sales
SELECT * FROM Sales;
-- Select all rows from Report
SELECT * FROM Report;
--change for inventory
--DELIMITER //
--CREATE TRIGGER AfterPaymentInsert
--AFTER INSERT ON Payment
--FOR EACH ROW
--BEGIN
-- DECLARE car_status VARCHAR(20);
-- DECLARE owner_id INT;
-- -- Fetch car status and owner id from Inventory based on the VIN
-- SELECT CarStatus, OwnerID INTO car_status, owner_id
-- FROM Inventory
-- WHERE VIN = NEW.VIN;
-- -- Check if payment status is Successful
-- IF NEW.Status = 'Successful' THEN
-- -- Insert into Sales table
-- INSERT INTO Sales (VIN, NumberOfCarsSold, SaleDate)
-- VALUES (NEW.VIN, 1, NEW.SaleDate);
-- -- Update Inventory table
-- UPDATE Inventory
-- SET CarStatus = 'Sold', OwnerID = NEW.CustomerID, LastUpdated = CURRENT_DATE
-- WHERE VIN = NEW.VIN;
-- END IF;
--END;
--//
--DELIMITER ;
--go
--CREATE TRIGGER AfterPaymentInsert
--ON Payment
--AFTER INSERT
--AS
--BEGIN
-- SET NOCOUNT ON;
-- DECLARE @car_status VARCHAR(20);
-- DECLARE @owner_id INT;
-- -- Fetch car status and owner id from Inventory based on the VIN
-- SELECT @car_status = CarStatus, @owner_id = OwnerID
-- FROM Inventory
-- WHERE VIN = (SELECT VIN FROM INSERTED);
-- -- Check if payment status is Successful
-- IF (SELECT Status FROM INSERTED) = 'Successful'
-- BEGIN
-- -- Insert into Sales table
-- INSERT INTO Sales (VIN, NumberOfCarsSold, SaleDate)
-- VALUES ((SELECT VIN FROM INSERTED), 1, (SELECT SaleDate FROM INSERTED));
-- -- Update Inventory table
-- UPDATE Inventory
-- SET CarStatus = 'Sold', OwnerID = (SELECT CustomerID FROM INSERTED), LastUpdated = GETDATE()
-- WHERE VIN = (SELECT VIN FROM INSERTED);
-- END
--END;
--go
---insertion
-- Insert into Users
INSERT INTO Users (UserID, Name, EmailID, Password, ContactInformation) VALUES (1, 'John Doe', '[email protected]', 'password123', '555-1234');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES ('ABC123');
INSERT INTO Inventory (VINs) VALUES ('DEF567');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES ('ABC123', 'Toyota', 'Camry', 25000, 2022, '2023-10-01', 0, 'Blue', 5000, 'Available');
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES ('DEF567', 'Ford', 'Escape', 28000, 2023, '2023-11-01', 0, 'Silver', 2000, 'Available');
--muust modiFY all these insertions below commented, not right
---- Insert into Customer
--INSERT INTO Customer (CustomerID, numberOfCarsOwned, ownedCars) VALUES (1, 1, 'ABC123');
---- Insert into ServiceSchedule
--INSERT INTO ServiceSchedule (VIN, ServiceType, Date, Status, Comments) VALUES ('ABC123', 'Oil Change', '2023-11-18', 'Scheduled', 'Routine maintenance');
---- Insert into TestDrive
--INSERT INTO TestDrive (VIN, CustomerID, Date, Time, Status, Comments) VALUES ('DEF567', 1, '2023-11-20', '14:00:00', 'Completed', 'Test drive for potential purchase');
---- Insert into PurchaseOrder
--INSERT INTO PurchaseOrder (OrderID, Date, VIN, Comments, Status) VALUES (1, '2023-11-21', 'DEF567', 'Interested in buying', 'Pending');
---- Insert into Invoice
--INSERT INTO Invoice (InvoiceID, OrderID, CustomerID, Tax, Price, TotalAmount, Date, TermsAndConditions) VALUES (1, 1, 1, 5, 28000, 28005, '2023-11-22', 'Payment due in 30 days');
---- Insert into Payment
--INSERT INTO Payment (TransactionID, InvoiceID, CustomerID, Amount, PaymentDate) VALUES (1, 1, 1, 28005, '2023-11-23');
---- Insert into Cash
--INSERT INTO Cash (TransactionID, AccountNumber, PIN, Bank) VALUES (1, '9876543210', 4321, 'Secure Bank');
---- Insert into CreditCard
--INSERT INTO CreditCard (TransactionID, CreditCardNumber, Code) VALUES (1, '1234567890123456', 789);
---- Insert into Sales
--INSERT INTO Sales (SaleID, SaleDate, carSold) VALUES (1, '2023-11-24', 'DEF567');
---- Insert into Report
--INSERT INTO Report (ReportID, Name, Type, DateGenerated, FilterProperty) VALUES (1, 'SalesReport', 'Monthly', '2023-11-25', 'Type=New');
INSERT INTO Users (UserID, Name, EmailID, Password, ContactInformation) VALUES
(2, 'Jane Smith', '[email protected]', 'pass456', '555-5678'),
(3, 'Alice Johnson', '[email protected]', 'pass789', '555-7890'),
(4, 'Bob Wilson', '[email protected]', 'pass123', '555-4567');
INSERT INTO Inventory (VINs) VALUES
('GHI789'),
('JKL012');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('GHI789', 'Honda', 'Accord', 30000, 2021, '2023-09-15', 0, 'Red', 8000, 'Available'),
('JKL012', 'Chevrolet', 'Malibu', 27000, 2022, '2023-11-05', 0, 'White', 6000, 'Available');
-- Insert into Users
INSERT INTO Users (UserID, Name, EmailID, Password, ContactInformation) VALUES
(5, 'Eva Rodriguez', '[email protected]', 'pass567', '555-2345'),
(6, 'Charlie Brown', '[email protected]', 'pass890', '555-6789');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('MNO345'),
('PQR678');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('MNO345', 'Nissan', 'Altima', 26000, 2022, '2023-10-20', 0, 'Black', 7000, 'Available'),
('PQR678', 'Hyundai', 'Elantra', 23000, 2021, '2023-09-10', 0, 'Silver', 9000, 'Available');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('STU901'),
('VWX234');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('STU901', 'Toyota', 'Rav4', 32000, 2023, '2023-11-15', 0, 'Green', 4000, 'Available'),
('VWX234', 'Honda', 'Civic', 28000, 2022, '2023-10-05', 0, 'Blue', 5500, 'Available');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('YZA567'),
('BCD890');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('YZA567', 'Ford', 'Explorer', 35000, 2023, '2023-11-25', 0, 'Silver', 3000, 'Available'),
('BCD890', 'Chevrolet', 'Equinox', 30000, 2022, '2023-10-10', 0, 'Black', 6000, 'Available');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('EFG123'),
('HIJ456');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('EFG123', 'Toyota', 'Highlander', 38000, 2023, '2023-11-30', 0, 'Red', 2000, 'Available'),
('HIJ456', 'Honda', 'Pilot', 40000, 2022, '2023-11-20', 0, 'White', 5000, 'Available');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('KLM789'),
('NOP012');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('KLM789', 'Ford', 'Fusion', 32000, 2023, '2023-11-15', 0, 'Blue', 3000, 'Available'),
('NOP012', 'Chevrolet', 'Traverse', 35000, 2022, '2023-11-05', 0, 'Black', 4000, 'Available');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('QRS345'),
('TUV678');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('QRS345', 'Nissan', 'Rogue', 30000, 2023, '2023-11-25', 0, 'Silver', 2500, 'Available'),
('TUV678', 'Hyundai', 'Santa Fe', 32000, 2022, '2023-11-20', 0, 'Red', 3500, 'Available');
-- Insert into Inventory
INSERT INTO Inventory (VINs) VALUES
('WXY901'),
('ZAB234'),
('CDE567'),
('FGH890');
-- Insert into Car
INSERT INTO Car (VIN, Make, Model, Price, Year, LastServiceDate, Owner, Color, Mileage, AvailabilityStatus) VALUES
('WXY901', 'Toyota', 'Corolla', 25000, 2023, '2023-11-10', 0, 'White', 1500, 'Available'),
('ZAB234', 'Honda', 'CR-V', 28000, 2022, '2023-10-30', 0, 'Black', 2000, 'Available'),
('CDE567', 'Ford', 'Edge', 32000, 2023, '2023-11-05', 0, 'Blue', 3000, 'Available'),
('FGH890', 'Chevrolet', 'Camaro', 40000, 2022, '2023-10-20', 0, 'Red', 5000, 'Available');
INSERT INTO manager (managerId, department) VALUES (4, 'Car Sales Department');
INSERT INTO manager (managerId, department) values (5, 'Car Sales Deparment');
insert into staff(StaffID, Designation) values (2, 'Inventory Handler');
insert into staff(StaffID, Designation) values (3, 'Service Handler');
insert into customer(ownedCars, customerID, numberOfCarsOwned) values (null, 6, null);
insert into customer(ownedCars, customerID, numberOfCarsOwned) values (null, 8, null);
insert into customer(ownedCars, customerID, numberOfCarsOwned) values (null, 9, null);
insert into customer(ownedCars, customerID, numberOfCarsOwned) values (null, 10, null);