| 6 | | teo |
| | 6 | |
| | 7 | CREATE TABLE "User" ( |
| | 8 | user_id SERIAL, |
| | 9 | name varchar(255), |
| | 10 | surname varchar(255), |
| | 11 | email varchar(255), |
| | 12 | password varchar(255), |
| | 13 | phone_number varchar(255), |
| | 14 | registration_date date, |
| | 15 | PRIMARY KEY (user_id), |
| | 16 | |
| | 17 | CONSTRAINT email_check CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'), |
| | 18 | |
| | 19 | CONSTRAINT password_complexity CHECK ( |
| | 20 | password ~ '[0-9]' AND |
| | 21 | password ~ '[^a-zA-Z0-9]' |
| | 22 | ) |
| | 23 | ); |
| | 24 | |
| | 25 | CREATE TABLE VehicleType ( |
| | 26 | vehicle_type_id SERIAL, |
| | 27 | vehicle_type varchar(255), |
| | 28 | PRIMARY KEY (vehicle_type_id) |
| | 29 | ); |
| | 30 | |
| | 31 | CREATE TABLE Vehicle ( |
| | 32 | vehicle_id SERIAL, |
| | 33 | registration varchar(255), |
| | 34 | vehicle_type_id int DEFAULT -1, |
| | 35 | PRIMARY KEY (vehicle_id), |
| | 36 | FOREIGN KEY (vehicle_type_id) REFERENCES VehicleType(vehicle_type_id) |
| | 37 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 38 | ); |
| | 39 | |
| | 40 | CREATE TABLE Driver ( |
| | 41 | driver_id int DEFAULT -1, |
| | 42 | vehicle_id int DEFAULT -1, |
| | 43 | status varchar(255), |
| | 44 | hire_date date, |
| | 45 | PRIMARY KEY (driver_id), |
| | 46 | FOREIGN KEY (driver_id) REFERENCES "User"(user_id) |
| | 47 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 48 | FOREIGN KEY (vehicle_id) REFERENCES Vehicle(vehicle_id) |
| | 49 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 50 | ); |
| | 51 | |
| | 52 | CREATE TABLE Customer ( |
| | 53 | customer_id int DEFAULT -1, |
| | 54 | PRIMARY KEY (customer_id), |
| | 55 | FOREIGN KEY (customer_id) REFERENCES "User"(user_id) |
| | 56 | ON DELETE CASCADE ON UPDATE CASCADE |
| | 57 | ); |
| | 58 | |
| | 59 | CREATE TABLE LoyalCustomer ( |
| | 60 | loyalty_id int DEFAULT -1, |
| | 61 | points int DEFAULT 0 CHECK (points >= 0), |
| | 62 | joined_day date, |
| | 63 | birthday date, |
| | 64 | PRIMARY KEY (loyalty_id), |
| | 65 | FOREIGN KEY (loyalty_id) REFERENCES Customer(customer_id) |
| | 66 | ON DELETE CASCADE ON UPDATE CASCADE |
| | 67 | ); |
| | 68 | |
| | 69 | CREATE TABLE City ( |
| | 70 | city_id SERIAL, |
| | 71 | city_name varchar(255), |
| | 72 | postal_code int, |
| | 73 | PRIMARY KEY (city_id) |
| | 74 | ); |
| | 75 | |
| | 76 | CREATE TABLE StoreType ( |
| | 77 | store_type_id SERIAL, |
| | 78 | type_name varchar(255), |
| | 79 | description varchar(255), |
| | 80 | PRIMARY KEY (store_type_id) |
| | 81 | ); |
| | 82 | |
| | 83 | CREATE TABLE Store ( |
| | 84 | store_id SERIAL, |
| | 85 | store_name varchar(255), |
| | 86 | city_id int DEFAULT -1, |
| | 87 | store_type_id int DEFAULT -1, |
| | 88 | rating double precision CHECK (rating >= 0 AND rating <= 5), |
| | 89 | PRIMARY KEY (store_id), |
| | 90 | FOREIGN KEY (city_id) REFERENCES City(city_id) |
| | 91 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 92 | FOREIGN KEY (store_type_id) REFERENCES StoreType(store_type_id) |
| | 93 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 94 | ); |
| | 95 | |
| | 96 | CREATE TABLE DeliveryZone ( |
| | 97 | zone_id SERIAL, |
| | 98 | city_id int DEFAULT -1, |
| | 99 | zone_name varchar(255), |
| | 100 | PRIMARY KEY (zone_id), |
| | 101 | FOREIGN KEY (city_id) REFERENCES City(city_id) |
| | 102 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 103 | ); |
| | 104 | |
| | 105 | CREATE TABLE Address ( |
| | 106 | address_id SERIAL, |
| | 107 | zone_id int DEFAULT -1, |
| | 108 | street varchar(255), |
| | 109 | street_number int, |
| | 110 | door_number int, |
| | 111 | PRIMARY KEY (address_id), |
| | 112 | FOREIGN KEY (zone_id) REFERENCES DeliveryZone(zone_id) |
| | 113 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 114 | ); |
| | 115 | |
| | 116 | CREATE TABLE StoreInstance ( |
| | 117 | store_instance_id SERIAL, |
| | 118 | store_id int DEFAULT -1, |
| | 119 | address_id int DEFAULT -1, |
| | 120 | phone varchar(255), |
| | 121 | zone_id int DEFAULT -1, |
| | 122 | PRIMARY KEY (store_instance_id), |
| | 123 | FOREIGN KEY (store_id) REFERENCES Store(store_id) |
| | 124 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 125 | FOREIGN KEY (address_id) REFERENCES Address(address_id) |
| | 126 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 127 | FOREIGN KEY (zone_id) REFERENCES DeliveryZone(zone_id) |
| | 128 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 129 | ); |
| | 130 | |
| | 131 | CREATE TABLE Delivery_zone_Store_instance ( |
| | 132 | zone_id int DEFAULT -1, |
| | 133 | store_instance_id int DEFAULT -1, |
| | 134 | |
| | 135 | |
| | 136 | FOREIGN KEY (zone_id) REFERENCES DeliveryZone(zone_id) |
| | 137 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 138 | |
| | 139 | FOREIGN KEY (store_instance_id) REFERENCES StoreInstance(store_instance_id) |
| | 140 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 141 | |
| | 142 | PRIMARY KEY (zone_id, store_instance_id) |
| | 143 | ); |
| | 144 | |
| | 145 | CREATE TABLE Product ( |
| | 146 | product_id SERIAL, |
| | 147 | store_id int DEFAULT -1, |
| | 148 | name varchar(255), |
| | 149 | price int DEFAULT 0 CHECK (price >= 0), |
| | 150 | PRIMARY KEY (product_id), |
| | 151 | FOREIGN KEY (store_id) REFERENCES Store(store_id) |
| | 152 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 153 | ); |
| | 154 | |
| | 155 | CREATE TABLE Promotion ( |
| | 156 | promotion_id SERIAL, |
| | 157 | store_id int DEFAULT -1, |
| | 158 | promotion_name varchar(255), |
| | 159 | discount_percent int DEFAULT 0, |
| | 160 | start_date date, |
| | 161 | end_date date, |
| | 162 | PRIMARY KEY (promotion_id), |
| | 163 | FOREIGN KEY (store_id) REFERENCES Store(store_id) |
| | 164 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 165 | CHECK (discount_percent >= 0 AND discount_percent <= 100), |
| | 166 | CHECK (start_date <= end_date) |
| | 167 | ); |
| | 168 | |
| | 169 | CREATE TABLE "Order" ( |
| | 170 | order_id SERIAL, |
| | 171 | customer_id int DEFAULT -1, |
| | 172 | promotion_id int DEFAULT -1, |
| | 173 | driver_id int DEFAULT -1, |
| | 174 | store_instance_id int DEFAULT -1, |
| | 175 | delivery_zone_id int DEFAULT -1, |
| | 176 | status varchar(255), |
| | 177 | delivery_fee int DEFAULT 0 CHECK (delivery_fee >= 0), |
| | 178 | total_price int DEFAULT 0 CHECK (total_price >= 0), |
| | 179 | PRIMARY KEY (order_id), |
| | 180 | FOREIGN KEY (customer_id) REFERENCES Customer(customer_id) |
| | 181 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 182 | FOREIGN KEY (promotion_id) REFERENCES Promotion(promotion_id) |
| | 183 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 184 | FOREIGN KEY (driver_id) REFERENCES Driver(driver_id) |
| | 185 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 186 | FOREIGN KEY (store_instance_id) REFERENCES StoreInstance(store_instance_id) |
| | 187 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 188 | FOREIGN KEY (delivery_zone_id) REFERENCES DeliveryZone(zone_id) |
| | 189 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 190 | ); |
| | 191 | |
| | 192 | CREATE TABLE OrderItem ( |
| | 193 | order_item_id SERIAL, |
| | 194 | order_id int DEFAULT -1, |
| | 195 | product_id int DEFAULT -1, |
| | 196 | quantity int DEFAULT 1 CHECK (quantity > 0), |
| | 197 | PRIMARY KEY (order_item_id), |
| | 198 | FOREIGN KEY (order_id) REFERENCES "Order"(order_id) |
| | 199 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 200 | FOREIGN KEY (product_id) REFERENCES Product(product_id) |
| | 201 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 202 | ); |
| | 203 | |
| | 204 | CREATE TABLE Review ( |
| | 205 | review_id SERIAL, |
| | 206 | order_id int DEFAULT -1, |
| | 207 | customer_id int DEFAULT -1, |
| | 208 | rating double precision CHECK (rating >= 0 AND rating <= 5), |
| | 209 | comment varchar(255), |
| | 210 | review_date date, |
| | 211 | PRIMARY KEY (review_id), |
| | 212 | FOREIGN KEY (order_id) REFERENCES "Order"(order_id) |
| | 213 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 214 | FOREIGN KEY (customer_id) REFERENCES Customer(customer_id) |
| | 215 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 216 | ); |
| | 217 | |
| | 218 | CREATE TABLE Products_review ( |
| | 219 | review_id int DEFAULT -1, |
| | 220 | store_instance_id int DEFAULT -1, |
| | 221 | FOREIGN KEY (review_id ) REFERENCES Review(review_id) |
| | 222 | ON DELETE CASCADE |
| | 223 | ON UPDATE CASCADE, |
| | 224 | FOREIGN KEY (store_instance_id ) REFERENCES StoreInstance(store_instance_id) |
| | 225 | ON DELETE SET default |
| | 226 | ON UPDATE CASCADE, |
| | 227 | PRIMARY KEY(review_id)); |
| | 228 | |
| | 229 | CREATE TABLE Delivery_review ( |
| | 230 | review_id int DEFAULT -1, |
| | 231 | driver_id int DEFAULT -1, |
| | 232 | FOREIGN KEY (review_id ) REFERENCES Review(review_id) |
| | 233 | ON DELETE CASCADE |
| | 234 | ON UPDATE CASCADE, |
| | 235 | FOREIGN KEY (driver_id ) REFERENCES Driver(driver_id) |
| | 236 | ON DELETE SET default |
| | 237 | ON UPDATE CASCADE, |
| | 238 | PRIMARY KEY(review_id)); |
| | 239 | |
| | 240 | |
| | 241 | CREATE TABLE Payment_method ( |
| | 242 | payment_method_id SERIAL, |
| | 243 | payment varchar(255), |
| | 244 | PRIMARY KEY (payment_method_id) |
| | 245 | ); |
| | 246 | |
| | 247 | CREATE TABLE Payment ( |
| | 248 | payment_id SERIAL, |
| | 249 | status varchar(255), |
| | 250 | payment_date date, |
| | 251 | payment_method_id int DEFAULT -1, |
| | 252 | order_id int DEFAULT -1, |
| | 253 | PRIMARY KEY (payment_id), |
| | 254 | FOREIGN KEY (payment_method_id) REFERENCES Payment_method(payment_method_id) |
| | 255 | ON DELETE SET DEFAULT ON UPDATE CASCADE, |
| | 256 | FOREIGN KEY (order_id) REFERENCES "Order"(order_id) |
| | 257 | ON DELETE SET DEFAULT ON UPDATE CASCADE |
| | 258 | ); |
| | 259 | |
| | 260 | CREATE TABLE Customer_payment_method ( |
| | 261 | customer_id int DEFAULT -1, |
| | 262 | payment_method_id int DEFAULT -1, |
| | 263 | date_added date DEFAULT CURRENT_DATE, |
| | 264 | |
| | 265 | -- Композитен примарен клуч |
| | 266 | |
| | 267 | |
| | 268 | -- Референца кон Customer |
| | 269 | FOREIGN KEY (customer_id) REFERENCES Customer(customer_id) |
| | 270 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 271 | |
| | 272 | -- Референца кон Payment_method |
| | 273 | FOREIGN KEY (payment_method_id) REFERENCES Payment_method(payment_method_id) |
| | 274 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 275 | |
| | 276 | PRIMARY KEY (customer_id, payment_method_id) |
| | 277 | ); |
| | 278 | |
| | 279 | CREATE TABLE Customer_Address ( |
| | 280 | customer_id int DEFAULT -1, |
| | 281 | address_id int DEFAULT -1, |
| | 282 | address_label varchar(255), -- Опционално: за ознаки како 'Home', 'Work' |
| | 283 | FOREIGN KEY (customer_id) REFERENCES Customer(customer_id) |
| | 284 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 285 | |
| | 286 | FOREIGN KEY (address_id) REFERENCES Address(address_id) |
| | 287 | ON DELETE CASCADE ON UPDATE CASCADE, |
| | 288 | |
| | 289 | PRIMARY KEY (customer_id, address_id) |
| | 290 | ); |