| 1 | CREATE TABLE customer (
|
|---|
| 2 | CustomerID integer PRIMARY KEY,
|
|---|
| 3 | CustomerName varchar(100),
|
|---|
| 4 | CustomerSurName varchar(100),
|
|---|
| 5 | Email varchar(100),
|
|---|
| 6 | Address varchar(255),
|
|---|
| 7 | CustomerContact varchar(20),
|
|---|
| 8 | Password varchar(255) NOT NULL
|
|---|
| 9 | );
|
|---|
| 10 |
|
|---|
| 11 | CREATE TABLE employee (
|
|---|
| 12 | EmployeeID integer PRIMARY KEY,
|
|---|
| 13 | EmployeeName varchar(100),
|
|---|
| 14 | EmployeeSurName varchar(100),
|
|---|
| 15 | Position varchar(100),
|
|---|
| 16 | Department varchar(100),
|
|---|
| 17 | Email varchar(100),
|
|---|
| 18 | Password varchar(100)
|
|---|
| 19 | );
|
|---|
| 20 |
|
|---|
| 21 | CREATE TABLE product (
|
|---|
| 22 | ProductID integer PRIMARY KEY,
|
|---|
| 23 | Model varchar(100),
|
|---|
| 24 | Price numeric(10,2),
|
|---|
| 25 | LicensePlate varchar(50),
|
|---|
| 26 | Status varchar(15) NOT NULL,
|
|---|
| 27 | CONSTRAINT product_status_check CHECK (Status IN ('available','sold','rented','maintenance'))
|
|---|
| 28 | );
|
|---|
| 29 |
|
|---|
| 30 | -- Потоа откако ќе се креираат табелите горе, продолжи со оние што реферираат од нив:
|
|---|
| 31 | CREATE TABLE procurement (
|
|---|
| 32 | TransactionID integer PRIMARY KEY,
|
|---|
| 33 | EmployeeID integer,
|
|---|
| 34 | CustomerID integer,
|
|---|
| 35 | ProductID integer,
|
|---|
| 36 | ProcurementDate date,
|
|---|
| 37 | Quantity integer,
|
|---|
| 38 | Status varchar(10) DEFAULT 'Pending',
|
|---|
| 39 | Notified boolean DEFAULT FALSE,
|
|---|
| 40 | GroupID varchar(255),
|
|---|
| 41 | CONSTRAINT procurement_employee_fk FOREIGN KEY (EmployeeID) REFERENCES employee(EmployeeID),
|
|---|
| 42 | CONSTRAINT procurement_customer_fk FOREIGN KEY (CustomerID) REFERENCES customer(CustomerID),
|
|---|
| 43 | CONSTRAINT procurement_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID)
|
|---|
| 44 | );
|
|---|
| 45 |
|
|---|
| 46 | CREATE TABLE maintenance (
|
|---|
| 47 | MainID integer PRIMARY KEY,
|
|---|
| 48 | EmployeeID integer,
|
|---|
| 49 | ProductID integer,
|
|---|
| 50 | MainDate date,
|
|---|
| 51 | Description text,
|
|---|
| 52 | Cost numeric(10,2),
|
|---|
| 53 | Status varchar(10) DEFAULT 'Pending',
|
|---|
| 54 | StartTime timestamp DEFAULT CURRENT_TIMESTAMP,
|
|---|
| 55 | EndTime timestamp,
|
|---|
| 56 | CONSTRAINT maintenance_status_check CHECK (Status IN ('Pending','Completed')),
|
|---|
| 57 | CONSTRAINT maintenance_employee_fk FOREIGN KEY (EmployeeID) REFERENCES employee(EmployeeID),
|
|---|
| 58 | CONSTRAINT maintenance_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID)
|
|---|
| 59 | );
|
|---|
| 60 |
|
|---|
| 61 | CREATE TABLE trailer (
|
|---|
| 62 | ProductID integer PRIMARY KEY,
|
|---|
| 63 | Capacity numeric(10,2),
|
|---|
| 64 | CONSTRAINT trailer_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID)
|
|---|
| 65 | );
|
|---|
| 66 |
|
|---|
| 67 | CREATE TABLE truck (
|
|---|
| 68 | ProductID integer PRIMARY KEY,
|
|---|
| 69 | HP integer,
|
|---|
| 70 | CONSTRAINT truck_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID)
|
|---|
| 71 | );
|
|---|
| 72 |
|
|---|
| 73 | CREATE TABLE views (
|
|---|
| 74 | CustomerID integer NOT NULL,
|
|---|
| 75 | ProductID integer NOT NULL,
|
|---|
| 76 | PRIMARY KEY (CustomerID, ProductID),
|
|---|
| 77 | CONSTRAINT views_customer_fk FOREIGN KEY (CustomerID) REFERENCES customer(CustomerID),
|
|---|
| 78 | CONSTRAINT views_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID)
|
|---|
| 79 | );
|
|---|
| 80 |
|
|---|
| 81 | CREATE TABLE wallet (
|
|---|
| 82 | WalletID serial PRIMARY KEY,
|
|---|
| 83 | CustomerID integer NOT NULL,
|
|---|
| 84 | Balance numeric(10,2) NOT NULL DEFAULT 0.00,
|
|---|
| 85 | CardNumber varchar(16),
|
|---|
| 86 | ExpiryDate date,
|
|---|
| 87 | CVV varchar(4),
|
|---|
| 88 | CardHolderName varchar(100),
|
|---|
| 89 | CONSTRAINT wallet_customer_fk FOREIGN KEY (CustomerID) REFERENCES customer(CustomerID)
|
|---|
| 90 | );
|
|---|
| 91 |
|
|---|
| 92 | CREATE TABLE customerfeedback (
|
|---|
| 93 | FeedbackID integer PRIMARY KEY,
|
|---|
| 94 | CustomerID integer,
|
|---|
| 95 | ProductID integer,
|
|---|
| 96 | Rating integer CHECK (Rating BETWEEN 1 AND 5),
|
|---|
| 97 | Comment text,
|
|---|
| 98 | FeedbackDate date,
|
|---|
| 99 | TransactionID integer,
|
|---|
| 100 | CONSTRAINT feedback_customer_fk FOREIGN KEY (CustomerID) REFERENCES customer(CustomerID),
|
|---|
| 101 | CONSTRAINT feedback_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID),
|
|---|
| 102 | CONSTRAINT feedback_transaction_fk FOREIGN KEY (TransactionID) REFERENCES procurement(TransactionID)
|
|---|
| 103 | );
|
|---|
| 104 |
|
|---|
| 105 | CREATE TABLE procurement_request (
|
|---|
| 106 | RequestID serial PRIMARY KEY,
|
|---|
| 107 | CustomerID integer NOT NULL,
|
|---|
| 108 | ProductID integer NOT NULL,
|
|---|
| 109 | Quantity integer DEFAULT 1,
|
|---|
| 110 | RequestedAt timestamp DEFAULT CURRENT_TIMESTAMP,
|
|---|
| 111 | Status varchar(10) DEFAULT 'Pending',
|
|---|
| 112 | PaymentMethod varchar(20),
|
|---|
| 113 | PaymentStatus varchar(10) DEFAULT 'Reserved',
|
|---|
| 114 | MonthlyPay numeric(10,2),
|
|---|
| 115 | TotalPrice numeric(10,2),
|
|---|
| 116 | Duration integer,
|
|---|
| 117 | CardID integer,
|
|---|
| 118 | TransactionType varchar(10) DEFAULT 'Buy',
|
|---|
| 119 | GroupID varchar(36),
|
|---|
| 120 | CONSTRAINT procurement_request_customer_fk FOREIGN KEY (CustomerID) REFERENCES customer(CustomerID),
|
|---|
| 121 | CONSTRAINT procurement_request_product_fk FOREIGN KEY (ProductID) REFERENCES product(ProductID),
|
|---|
| 122 | CONSTRAINT procurement_request_status_check CHECK (Status IN ('Pending','Approved','Rejected')),
|
|---|
| 123 | CONSTRAINT procurement_request_paymentstatus_check CHECK (PaymentStatus IN ('Reserved','Charged','Refunded')),
|
|---|
| 124 | CONSTRAINT procurement_request_transactiontype_check CHECK (TransactionType IN ('Buy','Rent'))
|
|---|
| 125 | );
|
|---|
| 126 |
|
|---|
| 127 | CREATE TABLE t_type (
|
|---|
| 128 | TransactionID integer PRIMARY KEY,
|
|---|
| 129 | Type varchar(10) NOT NULL,
|
|---|
| 130 | Duration integer,
|
|---|
| 131 | MonthlyPay numeric(10,2),
|
|---|
| 132 | TotalPrice numeric(10,2),
|
|---|
| 133 | CONSTRAINT t_type_type_check CHECK (Type IN ('Rent','Buy')),
|
|---|
| 134 | CONSTRAINT t_type_transaction_fk FOREIGN KEY (TransactionID) REFERENCES procurement(TransactionID)
|
|---|
| 135 | );
|
|---|