-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sql
211 lines (134 loc) · 5.52 KB
/
database.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
-- USER is a reserved keyword with Postgres
-- You must use double quotes in every query that user is in:
-- ex. SELECT * FROM "user";
-- Otherwise you will have errors!
-- new table --
CREATE TABLE "user" (
"id" serial NOT NULL,
"username" varchar(100) NOT NULL UNIQUE,
"password" varchar(100) NOT NULL,
"access_level" int NOT NULL DEFAULT '0',
CONSTRAINT "user_pk" PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
CREATE TABLE "plant" (
"id" serial NOT NULL,
"user_id" int NOT NULL,
"nickname" varchar(100) NOT NULL,
"avatar_url" varchar(255) NOT NULL,
"date_added" DATE NOT NULL,
"plant_type" varchar(100) NOT NULL,
"light_level" int NOT NULL,
"water_freq" int NOT NULL,
"date_watered" DATE NOT NULL,
"date_potted" DATE NOT NULL,
"date_fertilized" DATE NOT NULL,
"notes" varchar(255) DEFAULT 'null',
CONSTRAINT "plant_pk" PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
CREATE TABLE "photo" (
"id" serial NOT NULL,
"user_id" int NOT NULL,
"plant_id" int NOT NULL,
"photo_url" varchar(255) NOT NULL,
"date_uploaded" DATE NOT NULL,
CONSTRAINT "photo_pk" PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
CREATE TABLE "comment" (
"id" serial NOT NULL,
"user_id" int NOT NULL,
"comment" varchar(255) NOT NULL,
"type" varchar(100) NOT NULL,
CONSTRAINT "comment_pk" PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
ALTER TABLE "plant" ADD CONSTRAINT "plant_fk0" FOREIGN KEY ("user_id") REFERENCES "user"("id");
ALTER TABLE "photo" ADD CONSTRAINT "photo_fk0" FOREIGN KEY ("user_id") REFERENCES "user"("id");
ALTER TABLE "photo" ADD CONSTRAINT "photo_fk1" FOREIGN KEY ("plant_id") REFERENCES "plant"("id");
ALTER TABLE "comment" ADD CONSTRAINT "comment_fk0" FOREIGN KEY ("user_id") REFERENCES "user"("id");
-- new table end --
-- insert plant data --
INSERT INTO "plant"
("user_id", "nickname", "avatar_url", "date_added", "plant_type", "light_level", "water_freq", "date_watered", "date_potted", "date_fertilized", "notes")
VALUES
('1', 'Figgy', 'https://bit.ly/3owgpgV', '2019-10-11', 'Fiddle Leaf Fig', '3', '9', '2021-11-08', '2019-10-11', '2020-03-09', 'Turn on first water of the month' ),
('1', 'Devil', 'https://bit.ly/3qD0Bvp', '2020-08-19', 'Golden Pothos', '2', '7', '2021-11-15', '2020-08-19', '2020-08-19', null),
('1', 'Dummy', 'https://bit.ly/3ccBV4A', '2021-05-22', 'Dumbcane', '2', '6', '2021-11-15', '2021-05-22', '2021-05-22', null ),
('1', 'Joey', 'https://bit.ly/3Hpi2pl', '2020-08-19', 'Golden Pothos', '2', '7', '2021-11-15', '2020-08-19', '2020-08-19', null),
('2', 'Tony', 'https://bit.ly/3CiWKWO', '2019-12-09', 'Golden Pothos', '2', '7', '2021-08-19', '2020-08-19', '2020-08-19', null)
;
-- insert photo data --
INSERT INTO "photo"
("user_id", "plant_id", "photo_url", "date_uploaded")
VALUES
('1', '1', 'https://media.istockphoto.com/vectors/cute-plant-cartoon-hand-drawn-style-vector-id1163488567', '2021-08-19'),
('1', '1', 'https://media.istockphoto.com/vectors/cute-plant-cartoon-hand-drawn-style-vector-id1163488567', '2020-10-09'),
('1', '2', 'https://media.istockphoto.com/vectors/cute-plant-cartoon-hand-drawn-style-vector-id1163488567', '2020-08-19'),
('1', '3', 'https://media.istockphoto.com/vectors/cute-plant-cartoon-hand-drawn-style-vector-id1163488567', '2019-03-28'),
('1', '4', 'https://media.istockphoto.com/vectors/cute-plant-cartoon-hand-drawn-style-vector-id1163488567', '2020-06-03'),
('2', '5', 'https://media.istockphoto.com/vectors/cute-plant-cartoon-hand-drawn-style-vector-id1163488567', '2020-11-14')
;
-- // NOTES from spike below here // --
--"date_watered" TIMESTAMP DEFAULT CURRENT_DATE NOT NULL,
-- START HERE to create the DATES tabel --
CREATE TABLE "dates" (
"id" SERIAL PRIMARY KEY,
"date_added" DATE,
"date_watered" DATE,
"date_potted" DATE,
"date_fertilized" DATE,
"water_freq" INT
);
SELECT *
FROM "dates";
-- instert dummy valuse to see if working correctly --
INSERT INTO "dates"
("date_added", "date_watered", "date_potted", "date_fertilized", "water_freq")
VALUES
('1998-05-08', '2021-11-10', '1999-08-27', '1999-08-17', '5'),
('2005-05-08', '2021-11-28', '2008-08-27', '2010-08-17', '7');
-- as if we updated the date_watered with the current via an update from the DOM --
UPDATE "dates"
SET "date_watered" = CURRENT_DATE
WHERE "id" = 1;
-- can get date values straight from the SQL server --
SELECT NOW();
SELECT CURRENT_DATE;
SELECT DATE_PART('year', NOW()) AS "YEAR" ;
SELECT DATE_PART('month', NOW()) AS "MONTH" ;
SELECT DATE_PART('day', NOW()) AS "DAY" ;
-- can add the current_date on to any query --
SELECT CURRENT_DATE, *
FROM "dates";
-- current date + 5 days --
SELECT CURRENT_DATE, CURRENT_DATE + INTERVAL '5 days' AS "current_date_5";
-- adding days to the last time this plant was watered --
-- how to get table values into the interval?? --
SELECT CURRENT_DATE, *, ("date_watered" + INTERVAL '5 days') AS "next_water_date"
FROM "dates";
-- how to use other table data to add to the date? --
SELECT CURRENT_DATE, "date_watered", "water_freq", "date_watered" + (INTERVAL '1 days' * "water_freq") AS "next_water_date"
FROM "dates"
ORDER BY "next_water_date" ASC;
--SELECT
-- CURRENT_DATE,
-- DATE_PART('day', NOW()) AS "curret_day",
--
-- "date_watered",
-- DATE_PART('day', "date_watered") AS "last_water_day",
--
-- "water_freq",
-- "date_watered" + INTERVAL '1 days' * "water_freq" AS "next_water_day"
--
--FROM "dates";
-- the two dates that we need to compare --
SELECT CURRENT_DATE, "date_watered" + INTERVAL '1 day' * "water_freq" AS "next_water"
FROM "dates";
SELECT CURRENT_DATE, "plants".date_watered
FROM "plants";