-
Notifications
You must be signed in to change notification settings - Fork 1
/
initDB.sql
104 lines (93 loc) · 3.77 KB
/
initDB.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
CREATE DATABASE IF NOT EXISTS centralized_mess;
USE centralized_mess;
/* Create a DB user (Username/Password for demonstration only) */
CREATE USER IF NOT EXISTS 'soppos'@'localhost' IDENTIFIED BY 'possop';
GRANT ALL ON centralized_mess.* TO 'soppos'@'localhost';
/* Create all tables */
CREATE TABLE IF NOT EXISTS students (
roll_number VARCHAR(10) NOT NULL UNIQUE,
name VARCHAR(50) NOT NULL,
phone_number VARCHAR(15) NOT NULL UNIQUE,
address VARCHAR(100) NOT NULL,
email VARCHAR(30) NOT NULL UNIQUE,
password VARCHAR(200),
PRIMARY KEY (roll_number)
);
CREATE TABLE IF NOT EXISTS messes (
mess_id INT NOT NULL AUTO_INCREMENT UNIQUE,
name VARCHAR(20) NOT NULL UNIQUE,
PRIMARY KEY (mess_id)
);
CREATE TABLE IF NOT EXISTS managers (
pf_number VARCHAR(10) NOT NULL UNIQUE,
name VARCHAR(50) NOT NULL,
phone_number VARCHAR(15) NOT NULL UNIQUE,
mess_id INT NOT NULL,
email VARCHAR(30) NOT NULL UNIQUE,
password VARCHAR(200),
PRIMARY KEY (pf_number),
FOREIGN KEY (mess_id) REFERENCES messes(mess_id)
);
CREATE TABLE IF NOT EXISTS menus (
menu_id INT NOT NULL AUTO_INCREMENT UNIQUE,
menu_name VARCHAR(50) NOT NULL,
mess_id INT NOT NULL,
menu_time ENUM('Breakfast', 'Lunch', 'Dinner') NOT NULL,
contents VARCHAR(500) NOT NULL,
deleted BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (mess_id, menu_id),
FOREIGN KEY (mess_id) REFERENCES messes(mess_id)
);
CREATE TABLE IF NOT EXISTS extra_items (
item_id INT NOT NULL AUTO_INCREMENT UNIQUE,
item_name VARCHAR(50) NOT NULL,
mess_id INT NOT NULL,
cost_per_item REAL NOT NULL,
deleted BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (mess_id, item_id),
FOREIGN KEY (mess_id) REFERENCES messes(mess_id)
);
CREATE TABLE IF NOT EXISTS extras_in_menu (
menu_id INT NOT NULL,
item_id INT NOT NULL,
PRIMARY KEY (menu_id, item_id),
FOREIGN KEY (menu_id) REFERENCES menus(menu_id),
FOREIGN KEY (item_id) REFERENCES extra_items(item_id)
);
CREATE TABLE IF NOT EXISTS meals (
meal_date DATE NOT NULL,
mess_id INT NOT NULL,
menu_id INT NOT NULL,
meal_time ENUM('Breakfast', 'Lunch', 'Dinner') NOT NULL,
total_cost REAL,
PRIMARY KEY (mess_id, meal_date, meal_time),
FOREIGN KEY (mess_id) REFERENCES messes(mess_id),
FOREIGN KEY (menu_id) REFERENCES menus(menu_id)
);
CREATE TABLE IF NOT EXISTS basic_menu_bills (
time_id BIGINT NOT NULL UNIQUE,
roll_number VARCHAR(10) NOT NULL,
mess_id INT NOT NULL,
meal_date DATE NOT NULL,
meal_time ENUM('Breakfast', 'Lunch', 'Dinner') NOT NULL,
cost REAL,
PRIMARY KEY (time_id),
UNIQUE (roll_number, mess_id, meal_date, meal_time),
FOREIGN KEY (roll_number) REFERENCES students(roll_number),
FOREIGN KEY (mess_id) REFERENCES messes(mess_id)
);
CREATE TABLE IF NOT EXISTS extra_items_bills (
time_id BIGINT NOT NULL UNIQUE,
roll_number VARCHAR(10) NOT NULL,
mess_id INT NOT NULL,
meal_date DATE NOT NULL,
meal_time ENUM('Breakfast', 'Lunch', 'Dinner') NOT NULL,
item_id INT NOT NULL,
quantity INT NOT NULL,
claimed BOOLEAN NOT NULL DEFAULT false,
cost REAL,
PRIMARY KEY (time_id),
FOREIGN KEY (roll_number) REFERENCES students(roll_number),
FOREIGN KEY (mess_id) REFERENCES messes(mess_id),
FOREIGN KEY (item_id) REFERENCES extra_items(item_id)
);