-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.sql
More file actions
281 lines (248 loc) · 7.33 KB
/
create.sql
File metadata and controls
281 lines (248 loc) · 7.33 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- Created by Redgate Data Modeler (https://datamodeler.redgate-platform.com)
-- Last modification date: 2025-11-29 18:05:01.178
-- tables
-- Table: Administrator
CREATE TABLE Administrator (
aid int NOT NULL,
name text NOT NULL,
email text NOT NULL,
role text NOT NULL,
CONSTRAINT Administrator_pk PRIMARY KEY (aid)
);
-- Table: Brand
CREATE TABLE Brand (
brand_id int NOT NULL,
brand_name text NOT NULL,
description text NOT NULL,
CONSTRAINT Brand_pk PRIMARY KEY (brand_id)
);
-- Table: Card
CREATE TABLE Card (
method_id int NOT NULL,
card_number int NOT NULL,
card_network text NOT NULL,
pin int NOT NULL,
owner_name text NOT NULL,
CONSTRAINT Card_pk PRIMARY KEY (method_id)
);
-- Table: Category
CREATE TABLE Category (
category_id int NOT NULL,
name text NOT NULL,
description text NOT NULL,
CONSTRAINT Category_pk PRIMARY KEY (category_id)
);
-- Table: CategoryBrand
CREATE TABLE CategoryBrand (
category_id int NOT NULL,
brand_id int NOT NULL,
CONSTRAINT CategoryBrand_pk PRIMARY KEY (category_id,brand_id)
);
-- Table: Customer
CREATE TABLE Customer (
cid int NOT NULL,
name text NOT NULL,
email text NOT NULL,
phone int NOT NULL,
registration_date date NOT NULL,
status text NOT NULL CHECK (status IN ('inactive', 'active')),
CONSTRAINT Customer_pk PRIMARY KEY (cid)
);
-- Table: Gift
CREATE TABLE Gift (
gift_id int NOT NULL,
sender_id int NOT NULL,
receiver_id int NOT NULL,
offer_id int NOT NULL,
CONSTRAINT noteq CHECK (sender_id <> receiver_id) NOT DEFERRABLE INITIALLY IMMEDIATE,
CONSTRAINT Gift_pk PRIMARY KEY (gift_id)
);
-- Table: Loyalty_Account
CREATE TABLE Loyalty_Account (
method_id int NOT NULL,
amount decimal(100,2) NOT NULL,
expiry_date date NOT NULL,
CONSTRAINT Loyalty_Account_pk PRIMARY KEY (method_id)
);
-- Table: Merchant
CREATE TABLE Merchant (
mid int NOT NULL,
aid int NOT NULL,
name text NOT NULL,
status text NOT NULL CHECK (status IN ('inactive', 'active')),
registration_date date NOT NULL,
business_type text NOT NULL,
pin_code int NOT NULL,
address text NOT NULL,
contact_number int NOT NULL,
CONSTRAINT Merchant_pk PRIMARY KEY (mid)
);
-- Table: Offer
CREATE TABLE Offer (
offer_id int NOT NULL,
mid int NOT NULL,
title text NOT NULL,
description text NOT NULL,
discount_value decimal(100,2) NOT NULL,
original_price decimal(100,2) NOT NULL,
status text NOT NULL CHECK (status IN ('inactive', 'active')),
valid_from date NOT NULL,
valid_to date NOT NULL,
max_redemptions int NOT NULL,
total_redemptions int NOT NULL,
max_per_customer int NOT NULL,
daily_cap int NOT NULL,
terms_and_conditions text NOT NULL,
CONSTRAINT Offer_pk PRIMARY KEY (offer_id)
);
-- Table: OfferCategory
CREATE TABLE OfferCategory (
offer_id int NOT NULL,
category_id int NOT NULL,
CONSTRAINT OfferCategory_pk PRIMARY KEY (offer_id,category_id)
);
-- Table: Payment_method
CREATE TABLE Payment_method (
method_id int NOT NULL,
customer_id int NOT NULL,
CONSTRAINT Payment_method_pk PRIMARY KEY (method_id)
);
-- Table: Redemption
CREATE TABLE Redemption (
rid int NOT NULL,
merchant_id int NOT NULL,
customer_id int NOT NULL,
offer_id int NOT NULL,
savings_amount decimal(100,2) NOT NULL,
redemption_date date NOT NULL,
status text NOT NULL CHECK (status IN ('pending', 'completed', 'cancelled')),
pin_entered int NOT NULL,
CONSTRAINT Redemption_pk PRIMARY KEY (rid)
);
-- Table: Save
CREATE TABLE Save (
offer_id int NOT NULL,
customer_id int NOT NULL,
CONSTRAINT Save_pk PRIMARY KEY (offer_id,customer_id)
);
-- foreign keys
-- Reference: Brand_CategoryBrand (table: CategoryBrand)
ALTER TABLE CategoryBrand ADD CONSTRAINT Brand_CategoryBrand
FOREIGN KEY (brand_id)
REFERENCES Brand (brand_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Category_CategoryBrand (table: CategoryBrand)
ALTER TABLE CategoryBrand ADD CONSTRAINT Category_CategoryBrand
FOREIGN KEY (category_id)
REFERENCES Category (category_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Category_OfferCategory (table: OfferCategory)
ALTER TABLE OfferCategory ADD CONSTRAINT Category_OfferCategory
FOREIGN KEY (category_id)
REFERENCES Category (category_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Customer_Payment_method (table: Payment_method)
ALTER TABLE Payment_method ADD CONSTRAINT Customer_Payment_method
FOREIGN KEY (customer_id)
REFERENCES Customer (cid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Customer_Redemptions (table: Redemption)
ALTER TABLE Redemption ADD CONSTRAINT Customer_Redemptions
FOREIGN KEY (customer_id)
REFERENCES Customer (cid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Gift_Customer1 (table: Gift)
ALTER TABLE Gift ADD CONSTRAINT Gift_Customer1
FOREIGN KEY (receiver_id)
REFERENCES Customer (cid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Gift_Customer2 (table: Gift)
ALTER TABLE Gift ADD CONSTRAINT Gift_Customer2
FOREIGN KEY (sender_id)
REFERENCES Customer (cid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Manager (table: Merchant)
ALTER TABLE Merchant ADD CONSTRAINT Manager
FOREIGN KEY (aid)
REFERENCES Administrator (aid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Offer_Gift (table: Gift)
ALTER TABLE Gift ADD CONSTRAINT Offer_Gift
FOREIGN KEY (offer_id)
REFERENCES Offer (offer_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Offer_OfferCategory (table: OfferCategory)
ALTER TABLE OfferCategory ADD CONSTRAINT Offer_OfferCategory
FOREIGN KEY (offer_id)
REFERENCES Offer (offer_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Offers (table: Offer)
ALTER TABLE Offer ADD CONSTRAINT Offers
FOREIGN KEY (mid)
REFERENCES Merchant (mid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Payment_method_Card (table: Card)
ALTER TABLE Card ADD CONSTRAINT Payment_method_Card
FOREIGN KEY (method_id)
REFERENCES Payment_method (method_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Payment_method_Loyalty_Account (table: Loyalty_Account)
ALTER TABLE Loyalty_Account ADD CONSTRAINT Payment_method_Loyalty_Account
FOREIGN KEY (method_id)
REFERENCES Payment_method (method_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Redemed_Offers (table: Redemption)
ALTER TABLE Redemption ADD CONSTRAINT Redemed_Offers
FOREIGN KEY (offer_id)
REFERENCES Offer (offer_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Redemptions (table: Redemption)
ALTER TABLE Redemption ADD CONSTRAINT Redemptions
FOREIGN KEY (merchant_id)
REFERENCES Merchant (mid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Save_Customer (table: Save)
ALTER TABLE Save ADD CONSTRAINT Save_Customer
FOREIGN KEY (customer_id)
REFERENCES Customer (cid)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- Reference: Save_Offer (table: Save)
ALTER TABLE Save ADD CONSTRAINT Save_Offer
FOREIGN KEY (offer_id)
REFERENCES Offer (offer_id)
NOT DEFERRABLE
INITIALLY IMMEDIATE
;
-- End of file.