SQL & DDL: Truck Simulator DDL Final.txt

File Truck Simulator DDL Final.txt, 5.8 KB (added by 213231, 4 days ago)

Final DDL

Line 
1DROP TABLE IF EXISTS `aktivniprodukti`;
2DROP VIEW IF EXISTS `aktivniprodukti`;
3
4CREATE TABLE `customer` (
5 `CustomerID` int NOT NULL,
6 `CustomerName` varchar(100) DEFAULT NULL,
7 `CustomerSurName` varchar(100) DEFAULT NULL,
8 `Email` varchar(100) DEFAULT NULL,
9 `Address` varchar(255) DEFAULT NULL,
10 `CustomerContact` varchar(20) DEFAULT NULL,
11 `Password` varchar(255) NOT NULL,
12 PRIMARY KEY (`CustomerID`)
13) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
14
15CREATE TABLE `customerfeedback` (
16 `FeedbackID` int NOT NULL,
17 `CustomerID` int DEFAULT NULL,
18 `ProductID` int DEFAULT NULL,
19 `Rating` int DEFAULT NULL,
20 `Comment` text,
21 `FeedbackDate` date DEFAULT NULL,
22 `TransactionID` int DEFAULT NULL,
23 PRIMARY KEY (`FeedbackID`),
24 KEY `CustomerID` (`CustomerID`),
25 KEY `ProductID` (`ProductID`),
26 KEY `fk_feedback_transaction` (`TransactionID`),
27 CONSTRAINT `customerfeedback_ibfk_1` FOREIGN KEY (`CustomerID`) REFERENCES `customer` (`CustomerID`),
28 CONSTRAINT `customerfeedback_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`),
29 CONSTRAINT `fk_feedback_transaction` FOREIGN KEY (`TransactionID`) REFERENCES `procurement` (`TransactionID`),
30 CONSTRAINT `customerfeedback_chk_1` CHECK ((`Rating` between 1 and 5))
31) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
32
33CREATE TABLE `employee` (
34 `EmployeeID` int NOT NULL,
35 `EmployeeName` varchar(100) DEFAULT NULL,
36 `EmployeeSurName` varchar(100) DEFAULT NULL,
37 `Position` varchar(100) DEFAULT NULL,
38 `Department` varchar(100) DEFAULT NULL,
39 `Email` varchar(100) DEFAULT NULL,
40 `Password` varchar(100) DEFAULT NULL,
41 PRIMARY KEY (`EmployeeID`)
42) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
43
44CREATE TABLE `maintenance` (
45 `MainID` int NOT NULL,
46 `EmployeeID` int DEFAULT NULL,
47 `ProductID` int DEFAULT NULL,
48 `MainDate` date DEFAULT NULL,
49 `Description` text,
50 `Cost` decimal(10,2) DEFAULT NULL,
51 `Status` enum('Pending','Completed') DEFAULT 'Pending',
52 `StartTime` datetime DEFAULT CURRENT_TIMESTAMP,
53 `EndTime` datetime DEFAULT NULL,
54 PRIMARY KEY (`MainID`),
55 KEY `EmployeeID` (`EmployeeID`),
56 KEY `ProductID` (`ProductID`),
57 CONSTRAINT `maintenance_ibfk_1` FOREIGN KEY (`EmployeeID`) REFERENCES `employee` (`EmployeeID`),
58 CONSTRAINT `maintenance_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`)
59) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
60
61CREATE TABLE `procurement` (
62 `TransactionID` int NOT NULL,
63 `EmployeeID` int DEFAULT NULL,
64 `CustomerID` int DEFAULT NULL,
65 `ProductID` int DEFAULT NULL,
66 `ProcurementDate` date DEFAULT NULL,
67 `Quantity` int DEFAULT NULL,
68 `Status` enum('Pending','Approved','Rejected') DEFAULT 'Pending',
69 `Notified` tinyint(1) DEFAULT '0',
70 `GroupID` varchar(255) DEFAULT NULL,
71 PRIMARY KEY (`TransactionID`),
72 KEY `EmployeeID` (`EmployeeID`),
73 KEY `CustomerID` (`CustomerID`),
74 KEY `ProductID` (`ProductID`),
75 CONSTRAINT `procurement_ibfk_1` FOREIGN KEY (`EmployeeID`) REFERENCES `employee` (`EmployeeID`),
76 CONSTRAINT `procurement_ibfk_2` FOREIGN KEY (`CustomerID`) REFERENCES `customer` (`CustomerID`),
77 CONSTRAINT `procurement_ibfk_3` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`)
78) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
79
80CREATE TABLE `product` (
81 `ProductID` int NOT NULL,
82 `Model` varchar(100) DEFAULT NULL,
83 `Price` decimal(10,2) DEFAULT NULL,
84 `LicensePlate` varchar(50) DEFAULT NULL,
85 `Status` enum('available','sold','rented','maintenance') NOT NULL,
86 PRIMARY KEY (`ProductID`)
87) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
88
89CREATE TABLE `t_type` (
90 `TransactionID` int NOT NULL,
91 `Type` enum('Rent','Buy') NOT NULL,
92 `Duration` int DEFAULT NULL,
93 `MonthlyPay` decimal(10,2) DEFAULT NULL,
94 `TotalPrice` decimal(10,2) DEFAULT NULL,
95 PRIMARY KEY (`TransactionID`),
96 CONSTRAINT `t_type_ibfk_1` FOREIGN KEY (`TransactionID`) REFERENCES `procurement` (`TransactionID`)
97) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
98
99CREATE TABLE `trailer` (
100 `ProductID` int NOT NULL,
101 `Capacity` decimal(10,2) DEFAULT NULL,
102 PRIMARY KEY (`ProductID`),
103 CONSTRAINT `trailer_ibfk_1` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`)
104) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
105
106CREATE TABLE `truck` (
107 `ProductID` int NOT NULL,
108 `HP` int DEFAULT NULL,
109 PRIMARY KEY (`ProductID`),
110 CONSTRAINT `truck_ibfk_1` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`)
111) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
112
113CREATE TABLE `views` (
114 `CustomerID` int NOT NULL,
115 `ProductID` int NOT NULL,
116 PRIMARY KEY (`CustomerID`,`ProductID`),
117 KEY `ProductID` (`ProductID`),
118 CONSTRAINT `views_ibfk_1` FOREIGN KEY (`CustomerID`) REFERENCES `customer` (`CustomerID`),
119 CONSTRAINT `views_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`)
120) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
121
122CREATE TABLE `wallet` (
123 `WalletID` int NOT NULL AUTO_INCREMENT,
124 `CustomerID` int NOT NULL,
125 `Balance` decimal(10,2) NOT NULL DEFAULT '0.00',
126 `CardNumber` varchar(16) DEFAULT NULL,
127 `ExpiryDate` date DEFAULT NULL,
128 `CVV` varchar(4) DEFAULT NULL,
129 `CardHolderName` varchar(100) DEFAULT NULL,
130 PRIMARY KEY (`WalletID`),
131 KEY `CustomerID` (`CustomerID`),
132 CONSTRAINT `wallet_ibfk_1` FOREIGN KEY (`CustomerID`) REFERENCES `customer` (`CustomerID`)
133) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
134
135-- VIEWS
136CREATE VIEW `aktivniprodukti` AS
137SELECT `product`.`ProductID` AS `ProductID`,
138 `product`.`Model` AS `Model`,
139 `product`.`Price` AS `Price`,
140 `product`.`LicensePlate` AS `LicensePlate`
141FROM `product`
142WHERE (`product`.`Status` = 'available');