RelationalModel: V2_Init_DB.sql

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