create_tables.sql 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. CREATE TABLE public.Truck (
  2. Tru_id VARCHAR NOT NULL,
  3. Tru_available BOOLEAN DEFAULT True NOT NULL,
  4. CONSTRAINT truck_pk PRIMARY KEY (Tru_id)
  5. );
  6. COMMENT ON COLUMN public.Truck.Tru_id IS 'Unique identifier (for instance : AC-543-AG).';
  7. COMMENT ON COLUMN public.Truck.Tru_available IS 'True if can make deliveries. False if in maintenance (temporally unavailable) or written off (definitely unavailable, for example car accident, end of life). Warning ! This attribute does NOT track if a truck is unavailble because it is on the road !';
  8. CREATE SEQUENCE public.driver_dri_id_seq_1;
  9. CREATE TABLE public.Driver (
  10. Dri_id INTEGER NOT NULL DEFAULT nextval('public.driver_dri_id_seq_1'),
  11. Dri_firstName VARCHAR,
  12. Dri_lastName VARCHAR NOT NULL,
  13. Dri_available BOOLEAN DEFAULT True NOT NULL,
  14. Tru_id VARCHAR NOT NULL,
  15. CONSTRAINT driver_pk PRIMARY KEY (Dri_id)
  16. );
  17. COMMENT ON TABLE public.Driver IS 'A driver who is working in our society or has worked in our society by the past';
  18. COMMENT ON COLUMN public.Driver.Dri_available IS 'True if the driver is availbale and can make deliveries. False if the driver is fired, has resigned, is sick, etc.';
  19. COMMENT ON COLUMN public.Driver.Tru_id IS 'Unique identifier (for instance : AC-543-AG).';
  20. ALTER SEQUENCE public.driver_dri_id_seq_1 OWNED BY public.Driver.Dri_id;
  21. CREATE SEQUENCE public.postaladdress_add_id_seq_2;
  22. CREATE TABLE public.PostalAddress (
  23. Add_id INTEGER NOT NULL DEFAULT nextval('public.postaladdress_add_id_seq_2'),
  24. Add_number VARCHAR,
  25. Add_name VARCHAR NOT NULL,
  26. Add_city VARCHAR NOT NULL,
  27. CONSTRAINT postaladdress_pk PRIMARY KEY (Add_id)
  28. );
  29. COMMENT ON COLUMN public.PostalAddress.Add_number IS 'Number in the street (NULL if irrelevant).';
  30. COMMENT ON COLUMN public.PostalAddress.Add_name IS 'Name of the street if relevant or name of the place';
  31. COMMENT ON COLUMN public.PostalAddress.Add_city IS 'City';
  32. ALTER SEQUENCE public.postaladdress_add_id_seq_2 OWNED BY public.PostalAddress.Add_id;
  33. CREATE SEQUENCE public.loadingpoint_lpt_id_seq_1;
  34. CREATE TABLE public.LoadingPoint (
  35. Lpt_id INTEGER NOT NULL DEFAULT nextval('public.loadingpoint_lpt_id_seq_1'),
  36. Add_id INTEGER NOT NULL,
  37. CONSTRAINT loadingpoint_pk PRIMARY KEY (Lpt_id)
  38. );
  39. COMMENT ON TABLE public.LoadingPoint IS 'A place were trucks can be loaded.';
  40. ALTER SEQUENCE public.loadingpoint_lpt_id_seq_1 OWNED BY public.LoadingPoint.Lpt_id;
  41. CREATE SEQUENCE public.mission_mis_id_seq;
  42. CREATE TABLE public.Mission (
  43. Mis_id INTEGER NOT NULL DEFAULT nextval('public.mission_mis_id_seq'),
  44. Lpt_id INTEGER NOT NULL,
  45. Tru_id VARCHAR NOT NULL,
  46. Mis_loadingDate TIMESTAMP NOT NULL,
  47. Dri_id INTEGER NOT NULL,
  48. CONSTRAINT mission_pk PRIMARY KEY (Mis_id)
  49. );
  50. COMMENT ON TABLE public.Mission IS 'A sequence of targets for a driver, from a loading point ans multiple delivery points';
  51. COMMENT ON COLUMN public.Mission.Tru_id IS 'Unique identifier (for instance : AC-543-AG).';
  52. COMMENT ON COLUMN public.Mission.Mis_loadingDate IS 'The time of truck loading. The driver should be present at that time to the mission associated loading point.';
  53. ALTER SEQUENCE public.mission_mis_id_seq OWNED BY public.Mission.Mis_id;
  54. CREATE SEQUENCE public.product_pro_id_seq;
  55. CREATE TABLE public.Product (
  56. Pro_id INTEGER NOT NULL DEFAULT nextval('public.product_pro_id_seq'),
  57. Pro_name VARCHAR NOT NULL,
  58. CONSTRAINT product_pk PRIMARY KEY (Pro_id)
  59. );
  60. COMMENT ON TABLE public.Product IS 'List of products than can be sold or were sold by the past';
  61. ALTER SEQUENCE public.product_pro_id_seq OWNED BY public.Product.Pro_id;
  62. CREATE SEQUENCE public.company_com_id_seq;
  63. CREATE TABLE public.Company (
  64. Com_id INTEGER NOT NULL DEFAULT nextval('public.company_com_id_seq'),
  65. Com_name VARCHAR NOT NULL,
  66. Add_id INTEGER NOT NULL,
  67. CONSTRAINT com_pk PRIMARY KEY (Com_id)
  68. );
  69. COMMENT ON TABLE public.Company IS 'Every company that can take orders or have taken orders by the past.';
  70. COMMENT ON COLUMN public.Company.Com_name IS 'The public name of the company';
  71. COMMENT ON COLUMN public.Company.Add_id IS 'The address of the company office';
  72. ALTER SEQUENCE public.company_com_id_seq OWNED BY public.Company.Com_id;
  73. CREATE SEQUENCE public.warehouse_war_id_seq_1;
  74. CREATE TABLE public.Warehouse (
  75. War_id INTEGER NOT NULL DEFAULT nextval('public.warehouse_war_id_seq_1'),
  76. Com_id INTEGER NOT NULL,
  77. Add_id INTEGER NOT NULL,
  78. CONSTRAINT warehouse_pk PRIMARY KEY (War_id)
  79. );
  80. COMMENT ON TABLE public.Warehouse IS 'A place for a customer where products can be delivered';
  81. ALTER SEQUENCE public.warehouse_war_id_seq_1 OWNED BY public.Warehouse.War_id;
  82. CREATE SEQUENCE public.bookorder_ord_id_seq;
  83. CREATE TABLE public.BookOrder (
  84. Ord_id INTEGER NOT NULL DEFAULT nextval('public.bookorder_ord_id_seq'),
  85. Ord_date TIMESTAMP NOT NULL,
  86. Com_id INTEGER NOT NULL,
  87. CONSTRAINT bookorder_pk PRIMARY KEY (Ord_id)
  88. );
  89. COMMENT ON TABLE public.BookOrder IS 'List or ordered products';
  90. COMMENT ON COLUMN public.BookOrder.Ord_date IS 'The date of order creation';
  91. ALTER SEQUENCE public.bookorder_ord_id_seq OWNED BY public.BookOrder.Ord_id;
  92. CREATE SEQUENCE public.ordereditem_ori_id_seq;
  93. CREATE TABLE public.OrderedItem (
  94. Ori_id INTEGER NOT NULL DEFAULT nextval('public.ordereditem_ori_id_seq'),
  95. Ori_quantity INTEGER NOT NULL,
  96. Ori_deliveryDueDate TIMESTAMP NOT NULL,
  97. Pro_id INTEGER NOT NULL,
  98. Ord_id INTEGER NOT NULL,
  99. War_id INTEGER NOT NULL,
  100. CONSTRAINT ordereditem_pk PRIMARY KEY (Ori_id)
  101. );
  102. COMMENT ON TABLE public.OrderedItem IS 'A line in an order, associated to a product. Should be delivered in the future.';
  103. COMMENT ON COLUMN public.OrderedItem.Ori_quantity IS 'In kilogrammes';
  104. COMMENT ON COLUMN public.OrderedItem.Ori_deliveryDueDate IS 'The initially planned delivery date';
  105. ALTER SEQUENCE public.ordereditem_ori_id_seq OWNED BY public.OrderedItem.Ori_id;
  106. CREATE TABLE public.OrderedItemMission (
  107. Ori_id INTEGER NOT NULL,
  108. Mis_id INTEGER NOT NULL,
  109. CONSTRAINT ordereditemmission_pk PRIMARY KEY (Ori_id, Mis_id)
  110. );
  111. COMMENT ON TABLE public.OrderedItemMission IS 'Association table, an order item can be added in mission several times if the product was not found (ie : was missing) in the previous delivery.';
  112. CREATE SEQUENCE public.slip_sli_id_seq;
  113. CREATE TABLE public.Slip (
  114. Sli_id INTEGER NOT NULL DEFAULT nextval('public.slip_sli_id_seq'),
  115. Sli_deliveryDate TIMESTAMP NOT NULL,
  116. Sli_signed BOOLEAN NOT NULL,
  117. Ori_id INTEGER NOT NULL,
  118. CONSTRAINT slip_pk PRIMARY KEY (Sli_id)
  119. );
  120. COMMENT ON TABLE public.Slip IS 'Created when an order item is delivered. Should be signed.';
  121. COMMENT ON COLUMN public.Slip.Sli_deliveryDate IS 'Effective delivery date';
  122. COMMENT ON COLUMN public.Slip.Sli_signed IS 'True if the physical paper slip has been signed.';
  123. ALTER SEQUENCE public.slip_sli_id_seq OWNED BY public.Slip.Sli_id;
  124. ALTER TABLE public.Mission ADD CONSTRAINT truck_mission_fk
  125. FOREIGN KEY (Tru_id)
  126. REFERENCES public.Truck (Tru_id)
  127. ON DELETE NO ACTION
  128. ON UPDATE NO ACTION
  129. NOT DEFERRABLE;
  130. ALTER TABLE public.Driver ADD CONSTRAINT truck_driver_fk
  131. FOREIGN KEY (Tru_id)
  132. REFERENCES public.Truck (Tru_id)
  133. ON DELETE NO ACTION
  134. ON UPDATE NO ACTION
  135. NOT DEFERRABLE;
  136. ALTER TABLE public.Mission ADD CONSTRAINT driver_mission_fk
  137. FOREIGN KEY (Dri_id)
  138. REFERENCES public.Driver (Dri_id)
  139. ON DELETE NO ACTION
  140. ON UPDATE NO ACTION
  141. NOT DEFERRABLE;
  142. ALTER TABLE public.Company ADD CONSTRAINT postaladdress_company_fk
  143. FOREIGN KEY (Add_id)
  144. REFERENCES public.PostalAddress (Add_id)
  145. ON DELETE NO ACTION
  146. ON UPDATE NO ACTION
  147. NOT DEFERRABLE;
  148. ALTER TABLE public.Warehouse ADD CONSTRAINT postaladdress_warehouse_fk
  149. FOREIGN KEY (Add_id)
  150. REFERENCES public.PostalAddress (Add_id)
  151. ON DELETE NO ACTION
  152. ON UPDATE NO ACTION
  153. NOT DEFERRABLE;
  154. ALTER TABLE public.LoadingPoint ADD CONSTRAINT postaladdress_loadingpoint_fk
  155. FOREIGN KEY (Add_id)
  156. REFERENCES public.PostalAddress (Add_id)
  157. ON DELETE NO ACTION
  158. ON UPDATE NO ACTION
  159. NOT DEFERRABLE;
  160. ALTER TABLE public.Mission ADD CONSTRAINT loadingpoint_mission_fk
  161. FOREIGN KEY (Lpt_id)
  162. REFERENCES public.LoadingPoint (Lpt_id)
  163. ON DELETE NO ACTION
  164. ON UPDATE NO ACTION
  165. NOT DEFERRABLE;
  166. ALTER TABLE public.OrderedItemMission ADD CONSTRAINT mission_ordereditemmission_fk
  167. FOREIGN KEY (Mis_id)
  168. REFERENCES public.Mission (Mis_id)
  169. ON DELETE NO ACTION
  170. ON UPDATE NO ACTION
  171. NOT DEFERRABLE;
  172. ALTER TABLE public.OrderedItem ADD CONSTRAINT product_ordereditem_fk
  173. FOREIGN KEY (Pro_id)
  174. REFERENCES public.Product (Pro_id)
  175. ON DELETE NO ACTION
  176. ON UPDATE NO ACTION
  177. NOT DEFERRABLE;
  178. ALTER TABLE public.BookOrder ADD CONSTRAINT company_bookorder_fk
  179. FOREIGN KEY (Com_id)
  180. REFERENCES public.Company (Com_id)
  181. ON DELETE NO ACTION
  182. ON UPDATE NO ACTION
  183. NOT DEFERRABLE;
  184. ALTER TABLE public.Warehouse ADD CONSTRAINT company_warehouse_fk
  185. FOREIGN KEY (Com_id)
  186. REFERENCES public.Company (Com_id)
  187. ON DELETE NO ACTION
  188. ON UPDATE NO ACTION
  189. NOT DEFERRABLE;
  190. ALTER TABLE public.OrderedItem ADD CONSTRAINT warehouse_ordereditem_fk
  191. FOREIGN KEY (War_id)
  192. REFERENCES public.Warehouse (War_id)
  193. ON DELETE NO ACTION
  194. ON UPDATE NO ACTION
  195. NOT DEFERRABLE;
  196. ALTER TABLE public.OrderedItem ADD CONSTRAINT bookorder_ordereditem_fk
  197. FOREIGN KEY (Ord_id)
  198. REFERENCES public.BookOrder (Ord_id)
  199. ON DELETE NO ACTION
  200. ON UPDATE NO ACTION
  201. NOT DEFERRABLE;
  202. ALTER TABLE public.Slip ADD CONSTRAINT ordereditem_slip_fk
  203. FOREIGN KEY (Ori_id)
  204. REFERENCES public.OrderedItem (Ori_id)
  205. ON DELETE NO ACTION
  206. ON UPDATE NO ACTION
  207. NOT DEFERRABLE;
  208. ALTER TABLE public.OrderedItemMission ADD CONSTRAINT ordereditem_ordereditemmission_fk
  209. FOREIGN KEY (Ori_id)
  210. REFERENCES public.OrderedItem (Ori_id)
  211. ON DELETE NO ACTION
  212. ON UPDATE NO ACTION
  213. NOT DEFERRABLE;