-
Notifications
You must be signed in to change notification settings - Fork 1
/
SQLQuery1.sql
177 lines (165 loc) · 4.42 KB
/
SQLQuery1.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
CREATE DATABASE RECIPE_APP
USE RECIPE_APP
CREATE TABLE ACCOUNT(
ACC_ID INTEGER NOT NULL IDENTITY(1,1),
EMAIL VARCHAR(255) NOT NULL,
DONATION_AMOUNT INTEGER NULL,
USERNAME VARCHAR(255) NOT NULL,
FOLLOWER_COUNT INTEGER NULL,
DONATION_COUNT INTEGER NULL,
IMAGE_URL VARCHAR(255) NULL,
PASSWORD_HASH VARCHAR(255) NOT NULL,
PRIMARY KEY (ACC_ID)
);
GO
CREATE TABLE DONATION_REC(
DON_ID INTEGER NOT NULL IDENTITY(1,1),
DONOR_ID INTEGER NOT NULL,
RECIPIENT_ID INTEGER NOT NULL,
AMOUNT INTEGER NOT NULL,
DATE_D VARCHAR(255) NOT NULL,
PRIMARY KEY(DON_ID)
);
GO
CREATE TABLE COMMENT(
COM_ID INTEGER NOT NULL IDENTITY(1,1),
RATING INTEGER NOT NULL,
DATE VARCHAR(255) NOT NULL,
CONTENT VARCHAR( 255) NOT NULL,
PROFILE_COMMENT_ID INTEGER NOT NULL,
RECIPE_COMMENT_ID INTEGER NOT NULL,
PRIMARY KEY (COM_ID)
);
GO
CREATE TABLE INGREDIENT(
INGRE_ID INTEGER NOT NULL IDENTITY(1,1),
NAME VARCHAR(255) NOT NULL,
UNIT VARCHAR(255) NOT NULL,
PRIMARY KEY (INGRE_ID)
);
GO
CREATE TABLE RECIPE(
REC_ID INTEGER NOT NULL IDENTITY(1,1),
RATING INTEGER NOT NULL,
SAVE_COUNT INTEGER NOT NULL,
TITLE VARCHAR(255) NOT NULL,
THUMBNAIL_URL VARCHAR(255) NOT NULL,
VIEW_COUNT VARCHAR(255) NOT NULL,
PROFILE_ID INTEGER NOT NULL,
PRIMARY KEY (REC_ID)
);
GO
CREATE TABLE STEP(
S_ID INTEGER NOT NULL IDENTITY(1,1),
CONTENT VARCHAR(255) NOT NULL,
IMAGE_URL VARCHAR(255) NOT NULL,
RECIPE_STEP_ID INTEGER NOT NULL,
PRIMARY KEY (S_ID)
);
GO
CREATE TABLE CAT_GROUP(
CG_ID INTEGER NOT NULL IDENTITY(1,1),
NAME VARCHAR(255) NOT NULL,
PRIMARY KEY (CG_ID)
);
GO
CREATE TABLE CATEGORY(
CAT_ID INTEGER NOT NULL IDENTITY(1,1),
NAME VARCHAR(255) NOT NULL,
CG_ID INTEGER NOT NULL,
PRIMARY KEY (CAT_ID)
);
GO
CREATE TABLE RECIPE_CAT(
RECIPE_CAT_ID INTEGER NOT NULL,
CATEGORY_ID INTEGER NOT NULL,
PRIMARY KEY (RECIPE_CAT_ID , CATEGORY_ID)
);
GO
CREATE TABLE RECIPE_INGREDIENT(
RECIPE_INGRE_ID INTEGER NOT NULL,
INGREDIENT_ID INTEGER NOT NULL,
QUANTITIATIVE FLOAT NOT NULL,
PRIMARY KEY (RECIPE_INGRE_ID, INGREDIENT_ID)
);
GO
ALTER TABLE RECIPE_INGREDIENT
ADD CONSTRAINT FK_RECIPE_INGRE_ID FOREIGN KEY (RECIPE_INGRE_ID) REFERENCES RECIPE (REC_ID),
CONSTRAINT FK_INGREDIENT_ID FOREIGN KEY (INGREDIENT_ID) REFERENCES INGREDIENT (INGRE_ID);
GO
ALTER TABLE RECIPE_CAT
ADD CONSTRAINT FK_RECIPE_CAT_ID FOREIGN KEY (RECIPE_CAT_ID) REFERENCES RECIPE(REC_ID),
CONSTRAINT FK_CATEGORY_ID FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY (CAT_ID);
GO
ALTER TABLE CATEGORY
ADD CONSTRAINT FK_CG_ID FOREIGN KEY (CG_ID) REFERENCES CAT_GROUP (CG_ID);
GO
ALTER TABLE STEP
ADD CONSTRAINT FK_RECIPE_STEP_ID FOREIGN KEY (RECIPE_STEP_ID) REFERENCES RECIPE (REC_ID);
GO
ALTER TABLE RECIPE
ADD CONSTRAINT FK_PROFILE_ID FOREIGN KEY (PROFILE_ID) REFERENCES ACCOUNT (ACC_ID);
GO
ALTER TABLE COMMENT
ADD CONSTRAINT FK_PROFILE_COMMENT_ID FOREIGN KEY (PROFILE_COMMENT_ID) REFERENCES ACCOUNT(ACC_ID),
CONSTRAINT FK_RECIPE_COMMENT_ID FOREIGN KEY (RECIPE_COMMENT_ID) REFERENCES RECIPE(REC_ID);
GO
ALTER TABLE DONATION_REC
ADD CONSTRAINT FK_DONOR_ID FOREIGN KEY (DONOR_ID) REFERENCES ACCOUNT (ACC_ID),
CONSTRAINT FK_RECIPIENT_ID FOREIGN KEY (RECIPIENT_ID) REFERENCES ACCOUNT (ACC_ID);
GO
-----------------------------------------
ALTER TABLE RECIPE_INGREDIENT
DROP CONSTRAINT FK_RECIPE_INGRE_ID,
CONSTRAINT FK_INGREDIENT_ID;
GO
ALTER TABLE RECIPE_CAT
DROP CONSTRAINT FK_RECIPE_CAT_ID,
CONSTRAINT FK_CATEGORY_ID;
GO
ALTER TABLE CATEGORY
DROP CONSTRAINT FK_CG_ID;
GO
ALTER TABLE STEP
DROP CONSTRAINT FK_RECIPE_STEP_ID;
GO
ALTER TABLE RECIPE
DROP CONSTRAINT FK_PROFILE_ID;
GO
ALTER TABLE COMMENT
DROP CONSTRAINT FK_PROFILE_COMMENT_ID,
CONSTRAINT FK_RECIPE_COMMENT_ID;
GO
ALTER TABLE DONATION_REC
DROP CONSTRAINT FK_DONOR_ID,
CONSTRAINT FK_RECIPIENT_ID;
GO
--DROP TABLE ACCOUNT
--DROP TABLE DONATION_REC
--DROP TABLE COMMENT
--DROP TABLE CAT_GROUP
--DROP TABLE CATEGORY
--DROP TABLE RECIPE_CAT
--DROP TABLE INGREDIENT
--DROP TABLE RECIPE
--DROP TABLE RECIPE_INGREDIENT
--DROP TABLE STEP
-------------------------------------------------------
CREATE TRIGGER HANDLE_DONATION_COUNT
ON DONATION_REC AFTER INSERT AS
BEGIN
UPDATE ACCOUNT
SET DONATION_COUNT = DONATION_COUNT + 1
FROM inserted
WHERE ACC_ID = inserted.RECIPIENT_ID
END;
CREATE TRIGGER HANDLE_DONATION_AMOUNT
ON DONATION_REC AFTER INSERT AS
BEGIN
UPDATE ACCOUNT
SET DONATION_AMOUNT = DONATION_AMOUNT + inserted.AMOUNT
FROM inserted
WHERE ACC_ID = INSERTED.RECIPIENT_ID
END;
INSERT INTO DONATION_REC(DONOR_ID,RECIPIENT_ID,AMOUNT,DATE_D) VALUES (12,13,14,'12/5')
SELECT * FROM DONATION_REC