| 1 | = Логички и физички дизајн |
| 2 | |
| 3 | == DDL скрипта |
| 4 | |
| 5 | {{{#!sql |
| 6 | -- DDL for Deleting Tables |
| 7 | DROP TABLE IF EXISTS ORDER_PRODUCT; |
| 8 | DROP TABLE IF EXISTS INVOICE; |
| 9 | DROP TABLE IF EXISTS PAYMENT; |
| 10 | DROP TABLE IF EXISTS ORDER; |
| 11 | DROP TABLE IF EXISTS TRANSPORT; |
| 12 | DROP TABLE IF EXISTS RECEIVER; |
| 13 | DROP TABLE IF EXISTS BUYER; |
| 14 | DROP TABLE IF EXISTS PRODUCT; |
| 15 | DROP TABLE IF EXISTS PRODUCER; |
| 16 | |
| 17 | -- DDL for Creating Tables |
| 18 | |
| 19 | -- Creating the PRODUCER table |
| 20 | CREATE TABLE PRODUCER ( |
| 21 | producer_id SERIAL PRIMARY KEY, |
| 22 | company_name VARCHAR(100), |
| 23 | country_of_origin VARCHAR(30), |
| 24 | tax_id VARCHAR(20), |
| 25 | registration_number VARCHAR(30), |
| 26 | bank_account_number VARCHAR(20), |
| 27 | bank_name VARCHAR(100), |
| 28 | swift_number VARCHAR(20), |
| 29 | iban VARCHAR(34), |
| 30 | export_license_number VARCHAR(20) |
| 31 | ); |
| 32 | |
| 33 | -- Creating the PRODUCT table |
| 34 | CREATE TABLE PRODUCT ( |
| 35 | product_id SERIAL PRIMARY KEY, |
| 36 | name VARCHAR(50), |
| 37 | hs_code VARCHAR(10), |
| 38 | description TEXT, |
| 39 | unit_of_measure VARCHAR(10), |
| 40 | price DECIMAL(10, 2), |
| 41 | producer_id INT REFERENCES PRODUCER(producer_id) |
| 42 | ); |
| 43 | |
| 44 | -- Creating the BUYER table |
| 45 | CREATE TABLE BUYER ( |
| 46 | buyer_id SERIAL PRIMARY KEY, |
| 47 | company_name VARCHAR(100), |
| 48 | billing_address VARCHAR(200), |
| 49 | country VARCHAR(50), |
| 50 | tax_id VARCHAR(20), |
| 51 | registration_number VARCHAR(30) |
| 52 | ); |
| 53 | |
| 54 | -- Creating the RECEIVER table |
| 55 | CREATE TABLE RECEIVER ( |
| 56 | receiver_id SERIAL PRIMARY KEY, |
| 57 | company_name VARCHAR(100), |
| 58 | shipping_address VARCHAR(200), |
| 59 | country VARCHAR(50), |
| 60 | phone_number VARCHAR(15), |
| 61 | contact_person VARCHAR(100) |
| 62 | ); |
| 63 | |
| 64 | -- Creating the TRANSPORT table |
| 65 | CREATE TABLE TRANSPORT ( |
| 66 | transport_id SERIAL PRIMARY KEY, |
| 67 | carrier_name VARCHAR(100), |
| 68 | departure_point VARCHAR(50), |
| 69 | arrival_point VARCHAR(50), |
| 70 | estimated_departure DATE, |
| 71 | estimated_arrival DATE, |
| 72 | total_distance DECIMAL(10, 2), |
| 73 | special_requirements TEXT, |
| 74 | insurance_conditions VARCHAR(200), |
| 75 | incoterm VARCHAR(20) |
| 76 | ); |
| 77 | |
| 78 | -- Creating the ORDER table |
| 79 | CREATE TABLE ORDER ( |
| 80 | order_id SERIAL PRIMARY KEY, |
| 81 | order_date DATE, |
| 82 | estimated_delivery_date DATE, |
| 83 | status VARCHAR(20), |
| 84 | employee_id INT, |
| 85 | buyer_id INT REFERENCES BUYER(buyer_id), |
| 86 | receiver_id INT REFERENCES RECEIVER(receiver_id), |
| 87 | transport_id INT REFERENCES TRANSPORT(transport_id) |
| 88 | ); |
| 89 | |
| 90 | -- Creating the PAYMENT table |
| 91 | CREATE TABLE PAYMENT ( |
| 92 | payment_id SERIAL PRIMARY KEY, |
| 93 | amount DECIMAL(10, 2), |
| 94 | exchange_rate DECIMAL(10, 6), |
| 95 | currency VARCHAR(3), |
| 96 | payment_status VARCHAR(20), |
| 97 | payment_method VARCHAR(20), |
| 98 | due_date DATE, |
| 99 | payment_date DATE, |
| 100 | order_id INT REFERENCES ORDER(order_id) |
| 101 | ); |
| 102 | |
| 103 | -- Creating the INVOICE table |
| 104 | CREATE TABLE INVOICE ( |
| 105 | invoice_key SERIAL PRIMARY KEY, |
| 106 | invoice_date DATE, |
| 107 | status VARCHAR(20), |
| 108 | total_amount DECIMAL(10, 2), |
| 109 | order_id INT REFERENCES ORDER(order_id) |
| 110 | ); |
| 111 | |
| 112 | -- Creating the ORDER_PRODUCT table (Many-to-Many relationship between ORDER and PRODUCT) |
| 113 | CREATE TABLE ORDER_PRODUCT ( |
| 114 | order_id INT REFERENCES ORDER(order_id), |
| 115 | product_id INT REFERENCES PRODUCT(product_id), |
| 116 | PRIMARY KEY (order_id, product_id) |
| 117 | ); |
| 118 | }}} |
| 119 | |
| 120 | == DML скрипта |
| 121 | |
| 122 | {{{#!sql |
| 123 | INSERT INTO PRODUCER (company_name, country_of_origin, tax_id, registration_number, bank_account_number, bank_name, swift_number, iban, export_license_number) VALUES |
| 124 | ('Producer A', 'Country X', 'TX123', 'RN123', 'BAN123', 'Bank A', 'SW123', 'IBAN123', 'EL123'); |
| 125 | |
| 126 | INSERT INTO PRODUCT (name, hs_code, description, unit_of_measure, price, producer_id) VALUES |
| 127 | ('Product 1', 'HS101', 'Description 1', 'Unit 1', 100.00, 1), |
| 128 | ('Product 2', 'HS102', 'Description 2', 'Unit 2', 150.00, 1); |
| 129 | |
| 130 | INSERT INTO BUYER (company_name, billing_address, country, tax_id, registration_number) VALUES |
| 131 | ('Buyer A', 'Address 1', 'Country Y', 'TX456', 'RN456'); |
| 132 | |
| 133 | INSERT INTO RECEIVER (company_name, shipping_address, country, phone_number, contact_person) VALUES |
| 134 | ('Receiver A', 'Shipping Address 1', 'Country Z', '1234567890', 'Contact A'); |
| 135 | |
| 136 | INSERT INTO TRANSPORT (carrier_name, departure_point, arrival_point, estimated_departure, estimated_arrival, total_distance, special_requirements, insurance_conditions, incoterm) VALUES |
| 137 | ('Transport A', 'Point A', 'Point B', '2022-01-01', '2022-01-02', 1000.00, 'None', 'Condition A', 'FOB'); |
| 138 | |
| 139 | INSERT INTO ORDER (order_date, estimated_delivery_date, status, employee_id, buyer_id, receiver_id, transport_id) VALUES |
| 140 | ('2022-01-01', '2022-02-01', 'Pending', 1, 1, 1, 1); |
| 141 | |
| 142 | INSERT INTO PAYMENT (amount, exchange_rate, currency, payment_status, payment_method, due_date, payment_date, order_id) VALUES |
| 143 | (1000.00, 1.000000, 'USD', 'Completed', 'Credit Card', '2022-01-15', '2022-01-10', 1); |
| 144 | |
| 145 | INSERT INTO INVOICE (invoice_date, status, total_amount, order_id) VALUES |
| 146 | ('2022-01-10', 'Issued', 1000.00, 1); |
| 147 | |
| 148 | INSERT INTO ORDER_PRODUCT (order_id, product_id) VALUES |
| 149 | (1, 1), |
| 150 | (1, 2); |
| 151 | }}} |
| 152 | |
| 153 | == DBeaver релациски дијаграм |
| 154 | |
| 155 | [[Image(DBeaver_Relational_Diagram.png)]] |