RelationalModel: V1_Init_DB.sql

File V1_Init_DB.sql, 5.0 KB (added by 153093, 5 weeks ago)
Line 
1drop table if exists public.Wine_Type cascade;
2drop table if exists public.Wine cascade;
3drop table if exists public.Address cascade;
4drop table if exists public.Customer_Type cascade;
5drop table if exists public.Customer cascade;
6drop table if exists public.Vehicle_Type cascade;
7drop table if exists public.Vehicle cascade;
8drop table if exists public.Payment cascade;
9drop table if exists public.Expense_Type cascade;
10drop table if exists public.Warehouse cascade;
11drop table if exists public.Employee cascade;
12drop table if exists public.Employee_Drives_Vehicle cascade;
13drop table if exists public.Shipment cascade;
14drop table if exists public.Shipment_Has_Expense_Of_Expense_Type cascade;
15drop table if exists public.Shipment_Load cascade;
16
17
18create table public.Wine_Type(
19 Wine_Type_Id serial primary key,
20 Wine_Type_Name varchar(50) unique not null,
21 Wine_Type_Description varchar(200) not null,
22 Wine_Type_Region varchar(50) not null
23);
24
25create table public.Wine(
26 Wine_Id serial primary key,
27 Wine_Name varchar(50) unique not null,
28 Base_Price double precision not null,
29 Year_Produced date not null,
30 Wine_Type_Id integer not null,
31 foreign key (Wine_Type_Id) references public.Wine_Type (Wine_Type_Id)
32);
33
34create table public.Customer_Type(
35 Customer_Type_Id serial primary key,
36 Customer_Type_Name varchar(50) unique not null,
37 Customer_Type_Description varchar(200) not null
38);
39
40create table public.Address(
41 Address_Id serial primary key,
42 Street varchar(70) unique not null,
43 City varchar(50) not null,
44 Postcode varchar(20) not null
45);
46
47create table public.Customer(
48 Customer_Id serial primary key,
49 Customer_Name varchar(50) unique not null,
50 Customer_Email varchar(70) not null,
51 Customer_Phone_Number varchar(100) not null,
52 Customer_Type_Id integer not null,
53 Address_Id integer not null,
54 foreign key (Customer_Type_Id) references public.Customer_Type (Customer_Type_Id),
55 foreign key (Address_Id) references public.Address (Address_Id)
56);
57
58create table public.Vehicle_Type(
59 Vehicle_Type_Id serial primary key,
60 Vehicle_Type_Name varchar(50) unique not null
61);
62
63create table public.Vehicle(
64 Vehicle_Id serial primary key,
65 Vehicle_Type_Id integer not null,
66 Make varchar(50) not null,
67 Model varchar(50) not null,
68 Registration varchar(50) not null,
69 Capacity integer not null,
70 foreign key (Vehicle_Type_Id) references public.Vehicle_Type (Vehicle_Type_Id)
71);
72
73create table public.Payment(
74 Payment_Id serial primary key,
75 Payment_Status integer not null
76);
77
78create table public.Expense_Type(
79 Expense_Type_Id serial primary key,
80 Expense_Type_Name varchar(50) unique not null,
81 Expense_Type_Description varchar(200) not null
82);
83
84create table public.Warehouse(
85 Warehouse_Id serial primary key,
86 Warehouse_Name varchar(50) unique not null,
87 Address_Id integer not null,
88 foreign key (Address_Id) references public.Address (Address_Id)
89);
90
91create table public.Employee(
92 Employee_Id serial primary key,
93 Employee_Name varchar(50) not null,
94 Employee_Surname varchar(50) not null,
95 Warehouse_Id integer not null,
96 foreign key (Warehouse_Id) references public.Warehouse (Warehouse_Id)
97);
98
99create table public.Employee_Drives_Vehicle(
100 Employee_Id integer not null,
101 Vehicle_Id integer not null,
102 primary key (Employee_Id, Vehicle_Id), -- Composite primary key
103 foreign key (Vehicle_Id) references public.Vehicle (Vehicle_Id),
104 foreign key (Employee_Id) references public.Employee (Employee_Id)
105);
106
107create table public.Shipment(
108 Shipment_Id serial primary key,
109 Employee_Id integer not null,
110 Payment_Id integer not null,
111 Vehicle_Id integer not null,
112 Shipment_Date date not null,
113 foreign key (Employee_Id) references public.Employee (Employee_Id),
114 foreign key (Payment_Id) references public.Payment (Payment_Id),
115 foreign key (Vehicle_Id) references public.Vehicle (Vehicle_Id)
116);
117
118create table public.Shipment_Has_Expense_Of_Expense_Type(
119 Expense_Type_Id integer not null,
120 Shipment_Id integer not null,
121 Amount integer not null,
122 primary key (Expense_Type_Id, Shipment_Id), -- Composite primary key
123 foreign key (Expense_Type_Id) references public.Expense_Type (Expense_Type_Id),
124 foreign key (Shipment_Id) references public.Shipment (Shipment_Id)
125);
126
127create table public.Shipment_Load(
128 Shipment_Id integer not null,
129 Customer_Id integer not null,
130 Wine_Id integer not null,
131 Wine_Base_Price double precision not null,
132 Wine_Agreed_Price double precision not null,
133 Wine_Quantity integer not null,
134 primary key (Shipment_Id, Customer_Id, Wine_Id), -- Composite primary key
135 foreign key (Shipment_Id) references public.Shipment (Shipment_Id),
136 foreign key (Customer_Id) references public.Customer (Customer_Id),
137 foreign key (Wine_Id) references public.Wine (Wine_Id)
138);