| 1 | == Релациска шема (со мапирачка трансформација) |
| 2 | |
| 3 | === Ознаки |
| 4 | |
| 5 | - Примарните клучеви се означени со bold и underline |
| 6 | - Надворешните клучеви се означени со * |
| 7 | |
| 8 | |
| 9 | === Табели |
| 10 | |
| 11 | ''Client'' (**__telephone_number__**, name, last_name)\\ |
| 12 | ''Motorcycle'' (**__registration__**, chassis_number, year, model, **telephone_number** *Client)\\ |
| 13 | ''Service'' (**__service_id__**, service_date, total_price, **telephone_number** *Client, **registration** *Motorcycle)\\ |
| 14 | ''Mechanic'' (**__embg__**, name, last_name)\\ |
| 15 | ''Service_Mechanic'' (**__service_id *Service, mechanic_embg *Mechanic__**)\\ |
| 16 | ''Item'' (**__item_id__**, price, tax, description, unit_of_measurement)\\ |
| 17 | ''Service_Item'' (**__service_id *Service, item_id *Item __**, number_of_units, total_price)\\ |
| 18 | ''!ServiceInvoice'' (**__invoice_number__**, date, photo_copy, **recipient** *Client, **service_id** *Service)\\ |
| 19 | ''!FiscalBill'' (**__fiscal_id__**, datetime, total_price, ddv, edb, description, **issuer** *Client, **invoice_number** *ServiceInvoice)\\ |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | == DDL скрипта за бришење на табелите и креирање на табелите |
| 25 | |
| 26 | {{{ |
| 27 | -- Create database |
| 28 | CREATE DATABASE motorcycle_service; |
| 29 | |
| 30 | -- Connect to database |
| 31 | \c motorcycle_service; |
| 32 | |
| 33 | -- Create tables |
| 34 | CREATE TABLE Client ( |
| 35 | telephone_number VARCHAR PRIMARY KEY, |
| 36 | name VARCHAR NOT NULL, |
| 37 | last_name VARCHAR NOT NULL |
| 38 | ); |
| 39 | |
| 40 | CREATE TABLE Motorcycle ( |
| 41 | registration VARCHAR PRIMARY KEY, |
| 42 | chassis_number VARCHAR NOT NULL, |
| 43 | year DATE NOT NULL, |
| 44 | model VARCHAR NOT NULL, |
| 45 | telephone_number VARCHAR NOT NULL, |
| 46 | FOREIGN KEY (telephone_number) REFERENCES Client(telephone_number) |
| 47 | ); |
| 48 | |
| 49 | CREATE TABLE Service ( |
| 50 | service_id SERIAL PRIMARY KEY, |
| 51 | service_date DATE NOT NULL, |
| 52 | total_price NUMERIC NOT NULL, |
| 53 | telephone_number VARCHAR NOT NULL, |
| 54 | registration VARCHAR NOT NULL, |
| 55 | FOREIGN KEY (telephone_number) REFERENCES Client(telephone_number), |
| 56 | FOREIGN KEY (registration) REFERENCES Motorcycle(registration) |
| 57 | ); |
| 58 | |
| 59 | CREATE TABLE Mechanic ( |
| 60 | embg NUMERIC PRIMARY KEY, |
| 61 | name VARCHAR NOT NULL, |
| 62 | last_name VARCHAR NOT NULL |
| 63 | ); |
| 64 | |
| 65 | CREATE TABLE Service_Mechanic ( |
| 66 | service_id INTEGER, |
| 67 | mechanic_embg NUMERIC, |
| 68 | PRIMARY KEY (service_id, mechanic_embg), |
| 69 | FOREIGN KEY (service_id) REFERENCES Service(service_id), |
| 70 | FOREIGN KEY (mechanic_embg) REFERENCES Mechanic(embg) |
| 71 | ); |
| 72 | |
| 73 | CREATE TABLE ServiceInvoice ( |
| 74 | invoice_number SERIAL PRIMARY KEY, |
| 75 | date DATE NOT NULL, |
| 76 | photo_copy VARCHAR NOT NULL, |
| 77 | recipient VARCHAR NOT NULL, |
| 78 | service_id INTEGER UNIQUE NOT NULL, |
| 79 | FOREIGN KEY (recipient) REFERENCES Client(telephone_number), |
| 80 | FOREIGN KEY (service_id) REFERENCES Service(service_id) |
| 81 | ); |
| 82 | |
| 83 | CREATE TABLE FiscalBill ( |
| 84 | fiscal_id SERIAL PRIMARY KEY, |
| 85 | datetime TIMESTAMP NOT NULL, |
| 86 | total_price NUMERIC NOT NULL, |
| 87 | ddv NUMERIC NOT NULL, |
| 88 | edb VARCHAR NOT NULL, |
| 89 | description VARCHAR, |
| 90 | issuer VARCHAR NOT NULL, |
| 91 | invoice_number INTEGER NOT NULL, |
| 92 | FOREIGN KEY (issuer) REFERENCES Client(telephone_number), |
| 93 | FOREIGN KEY (invoice_number) REFERENCES ServiceInvoice(invoice_number) |
| 94 | ); |
| 95 | |
| 96 | CREATE TABLE Item ( |
| 97 | item_id SERIAL PRIMARY KEY, |
| 98 | price NUMERIC NOT NULL, |
| 99 | tax NUMERIC NOT NULL, |
| 100 | description VARCHAR, |
| 101 | unit_of_measurement VARCHAR |
| 102 | ); |
| 103 | |
| 104 | CREATE TABLE Service_Item ( |
| 105 | service_id INTEGER, |
| 106 | item_id INTEGER, |
| 107 | number_of_units NUMERIC NOT NULL, |
| 108 | total_price NUMERIC NOT NULL, |
| 109 | PRIMARY KEY (service_id, item_id), |
| 110 | FOREIGN KEY (service_id) REFERENCES Service(service_id), |
| 111 | FOREIGN KEY (item_id) REFERENCES Item(item_id) |
| 112 | ); |
| 113 | }}} |
| 114 | |
| 115 | == DML скрипта за полнење на табелите со податоци |
| 116 | |
| 117 | {{{ |
| 118 | -- Insert Clients |
| 119 | INSERT INTO Client (telephone_number, name, last_name) VALUES |
| 120 | ('+38970123456', 'John', 'Doe'), |
| 121 | ('+38971234567', 'Jane', 'Smith'), |
| 122 | ('+38972345678', 'Bob', 'Johnson'); |
| 123 | |
| 124 | -- Insert Motorcycles |
| 125 | INSERT INTO Motorcycle (registration, chassis_number, year, model, telephone_number) VALUES |
| 126 | ('SK-1234-AB', 'CH123456789', '2020-01-01', 'Honda CBR600RR', '+38970123456'), |
| 127 | ('SK-5678-CD', 'CH987654321', '2019-01-01', 'Yamaha R1', '+38971234567'), |
| 128 | ('SK-9012-EF', 'CH456789123', '2021-01-01', 'Kawasaki Ninja', '+38970123456'); |
| 129 | |
| 130 | -- Insert Mechanics |
| 131 | INSERT INTO Mechanic (embg, name, last_name) VALUES |
| 132 | (1234567890123, 'Mike', 'Wilson'), |
| 133 | (2345678901234, 'Sarah', 'Brown'), |
| 134 | (3456789012345, 'Tom', 'Davis'); |
| 135 | |
| 136 | -- Insert Items |
| 137 | INSERT INTO Item (item_id, description, unit_of_measurement, price, tax) VALUES |
| 138 | (1, 'Oil Filter', 'piece', 15.00, 1.50), |
| 139 | (2, 'Engine Oil', 'liter', 10.00, 1.00), |
| 140 | (3, 'Air Filter', 'piece', 25.00, 2.50); |
| 141 | |
| 142 | -- Insert Services |
| 143 | INSERT INTO Service (service_date, total_price, telephone_number, registration) VALUES |
| 144 | ('2024-01-05', 150.00, '+38970123456', 'SK-1234-AB'), |
| 145 | ('2024-01-06', 200.00, '+38971234567', 'SK-5678-CD'), |
| 146 | ('2024-01-07', 175.00, '+38970123456', 'SK-9012-EF'); |
| 147 | |
| 148 | -- Insert Service_Mechanic relationships |
| 149 | INSERT INTO Service_Mechanic (service_id, mechanic_embg) VALUES |
| 150 | (1, 1234567890123), |
| 151 | (1, 2345678901234), |
| 152 | (2, 2345678901234), |
| 153 | (3, 3456789012345); |
| 154 | |
| 155 | -- Insert Service_Item relationships |
| 156 | INSERT INTO Service_Item (service_id, item_id, number_of_units, total_price) VALUES |
| 157 | (1, 1, 1, 15.00), |
| 158 | (1, 2, 4, 40.00), |
| 159 | (2, 3, 1, 25.00), |
| 160 | (2, 2, 5, 50.00), |
| 161 | (3, 1, 1, 15.00); |
| 162 | |
| 163 | -- Insert ServiceInvoices |
| 164 | INSERT INTO ServiceInvoice (date, photo_copy, recipient, service_id) VALUES |
| 165 | ('2024-01-05', 'invoice1.pdf', '+38970123456', 1), |
| 166 | ('2024-01-06', 'invoice2.pdf', '+38971234567', 2), |
| 167 | ('2024-01-07', 'invoice3.pdf', '+38970123456', 3); |
| 168 | |
| 169 | -- Insert FiscalBills |
| 170 | INSERT INTO FiscalBill (datetime, issuer, description, total_price, ddv, edb, invoice_number) VALUES |
| 171 | ('2024-01-05 14:30:00', '+38970123456', 'Regular service', 150.00, 27.00, 'EDB123456', 1), |
| 172 | ('2024-01-06 15:45:00', '+38971234567', 'Full service', 200.00, 36.00, 'EDB234567', 2), |
| 173 | ('2024-01-07 16:15:00', '+38970123456', 'Oil change', 175.00, 31.50, 'EDB345678', 3); |
| 174 | }}} |