LogicAndPhysicalDeisgn: kreiranjeFinal2.sql

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