[f5f7c24] | 1 | drop table administrators;
|
---|
| 2 | drop table business_users;
|
---|
| 3 | drop table clients;
|
---|
| 4 | drop table companies;
|
---|
| 5 | drop table contracts;
|
---|
| 6 | drop table delivery_man;
|
---|
| 7 | drop table is_responsible_for;
|
---|
| 8 | drop table locations;
|
---|
| 9 | drop table models;
|
---|
| 10 | drop table payment_cards;
|
---|
| 11 | drop table payments;
|
---|
| 12 | drop table registrations;
|
---|
| 13 | drop table users;
|
---|
| 14 | drop table vehicles;
|
---|
| 15 |
|
---|
| 16 | create table users(
|
---|
| 17 | user_id serial primary key,
|
---|
| 18 | email varchar(255) unique not null,
|
---|
| 19 | username varchar(255) unique not null,
|
---|
| 20 | first_name varchar(255) not null,
|
---|
| 21 | last_name varchar(255) not null,
|
---|
| 22 | pass varchar(255) not null,
|
---|
| 23 | created_on date not null,
|
---|
| 24 | modified_on date
|
---|
| 25 | );
|
---|
| 26 |
|
---|
| 27 | create table administrators(
|
---|
| 28 | user_id integer primary key references users(user_id)
|
---|
| 29 | on delete cascade on update cascade
|
---|
| 30 | );
|
---|
| 31 |
|
---|
| 32 | create table clients(
|
---|
| 33 | user_id integer primary key references users(user_id)
|
---|
| 34 | on delete cascade on update cascade
|
---|
| 35 | );
|
---|
| 36 |
|
---|
| 37 | create table business_users(
|
---|
| 38 | user_id integer primary key references users(user_id)
|
---|
| 39 | on delete cascade on update cascade
|
---|
| 40 | );
|
---|
| 41 |
|
---|
| 42 | create table companies(
|
---|
| 43 | company_id serial primary key,
|
---|
| 44 | company_name varchar(256) not null,
|
---|
| 45 | company_email varchar(256) not null,
|
---|
| 46 | created_on date not null,
|
---|
| 47 | created_by integer not null,
|
---|
| 48 | modified_on date,
|
---|
| 49 | modified_by integer,
|
---|
| 50 | business_user_id integer references business_users(user_id)
|
---|
| 51 | on delete cascade on update cascade,
|
---|
| 52 | administrator_id integer references administrators(user_id)
|
---|
| 53 | on delete cascade on update cascade
|
---|
| 54 | );
|
---|
| 55 |
|
---|
| 56 | create table delivery_man(
|
---|
| 57 | user_id integer primary key references users(user_id)
|
---|
| 58 | on delete cascade on update cascade,
|
---|
| 59 | business_user_id integer references users(user_id)
|
---|
| 60 | on delete cascade on update cascade,
|
---|
| 61 | company_id integer references companies(company_id)
|
---|
| 62 | on delete cascade on update cascade,
|
---|
| 63 | salary decimal,
|
---|
| 64 | date_of_employment date not null
|
---|
| 65 | );
|
---|
| 66 |
|
---|
| 67 | create table models(
|
---|
| 68 | model_id serial primary key,
|
---|
| 69 | model_name varchar(100) not null,
|
---|
| 70 | color varchar(30) not null,
|
---|
| 71 | model_year date not null,
|
---|
| 72 | num_of_seats integer not null,
|
---|
| 73 | num_of_doors integer not null,
|
---|
| 74 | fuel varchar(30) not null,
|
---|
| 75 | transmission varchar(10) not null,
|
---|
| 76 | vehicle_type varchar(30) not null,
|
---|
| 77 | img_url varchar(10000)
|
---|
| 78 | );
|
---|
| 79 |
|
---|
| 80 | create table registrations(
|
---|
| 81 | registration_id serial primary key,
|
---|
| 82 | plate_num varchar(8) not null,
|
---|
| 83 | registred_on date not null,
|
---|
| 84 | valid_thru date not null,
|
---|
| 85 | is_available boolean not null
|
---|
| 86 | );
|
---|
| 87 |
|
---|
| 88 | create table locations(
|
---|
| 89 | location_id serial not null,
|
---|
| 90 | city varchar(50) not null,
|
---|
| 91 | street varchar(100) not null,
|
---|
| 92 | street_number integer not null,
|
---|
| 93 | company_id integer references companies(company_id)
|
---|
| 94 | on delete cascade on update cascade,
|
---|
| 95 | constraint pk_locations primary key (location_id,company_id)
|
---|
| 96 | );
|
---|
| 97 |
|
---|
| 98 | create table vehicles(
|
---|
| 99 | vehicle_id serial primary key,
|
---|
| 100 | chassis_number varchar(17) not null,
|
---|
| 101 | vehicle_type varchar(30) not null,
|
---|
| 102 | fuel_efficiency varchar(15) not null,
|
---|
| 103 | brand varchar(50) not null,
|
---|
| 104 | daily_rental_price decimal not null,
|
---|
| 105 | company_id integer,
|
---|
| 106 | model_id integer references models(model_id)
|
---|
| 107 | on delete cascade on update cascade,
|
---|
| 108 | location_id integer,
|
---|
| 109 | registration_id integer references registrations(registration_id)
|
---|
| 110 | on delete cascade on update cascade,
|
---|
| 111 | constraint fk_location foreign key (location_id,company_id) references locations(location_id,company_id)
|
---|
| 112 | on delete cascade on update cascade
|
---|
| 113 | );
|
---|
| 114 |
|
---|
| 115 | create table payment_cards(
|
---|
| 116 | card_id serial primary key,
|
---|
| 117 | card_number varchar(16) not null,
|
---|
| 118 | cvc numeric check(cvc>=100 and cvc<=999) not null,
|
---|
| 119 | valid_thru date not null,
|
---|
| 120 | holder_name varchar(100) not null,
|
---|
| 121 | user_id integer references clients(user_id)
|
---|
| 122 | on delete cascade on update cascade
|
---|
| 123 | );
|
---|
| 124 |
|
---|
| 125 | create table payments(
|
---|
| 126 | payment_id serial not null unique,
|
---|
| 127 | payment_date date not null,
|
---|
| 128 | payment_time time not null,
|
---|
| 129 | amount decimal not null,
|
---|
| 130 | card_id integer references payment_cards(card_id)
|
---|
| 131 | on delete cascade on update cascade,
|
---|
| 132 | constraint pk_payment primary key (payment_id, card_id)
|
---|
| 133 | );
|
---|
| 134 |
|
---|
| 135 | create table contracts (
|
---|
| 136 | signed_date date not null,
|
---|
| 137 | user_id integer references clients(user_id)
|
---|
| 138 | on delete cascade on update cascade,
|
---|
| 139 | vehicle_id integer references vehicles(vehicle_id)
|
---|
| 140 | on delete cascade on update cascade,
|
---|
| 141 | delivery_man_id integer references delivery_man(user_id)
|
---|
| 142 | on delete cascade on update cascade,
|
---|
| 143 | payment_id integer,
|
---|
| 144 | card_id integer,
|
---|
| 145 | address varchar(50) not null,
|
---|
| 146 | start_date date not null,
|
---|
| 147 | end_date date not null,
|
---|
| 148 | review varchar(500),
|
---|
| 149 | has_navigation boolean,
|
---|
| 150 | has_baby_seat boolean,
|
---|
| 151 | has_roofrack boolean,
|
---|
| 152 | has_green_card boolean,
|
---|
| 153 | constraint pk_contract primary key (signed_date,user_id,vehicle_id,delivery_man_id),
|
---|
| 154 | constraint fk_contract_payment foreign key (payment_id,card_id) references payments(payment_id,card_id)
|
---|
| 155 | on delete cascade on update cascade
|
---|
| 156 | );
|
---|
| 157 |
|
---|
| 158 | create table is_responsible_for(
|
---|
| 159 | user_id integer references delivery_man(user_id)
|
---|
| 160 | on delete cascade on update cascade,
|
---|
| 161 | vehicle_id integer references vehicles(vehicle_id)
|
---|
| 162 | on delete cascade on update cascade,
|
---|
| 163 | constraint pk_is_responsible_for primary key (user_id,vehicle_id)
|
---|
| 164 | );
|
---|