DDL: kreiranje.sql

File kreiranje.sql, 3.1 KB (added by 215010, 5 weeks ago)
Line 
1drop table if exists Service;
2drop table if exists Payment;
3drop table if exists Reservation;
4drop table if exists Room;
5drop table if exists Staff;
6drop table if exists Customer;
7drop table if exists Hotel_Building;
8drop table if exists Manager;
9drop table if exists Hotel_User;
10
11
12create table Hotel_User(
13 user_id bigint primary key,
14 first_name varchar(30) not null,
15 last_name varchar(30) not null,
16 phone varchar(9) not NULL unique,
17 email varchar(100) not null unique,
18 password varchar(100) not null
19);
20
21
22create table Customer(
23 user_id bigint primary key,
24 constraint customer_fk foreign key (user_id) references Hotel_User(user_id) on delete cascade
25);
26
27
28create table Manager(
29 user_id bigint primary key,
30 constraint manager_fk foreign key (user_id) references Hotel_User(user_id) on delete cascade
31);
32
33create table Hotel_Building(
34 building_id varchar(10) primary key,
35 address varchar(50) not null,
36 city varchar(20) not null,
37 floor integer not null,
38 manager_id bigint not null,
39 num_rooms integer not NULL,
40-- staff_id bigint,
41 constraint building_fk_manager foreign key(manager_id) references Manager(user_id)
42);
43
44create table Staff(
45 user_id bigint primary key,
46 building_id varchar(10) not null,
47 constraint staff_fk foreign key (user_id) references Hotel_User(user_id) on delete cascade,
48 CONSTRAINT hotel_building_fk FOREIGN KEY (building_id) REFERENCES Hotel_Building(building_id)
49);
50
51
52
53create table Room(
54 room_number integer,
55 room_type varchar(20) not null,
56 number_of_beds integer not null,
57 price_per_night bigint not null,
58 available boolean not null,
59 building_id varchar(10),
60 constraint room_pkey primary key (room_number, building_id),
61 constraint room_fk foreign key (building_id) references Hotel_Building(building_id)
62);
63
64create table Reservation(
65 reservation_id bigint primary key,
66 start_date date not null,
67 end_date date,
68 status varchar(40) not null,
69 reserv_date date not null,
70 room_number integer,
71 building_id varchar(10),
72 customer_id bigint,
73 manager_id bigint,
74 constraint reservation_fk_room foreign key(room_number, building_id) references Room(room_number, building_id),
75 constraint reservation_fk_customer foreign key(customer_id) references Customer(user_id),
76 constraint reservation_fk_manager foreign key(manager_id) references Manager(user_id)
77);
78
79create table Payment(
80 p_id bigint primary key,
81 p_method varchar(5) not null,
82 amount integer not null,
83 p_date date not null,
84 customer_id bigint,
85 reservation_id bigint unique,
86 constraint payment_fk_reservation foreign key (reservation_id) references Reservation(reservation_id),
87 constraint payment_fk foreign key (customer_id) references Customer(user_id)
88);
89
90
91create table Service(
92 service_id bigint primary key,
93 service_type varchar(40) not null,
94 service_date date,
95 service_status varchar(70) not null,
96 room_number integer,
97 building_id varchar(10),
98 staff_id bigint,
99 CONSTRAINT service_fk_room FOREIGN KEY (room_number, building_id) REFERENCES Room(room_number, building_id),
100 CONSTRAINT service_fk_staff FOREIGN KEY (staff_id) REFERENCES Staff(user_id)
101);