LogicAndPhysicalDeisgn: kreiranjeFinal.sql

File kreiranjeFinal.sql, 3.7 KB (added by 201136, 9 months ago)
Line 
1set search_path = project;
2
3
4drop table if exists UserHasProgram;
5drop table if exists UserHasDays;
6drop table if exists DayHasMeal;
7drop table if exists MealHasIngredient;
8drop table if exists DayHasWorkout;
9drop table if exists WorkoutHasExercise;
10drop table if exists WorkoutProgramHasWorkout;
11
12
13drop table if exists Meal;
14drop table if exists Workout;
15drop table if exists _Day cascade;
16drop table if exists _User cascade;
17drop table if exists PersonalizedIngredient;
18drop table if exists Ingredient;
19drop table if exists PersonalizedExercise;
20drop table if exists Exercise;
21drop table if exists WorkoutProgram;
22
23
24
25drop schema if exists project cascade;
26
27create schema project;
28
29set search_path = project;
30
31
32
33create table _User (
34 uID bigint primary key,
35 name VARCHAR(255) not null unique,
36 password VARCHAR(255) not null,
37 age integer not null,
38 height integer,
39 weight integer,
40 role VARCHAR(255)
41);
42
43create table _Day (
44 dID bigint primary key,
45 date date not null
46);
47
48create table Workout (
49 wID bigint primary key,
50 duration integer,
51 name VARCHAR(255),
52 uID BIGINT,
53 foreign key (uID) references _User(uID)
54);
55
56create table Meal (
57 mID bigint primary key,
58 name VARCHAR(255) not null,
59 type VARCHAR(255) not null,
60 uID BIGINT,
61 foreign key (uID) references _User(uID)
62);
63
64create table Ingredient (
65 iID bigint primary key,
66 name VARCHAR(255) not null,
67 calories integer not null,
68 protein integer not null,
69 carbs integer not null,
70 fats integer not null
71);
72
73create table PersonalizedIngredient (
74 piID bigint primary key,
75 quantity integer not null,
76 iID bigint,
77 foreign key (iID) references Ingredient(iID)
78);
79
80create table Exercise (
81 eID bigint primary key,
82 name VARCHAR(255) not null,
83 type VARCHAR(255) not null
84);
85create table PersonalizedExercise (
86 peID bigint primary key,
87 reps integer,
88 sets integer,
89 weight integer,
90 time integer,
91 eID bigint,
92 foreign key (eID) references Exercise(eID)
93);
94
95create table WorkoutProgram (
96 wpID bigint primary key,
97 name VARCHAR(255) not null
98);
99
100
101
102create table UserHasDays(
103 uID bigint references _User(uID),
104 dID bigint references _Day(dID),
105
106 constraint UserHasDays_pkey primary key (dID, uID)
107);
108
109create table UserHasProgram(
110 uID bigint references _User(uID),
111 wpID bigint references WorkoutProgram(wpID),
112
113 constraint UserHasProgram_pkey primary key (wpID, uID)
114);
115
116create table DayHasWorkout(
117 wID bigint references Workout(wID),
118 dID bigint references _Day(dID),
119
120 constraint DayHasWorkout_pkey primary key (wID, dID)
121);
122
123create table WorkoutProgramHasWorkout(
124 wID bigint references Workout(wID),
125 wpID bigint references WorkoutProgram(wpID),
126
127 constraint WorkoutProgramHasWorkout_pkey primary key (wpID, wID)
128);
129
130create table DayHasMeal(
131 mID bigint references Meal(mID),
132 dID bigint references _Day(dID),
133
134 constraint DayHasMeal_pkey primary key (dID, mID)
135);
136
137
138create table MealHasIngredient(
139 mID bigint references Meal(mID),
140 piID bigint references PersonalizedIngredient(piID),
141
142 constraint MealHasIngredient_pkey primary key (piID, mID)
143);
144
145create table WorkoutHasExercise(
146 peID bigint references PersonalizedExercise(peID),
147 wID bigint references Workout(wID),
148
149 constraint WorkoutHasExercise_pkey primary key (peID, wID)
150);
151
152CREATE SEQUENCE _user_seq START 1;
153CREATE SEQUENCE _day_seq START 1;
154CREATE SEQUENCE _workout_seq START 1;
155CREATE SEQUENCE _meal_seq START 1;
156CREATE SEQUENCE _ingredient_seq START 1;
157CREATE SEQUENCE _personalized_ingredient_seq START 1;
158CREATE SEQUENCE _exercise_seq START 1;
159CREATE SEQUENCE _personalized_exercise_seq START 1;
160CREATE SEQUENCE _workout_program_seq START 1;
161
162