DatabaseCreation: dml.sql

File dml.sql, 34.6 KB (added by 231012, 27 hours ago)
Line 
1--Inserti
2
3--1.User
4SELECT conname
5FROM pg_constraint
6WHERE conrelid = 'public."User"'::regclass
7AND contype = 'u';
8
9ALTER TABLE "User" ALTER COLUMN ssn DROP NOT NULL;
10CREATE TEMP TABLE first_names(name text);
11CREATE TEMP TABLE last_names(surname text);
12
13INSERT INTO first_names VALUES
14('James'), ('Mary'), ('John'), ('Patricia'), ('Robert'),
15('Jennifer'), ('Michael'), ('Linda'), ('William'), ('Elizabeth'),
16('David'), ('Barbara'), ('Richard'), ('Susan'), ('Joseph'),
17('Jessica'), ('Thomas'), ('Sarah'), ('Charles'), ('Karen'),
18('Christopher'), ('Nancy'), ('Daniel'), ('Lisa'), ('Matthew'),
19('Betty'), ('Anthony'), ('Margaret'), ('Mark'), ('Sandra'),
20('Donald'), ('Ashley'), ('Steven'), ('Kimberly'), ('Paul'),
21('Emily'), ('Andrew'), ('Donna'), ('Joshua'), ('Michelle'),
22('Kenneth'), ('Dorothy'), ('Kevin'), ('Carol'), ('Brian'),
23('Amanda'), ('George'), ('Melissa'), ('Edward'), ('Deborah'),
24('Ronald'), ('Stephanie'), ('Timothy'), ('Rebecca'), ('Jason'),
25('Laura'), ('Jeffrey'), ('Helen'), ('Ryan'), ('Sharon');
26
27INSERT INTO last_names VALUES
28('Smith'), ('Johnson'), ('Williams'), ('Brown'), ('Jones'),
29('Garcia'), ('Miller'), ('Davis'), ('Rodriguez'), ('Martinez'),
30('Hernandez'), ('Lopez'), ('Gonzalez'), ('Wilson'), ('Anderson'),
31('Thomas'), ('Taylor'), ('Moore'), ('Jackson'), ('Martin'),
32('Lee'), ('Perez'), ('Thompson'), ('White'), ('Harris'),
33('Sanchez'), ('Clark'), ('Ramirez'), ('Lewis'), ('Robinson'),
34('Walker'), ('Young'), ('Allen'), ('King'), ('Wright'),
35('Scott'), ('Torres'), ('Nguyen'), ('Hill'), ('Flores'),
36('Green'), ('Adams'), ('Nelson'), ('Baker'), ('Hall'),
37('Rivera'), ('Campbell'), ('Mitchell'), ('Carter'), ('Roberts'),
38('Gomez'), ('Phillips'), ('Evans'), ('Turner'), ('Diaz'),
39('Parker'), ('Cruz'), ('Edwards'), ('Collins'), ('Reyes');
40
41create table if not exists temp_ssn (
42 id bigserial primary key,
43 ssn varchar(13)
44);
45
46insert into temp_ssn (ssn)
47select
48 to_char(data.date_birth, 'DDMM') ||
49 to_char(extract(year from data.date_birth)::integer % 1000, 'FM099') ||
50 '4' || place || gender ||
51 to_char(number, 'FM099') as ssn
52from (
53 select
54 (now() - interval '70 years' * random())::date as date_birth,
55 (1 + random()*8)::integer as place,
56 (random()*5)::integer as gender,
57 (random()*100)::integer as number
58 from generate_series(1, 200000) s(i)
59) as data;
60
61INSERT INTO "User" (
62 name, surname, email, telephone_num, date_registration, type
63)
64SELECT
65 (SELECT name FROM first_names ORDER BY random() + gs.n LIMIT 1),
66 (SELECT surname FROM last_names ORDER BY random() + gs.n LIMIT 1),
67 lower(
68 (SELECT name FROM first_names ORDER BY random() + gs.n LIMIT 1)
69 || '.' ||
70 (SELECT surname FROM last_names ORDER BY random() + gs.n LIMIT 1)
71 || gs.n || '@example.com'
72 ),
73 '+3897' || (100000 + (random()*899999)::int),
74 CURRENT_DATE - ((random()*3650)::int),
75 CASE
76 WHEN random() < 0.5 THEN 'guest'
77 ELSE 'host'
78 END
79FROM generate_series(1, 200000) gs(n);
80
81UPDATE "User"
82SET email = lower(name || '.' || surname || user_id || '@example.com');
83
84WITH shuffled_ssn AS (
85 SELECT ssn,
86 row_number() OVER (ORDER BY random()) AS rn
87 FROM temp_ssn
88),
89numbered_users AS (
90 SELECT user_id,
91 row_number() OVER (ORDER BY user_id) AS rn
92 FROM "User"
93)
94UPDATE "User" u
95SET ssn = s.ssn
96FROM numbered_users nu
97JOIN shuffled_ssn s ON nu.rn = s.rn
98WHERE u.user_id = nu.user_id;
99
100DELETE
101FROM "User" u
102WHERE user_id NOT IN (
103 SELECT MIN(user_id)
104 FROM "User"
105 GROUP BY ssn
106);
107
108TRUNCATE TABLE Payment RESTART identity CASCADE;
109DROP TABLE IF EXISTS first_names;
110DROP TABLE IF EXISTS last_names;
111DROP TABLE IF EXISTS temp_ssn;
112
113SELECT * FROM "User" LIMIT 100;
114
115TRUNCATE TABLE "User" RESTART identity CASCADE;
116DROP TABLE IF EXISTS first_names;
117DROP TABLE IF EXISTS last_names;
118DROP TABLE IF EXISTS temp_ssn;
119
120--2. CampType ------------------------------------------------
121INSERT INTO CampType (type_name, description)
122VALUES
123('camp', 'Basic camping area for outdoor activities'),
124('bungalow', 'Wooden or luxury accommodation units'),
125('tent', 'Accommodation in a tent in nature');
126
127--3. EquipmentType---------------------------------------------------
128INSERT INTO EquipmentType (type_name, description)
129VALUES
130('Shelter Equipment', 'Equipment used for protection and sleeping outdoors'),
131('Sleeping Equipment', 'Items used for sleeping comfort and warmth'),
132('Cooking Equipment', 'Tools for preparing food outdoors'),
133('Lighting Equipment', 'Devices for illumination at night'),
134('Furniture', 'Portable outdoor furniture for camping'),
135('Safety Equipment', 'Emergency and protection gear'),
136('Adventure Equipment', 'Gear for hiking and outdoor activities'),
137('Water Equipment', 'Water storage and purification tools'),
138('Climbing Equipment', 'Gear used for climbing and vertical activities'),
139('Fishing Equipment', 'Tools and gear for fishing activities'),
140('Winter Equipment', 'Equipment for cold weather camping'),
141('Navigation Equipment', 'Tools for orientation and navigation like GPS and compasses'),
142('Hygiene Equipment', 'Items for personal hygiene in outdoor conditions'),
143('Entertainment Equipment', 'Gear for leisure and recreation during camping'),
144('Power Equipment', 'Portable energy sources like generators and solar panels'),
145('Repair Equipment', 'Tools for fixing and maintaining camping gear'),
146('Storage Equipment', 'Containers and bags for organizing equipment'),
147('Medical Equipment', 'Advanced medical and emergency care tools'),
148('Fire Equipment', 'Tools for fire starting and fire safety like matches and fire starters'),
149('Hiking Equipment', 'Essential gear for hiking and trekking activities'),
150('Cycling Equipment', 'Gear used for biking and cycling trips'),
151('Skiing Equipment', 'Equipment for snow skiing activities'),
152('Cooking Accessories', 'Small accessories used in outdoor food preparation'),
153('Camping Tools', 'General tools used for setting up and maintaining camp'),
154('Electronics Equipment', 'Electronic devices used in camping such as chargers and radios'),
155('Kids Equipment', 'Camping gear designed specifically for children'),
156('Pet Equipment', 'Equipment and accessories for pets during camping'),
157('Emergency Equipment', 'Critical survival and emergency response tools');
158
159
160--4. Service-----------------------------------------------------------
161INSERT INTO Service (service_name, description, additional_fee) VALUES
162('WiFi', 'Internet access service', 15.00),
163('Electricity', 'Power supply service (estimated monthly usage)', 60.00),
164('Water Supply', 'Cold and hot water utility', 20.00),
165('Heating', 'Central heating system usage', 50.00),
166('Air Conditioning', 'Cooling system usage and maintenance', 25.00),
167('Gas Supply', 'Gas utility for heating and cooking', 35.00),
168('Waste Collection', 'Garbage removal and processing service', 10.00),
169('Security Service', 'Building security and monitoring system', 40.00),
170('Maintenance Service', 'General building maintenance support', 30.00),
171('Cleaning Service', 'Regular cleaning of shared spaces', 18.00),
172('Parking', 'Assigned parking space usage', 45.00),
173('Elevator Service', 'Elevator maintenance and operation cost', 12.00),
174('Lighting', 'Common area electricity usage', 22.00),
175('Hot Water', 'Hot water system usage', 28.00),
176('Internet Upgrade', 'High-speed internet package upgrade', 25.00),
177('Laundry Service', 'Shared washing and drying facilities', 12.00),
178('Gym Access', 'Access to fitness facilities', 35.00),
179('Sauna Access', 'Use of sauna facilities', 20.00),
180('Swimming Pool Access', 'Access to pool facilities', 30.00),
181('Room Cleaning Extra', 'Deep cleaning service on request', 25.00),
182('Trash Room Pickup', 'Direct waste pickup from unit', 6.00),
183('Mail Handling', 'Receiving and handling mail/parcels', 4.00),
184('Visitor Parking', 'Temporary parking for guests', 10.00);
185
186
187--5. Availability------------------------------------------------------------
188INSERT INTO Availability (available_from_date, available_to_date, status)
189SELECT
190 from_date,
191 from_date + (1 + floor(random() * 14))::int,
192 CASE
193 WHEN random() < 0.75 THEN 'available'
194 WHEN random() < 0.9 THEN 'booked'
195 ELSE 'unavailable'
196 END
197FROM (
198 SELECT CURRENT_DATE + (floor(random() * 365))::int AS from_date
199 FROM generate_series(1, 2000000)
200) dates;
201
202
203--6. CampLocation---------------------------------------------
204INSERT INTO CampLocation (
205 CampTypetypeId, name, description, type,
206 max_guests, min_nights_stay, status, season,
207 opening_date, closing_date
208)
209SELECT
210 floor(random()*3 + 1)::int,
211 'Camp Location ' || n,
212 'Auto generated camping location #' || n,
213
214 (ARRAY['tent', 'cabin', 'bungalow'])[floor(random()*3)::int + 1],
215
216 (floor(random()*12) + 2)::int,
217 (floor(random()*7) + 1)::int,
218
219 CASE
220 WHEN random() < 0.8 THEN 'active'
221 WHEN random() < 0.95 THEN 'inactive'
222 ELSE 'closed'
223 END,
224
225 (ARRAY['spring','summer','autumn','winter','all'])[floor(random()*5)::int + 1],
226
227 d1,
228 d1 + (floor(random()*30) + 1)::int * INTERVAL '1 day'
229
230FROM (
231 SELECT
232 n,
233 CURRENT_DATE + (floor(random()*365)::int) * INTERVAL '1 day' AS d1
234 FROM generate_series(1, 100000) n
235) data;
236
237
238--7. Activity------------------------------------------------------
239INSERT INTO Activity (
240 location_id,
241 name,
242 description,
243 price_per_person,
244 max_participants,
245 duration_hours,
246 difficulty_level,
247 equipment_provided
248)
249SELECT
250 (gs.n % 100000) AS location_id,
251 CASE r
252 WHEN 0 THEN 'Hiking'
253 WHEN 1 THEN 'Camping'
254 WHEN 2 THEN 'Running Tour'
255 WHEN 3 THEN 'Kayaking'
256 WHEN 4 THEN 'Cycling Tour'
257 WHEN 5 THEN 'Picnic Experience'
258 WHEN 6 THEN 'Rock Climbing'
259 WHEN 7 THEN 'Fishing Trip'
260 WHEN 8 THEN 'Mountain Trekking'
261 WHEN 9 THEN 'Yoga in Nature'
262 WHEN 10 THEN 'Horse Riding'
263 WHEN 11 THEN 'Bird Watching'
264 WHEN 12 THEN 'Paragliding'
265 WHEN 13 THEN 'Ziplining'
266 WHEN 14 THEN 'Rafting'
267 WHEN 15 THEN 'Skiing'
268 WHEN 16 THEN 'Snowboarding'
269 WHEN 17 THEN 'Caving'
270 WHEN 18 THEN 'Safari Tour'
271 WHEN 19 THEN 'Photography Tour'
272 WHEN 20 THEN 'Trail Running'
273 WHEN 21 THEN 'Nature Walk'
274 WHEN 22 THEN 'Meditation Retreat'
275 WHEN 23 THEN 'Swimming Adventure'
276 WHEN 24 THEN 'Cliff Jumping'
277 WHEN 25 THEN 'Archery Practice'
278 WHEN 26 THEN 'Stand Up Paddleboarding'
279 WHEN 27 THEN 'Wildlife Exploration'
280 WHEN 28 THEN 'Forest Survival Training'
281 ELSE 'Sunset Watching Tour'
282 END AS name,
283 CASE r
284 WHEN 0 THEN 'guided hiking in mountains'
285 WHEN 1 THEN 'overnight camping in nature'
286 WHEN 2 THEN 'city running tour experience'
287 WHEN 3 THEN 'river kayaking adventure'
288 WHEN 4 THEN 'cycling through scenic routes'
289 WHEN 5 THEN 'relaxing picnic outdoors'
290 WHEN 6 THEN 'rock climbing training session'
291 WHEN 7 THEN 'fishing at lake or river'
292 WHEN 8 THEN 'high altitude trekking experience'
293 WHEN 9 THEN 'yoga session in nature'
294 WHEN 10 THEN 'horse riding trail adventure'
295 WHEN 11 THEN 'bird watching tour'
296 WHEN 12 THEN 'paragliding flight experience'
297 WHEN 13 THEN 'zipline adventure through forest'
298 WHEN 14 THEN 'white water rafting experience'
299 WHEN 15 THEN 'skiing on mountain slopes'
300 WHEN 16 THEN 'snowboarding adventure'
301 WHEN 17 THEN 'exploring natural caves'
302 WHEN 18 THEN 'safari wildlife tour'
303 WHEN 19 THEN 'guided nature photography tour'
304 WHEN 20 THEN 'long distance trail running'
305 WHEN 21 THEN 'easy nature walking tour'
306 WHEN 22 THEN 'guided meditation retreat outdoors'
307 WHEN 23 THEN 'open water swimming activity'
308 WHEN 24 THEN 'jumping from cliffs into water'
309 WHEN 25 THEN 'archery skill training'
310 WHEN 26 THEN 'paddleboarding on calm waters'
311 WHEN 27 THEN 'wildlife exploration trip'
312 WHEN 28 THEN 'forest survival skill training'
313 ELSE 'sunset viewing experience'
314 END AS description,
315 (10 + floor(random() * 200))::int AS price_per_person,
316 (2 + floor(random() * 20))::int AS max_participants,
317 (1 + floor(random() * 10))::int AS duration_hours,
318 CASE
319 WHEN random() < 0.2 THEN NULL
320 ELSE (1 + floor(random() * 5))::int
321 END AS difficulty_level,
322 CASE
323 WHEN random() < 0.3 THEN 'Provided'
324 WHEN random() < 0.6 THEN 'Basic equipment included'
325 ELSE 'Bring your own equipment'
326 END AS equipment_provided
327FROM (
328 SELECT row_number() OVER () AS n, floor(random() * 30)::int AS r
329 FROM generate_series(1, 250000)
330) gs;
331
332
333--------------------------------------------------------------
334--temp_tables za proverka
335CREATE TEMP TABLE test_user AS SELECT * FROM "User" LIMIT 1000;
336CREATE TEMP TABLE test_guest AS SELECT * FROM Guest LIMIT 1;
337CREATE TEMP TABLE test_host AS SELECT * FROM Host LIMIT 1;
338
339UPDATE test_user SET type = 'guest';
340UPDATE test_user SET type = 'host'
341WHERE user_id IN
342(SELECT user_id
343FROM test_user
344ORDER BY random()
345LIMIT (SELECT COUNT(*) * 0.3
346FROM test_user));
347
348select * from test_user;
349SELECT type, COUNT(*) FROM test_user GROUP BY type;
350
351INSERT INTO test_guest (UseruserId, num_reservations, preference_type)
352SELECT
353 user_id,
354 floor(random() * 30)::int,
355 CASE (floor(random() * 5))::int
356 WHEN 0 THEN 'mountain'
357 WHEN 1 THEN 'forest'
358 WHEN 2 THEN 'lake'
359 WHEN 3 THEN 'beach'
360 ELSE 'luxury'
361 END
362FROM test_user
363WHERE type = 'guest';
364
365INSERT INTO test_host (UseruserId, IBAN, average_score, is_verified)
366SELECT
367 user_id,
368 'MK' || (100000000000000000 + floor(random() * 899999999999999999))::text,
369 (1 + floor(random() * 5))::int,
370 CASE
371 WHEN random() < 0.7 THEN 'yes'
372 ELSE 'no'
373 END
374FROM test_user
375WHERE type = 'host';
376
377SELECT * FROM test_guest LIMIT 5;
378SELECT * FROM test_host LIMIT 5;
379-----------------------------------
380UPDATE "User" SET type = 'guest';
381
382UPDATE "User" SET type = 'host'
383WHERE user_id IN
384(SELECT user_id
385FROM "User"
386ORDER BY random()
387LIMIT (SELECT COUNT(*) * 0.3 FROM "User"));
388
389SELECT type, COUNT(*) FROM "User" GROUP BY type;
390
391--8. Guest----------------------------------------------------
392INSERT INTO Guest (UseruserId, num_reservations, preference_type)
393SELECT
394 user_id,
395 floor(random() * 30)::int,
396 CASE (floor(random() * 5))::int
397 WHEN 0 THEN 'mountain'
398 WHEN 1 THEN 'forest'
399 WHEN 2 THEN 'lake'
400 WHEN 3 THEN 'beach'
401 ELSE 'luxury'
402 END
403FROM "User"
404WHERE type = 'guest';
405
406
407--9. Host-----------------------------------------------------
408INSERT INTO Host (UseruserId, IBAN, average_score, is_verified)
409SELECT
410 user_id,
411 'MK' || to_char(floor(random() * 100000000)::bigint, 'FM00000000') ||
412 to_char(floor(random() * 100000000)::bigint, 'FM00000000'),
413 (1 + floor(random() * 5))::int,
414 CASE
415 WHEN random() < 0.7 THEN 'yes'
416 ELSE 'no'
417 END
418FROM "User"
419WHERE type = 'host';
420
421SELECT COUNT(*) FROM Guest;
422SELECT COUNT(*) FROM Host;
423
424--10. Promotion---------------------------------------------------------------
425DROP TABLE IF EXISTS temp_promo_hosts;
426
427CREATE TEMP TABLE temp_promo_hosts AS
428WITH host_array AS (
429 SELECT array_agg(UseruserId ORDER BY random()) AS hosts
430 FROM Host
431)
432SELECT
433 gs.n,
434 hosts[((gs.n - 1) % array_length(hosts, 1)) + 1] AS UseruserId
435FROM generate_series(1, 300000) gs(n)
436CROSS JOIN host_array;
437
438SELECT COUNT(DISTINCT UseruserId) FROM temp_promo_hosts;
439
440INSERT INTO Promotion (
441 HostUserUserId,
442 promo_code,
443 discount_percent,
444 discount_amount,
445 valid_from,
446 valid_to,
447 description
448)
449WITH base AS (
450 SELECT
451 gs.n,
452 h.UseruserId,
453 random() AS r,
454 CURRENT_DATE - (floor(random() * 365))::int AS d1
455 FROM generate_series(1, 300000) gs(n)
456 JOIN temp_promo_hosts h ON h.n = gs.n
457)
458SELECT
459 UseruserId,
460 'PROMO-' || n,
461 CASE WHEN r < 0.6 THEN (floor(random() * 8) + 1)::int * 10 ELSE 0 END,
462 CASE WHEN r >= 0.6 THEN ((floor(random() * 20) + 1)::int * 100)::numeric ELSE 0 END,
463 d1,
464 d1 + (1 + floor(random() * 30))::int,
465 'Promotion offer ' || n
466FROM base;
467
468
469--11.Host_Host-----------------------------------------
470INSERT INTO Host_Host (CampLocationcampLocationId, HostUseruserid)
471SELECT
472 cl.campLocationId,
473 h.UseruserId
474FROM CampLocation cl
475JOIN LATERAL (
476 SELECT UseruserId
477 FROM Host
478 ORDER BY random()
479 LIMIT (1 + floor(random() * 3))::int
480) h ON true;
481
482--12.camp_location_service-----------------------------------
483INSERT INTO camp_location_service (ServiceserviceId, CampLocationcampLocationId)
484SELECT
485 s.serviceId,
486 cl.campLocationId
487FROM CampLocation cl
488JOIN LATERAL (
489 SELECT serviceId
490 FROM Service
491 ORDER BY random()
492 LIMIT (1 + floor(random() * 5))::int
493) s ON TRUE;
494
495
496--13.Activity_Activity-----------------------------------------
497INSERT INTO Activity_Activity (
498 Activityactivity_id2,
499 CampLocationcampLocationId
500)
501SELECT
502 act.activity_id,
503 cl.campLocationId
504FROM CampLocation cl
505JOIN LATERAL (
506 SELECT activity_id
507 FROM Activity
508 ORDER BY random()
509 LIMIT (2 + floor(random() * 5))::int
510) act ON TRUE;
511
512--14.Availability_CampLocation_has-------------------------------
513INSERT INTO Availability_CampLocation_has (
514 AvailabilityavailabilityId,
515 CampLocationcampLocationId
516)
517WITH avail_array AS (
518 SELECT array_agg(availabilityId) AS ids
519 FROM Availability
520)
521SELECT
522 ids[1 + floor(random() * array_length(ids, 1))::int] AS availabilityId,
523 cl.campLocationId
524FROM CampLocation cl
525CROSS JOIN avail_array;
526
527
528--15.Favorites----------------------------------------
529INSERT INTO Favorites (locationId, date_added)
530WITH cl_array AS (
531 SELECT array_agg(campLocationId) AS ids
532 FROM CampLocation
533)
534SELECT
535 ids[1 + floor(random() * array_length(ids, 1))::int],
536 CURRENT_DATE - (floor(random() * 365))::int
537FROM generate_series(1, 300000)
538CROSS JOIN cl_array;
539
540
541--16.Favorites_Guest_saves-----------------------------
542INSERT INTO Favorites_Guest_saves (FavoritesfavoriteId, GuestUseruserId)
543SELECT
544 f.favorite_id,
545 g.UseruserId
546FROM Favorites f
547JOIN LATERAL (
548 SELECT UseruserId
549 FROM Guest
550 ORDER BY random()
551 LIMIT (1 + floor(random() * 3))::int
552) g ON true;
553
554--17.EmergancyContact---------------------------
555INSERT INTO EmergencyContact (
556 CampLocationcampLocationId,
557 contact_name,
558 phone_number,
559 created_at
560)
561WITH cl_array AS (
562 SELECT array_agg(campLocationId) AS ids,
563 array_agg(name) AS names
564 FROM CampLocation
565)
566SELECT
567 ids[1 + floor(random() * array_length(ids, 1))::int],
568 'Emergency Contact ' || gs.n,
569 '+3897' || lpad((floor(random() * 10000000))::text, 7, '0'),
570 CURRENT_DATE - (floor(random() * 365))::int
571FROM generate_series(1, 100000) gs(n)
572CROSS JOIN cl_array;
573select * from emergencycontact;
574
575
576--18.PriceHistory----------------------------------
577INSERT INTO PriceHistory (
578 CampLocationcampLocationId,
579 price_per_night,
580 date_from,
581 date_to
582)
583WITH cl_array AS (
584 SELECT array_agg(campLocationId) AS ids
585 FROM CampLocation
586),
587base AS (
588 SELECT
589 ids[1 + floor(random() * array_length(ids, 1))::int] AS campLocationId,
590 (floor(random() * 30) + 1)::int * 10 AS price,
591 DATE '2020-01-01' + (floor(random() * 1500))::int AS date_from
592 FROM generate_series(1, 5000000) gs
593 CROSS JOIN cl_array
594)
595SELECT
596 campLocationId,
597 price,
598 date_from,
599 date_from + (1 + floor(random() * 30))::int
600FROM base;
601
602
603--19.Equipment -------------------------------------
604TRUNCATE TABLE equipment RESTART IDENTITY CASCADE;
605INSERT INTO Equipment (
606 EquipmentTypeEquipmentTypeId,
607 CampLocationcampLocationId,
608 name,
609 description,
610 total_quantity,
611 available_quantity,
612 rental_price_per_day,
613 deposit_amount,
614 condition_status,
615 is_available
616)
617WITH et_array AS (
618 SELECT array_agg(EquipmentTypeId) AS et_ids,
619 array_agg(type_name) AS et_names,
620 array_agg(description) AS et_descs
621 FROM EquipmentType
622),
623cl_array AS (
624 SELECT array_agg(campLocationId) AS cl_ids
625 FROM CampLocation
626),
627base AS (
628 SELECT
629 gs.n,
630 et_ids[1 + floor(random() * array_length(et_ids, 1))::int] AS et_id,
631 et_names[1 + floor(random() * array_length(et_names, 1))::int] AS et_name,
632 et_descs[1 + floor(random() * array_length(et_descs, 1))::int] AS et_desc,
633 cl_ids[1 + floor(random() * array_length(cl_ids, 1))::int] AS cl_id,
634 (floor(random()*20 + 1))::int AS total_qty,
635 (floor(random() * (floor(random()*20 + 1))))::int AS avail_qty
636 FROM generate_series(1, 1000000) gs(n)
637 CROSS JOIN et_array
638 CROSS JOIN cl_array
639)
640SELECT
641 et_id,
642 cl_id,
643 CASE et_name
644 WHEN 'Shelter Equipment' THEN (ARRAY['4-Person Tent','Ultralight Tent','Tarp Shelter','Tent Poles Set','Groundsheet'])[floor(random()*5 + 1)::int]
645 WHEN 'Sleeping Equipment' THEN (ARRAY['Sleeping Bag - Winter','Sleeping Bag - Summer','Inflatable Mattress','Camping Pillow','Thermal Blanket'])[floor(random()*5 + 1)::int]
646 WHEN 'Cooking Equipment' THEN (ARRAY['Portable Gas Stove','Camping Grill','Cooking Pot Set','Frying Pan Outdoor','Camping Kettle'])[floor(random()*5 + 1)::int]
647 WHEN 'Lighting Equipment' THEN (ARRAY['LED Camping Lantern','Headlamp','Solar Lamp','Flashlight Pro','String Camping Lights'])[floor(random()*5 + 1)::int]
648 WHEN 'Furniture' THEN (ARRAY['Camping Chair','Folding Table','Camping Stool','Portable Bench','Relax Chair Lounger'])[floor(random()*5 + 1)::int]
649 WHEN 'Safety Equipment' THEN (ARRAY['First Aid Kit','Emergency Whistle','Survival Kit','Fire Extinguisher','Safety Helmet'])[floor(random()*5 + 1)::int]
650 WHEN 'Adventure Equipment' THEN (ARRAY['Hiking Backpack','Climbing Harness','Rope Set','Adventure Helmet','Trail Gloves'])[floor(random()*5 + 1)::int]
651 WHEN 'Water Equipment' THEN (ARRAY['Water Filter','Water Tank','Hydration Pack','Water Purification Tablets','Portable Shower'])[floor(random()*5 + 1)::int]
652 WHEN 'Climbing Equipment' THEN (ARRAY['Climbing Rope','Carabiner Set','Climbing Shoes','Belay Device','Climbing Harness Pro'])[floor(random()*5 + 1)::int]
653 WHEN 'Fishing Equipment' THEN (ARRAY['Fishing Rod','Fishing Net','Tackle Box','Fishing Reel','Bait Kit'])[floor(random()*5 + 1)::int]
654 WHEN 'Winter Equipment' THEN (ARRAY['Snow Boots','Winter Sleeping Bag','Thermal Jacket','Hand Warmers','Ice Axe'])[floor(random()*5 + 1)::int]
655 WHEN 'Navigation Equipment' THEN (ARRAY['GPS Device','Compass Pro','Topographic Map Kit','Altimeter','Navigation Watch'])[floor(random()*5 + 1)::int]
656 WHEN 'Hygiene Equipment' THEN (ARRAY['Camping Shower','Portable Toilet','Soap Kit','Wet Wipes Pack','Toothbrush Kit'])[floor(random()*5 + 1)::int]
657 WHEN 'Entertainment Equipment' THEN (ARRAY['Portable Speaker','Camping Cards','Board Game Set','Hammock','Projector Mini'])[floor(random()*5 + 1)::int]
658 WHEN 'Power Equipment' THEN (ARRAY['Solar Panel','Portable Generator','Power Bank','Battery Pack','Inverter'])[floor(random()*5 + 1)::int]
659 WHEN 'Repair Equipment' THEN (ARRAY['Multi-tool Kit','Repair Tape','Toolbox Set','Hammer Outdoor','Screwdriver Kit'])[floor(random()*5 + 1)::int]
660 WHEN 'Storage Equipment' THEN (ARRAY['Dry Bag','Storage Box','Backpack Organizer','Waterproof Container','Compression Sack'])[floor(random()*5 + 1)::int]
661 WHEN 'Medical Equipment' THEN (ARRAY['First Aid Advanced Kit','Bandage Set','Pain Relief Kit','Emergency Medical Kit','Thermometer Kit'])[floor(random()*5 + 1)::int]
662 WHEN 'Fire Equipment' THEN (ARRAY['Fire Starter Kit','Magnesium Rod','Waterproof Matches','Fire Steel','Emergency Torch'])[floor(random()*5 + 1)::int]
663 WHEN 'Hiking Equipment' THEN (ARRAY['Trekking Poles','Hiking Backpack','Trail Shoes','Rain Jacket','Hiking Map Kit'])[floor(random()*5 + 1)::int]
664 WHEN 'Cycling Equipment' THEN (ARRAY['Mountain Bike Helmet','Bike Repair Kit','Cycling Gloves','Bike Pump','Reflective Vest'])[floor(random()*5 + 1)::int]
665 WHEN 'Skiing Equipment' THEN (ARRAY['Ski Poles','Ski Helmet','Ski Goggles','Ski Boots','Ski Jacket'])[floor(random()*5 + 1)::int]
666 WHEN 'Cooking Accessories' THEN (ARRAY['Camping Spoon Set','Portable Knife Set','Cutting Board','Spice Kit','Cooking Utensil Set'])[floor(random()*5 + 1)::int]
667 WHEN 'Camping Tools' THEN (ARRAY['Hammer','Tent Peg Extractor','Camping Axe','Saw Folding','Multi-purpose Tool'])[floor(random()*5 + 1)::int]
668 WHEN 'Electronics Equipment' THEN (ARRAY['Camping Radio','Portable Charger','Walkie Talkie','LED Charger Lamp','Smartwatch Outdoor'])[floor(random()*5 + 1)::int]
669 WHEN 'Kids Equipment' THEN (ARRAY['Kids Sleeping Bag','Mini Backpack','Kids Chair','Outdoor Toy Set','Kids Lantern'])[floor(random()*5 + 1)::int]
670 WHEN 'Pet Equipment' THEN (ARRAY['Dog Leash Set','Pet Tent','Pet Food Container','Portable Water Bowl','Pet Harness'])[floor(random()*5 + 1)::int]
671 WHEN 'Emergency Equipment' THEN (ARRAY['Emergency Shelter','Survival Blanket','Rescue Whistle','Signal Mirror','Emergency Kit Pro'])[floor(random()*5 + 1)::int]
672 ELSE 'Generic Equipment'
673 END,
674 et_desc,
675 total_qty,
676 CASE WHEN avail_qty > total_qty THEN total_qty ELSE avail_qty END,
677 (floor(random() * 16) + 1)::int * 5,
678 (floor(random() * 25))::int * 10,
679 (ARRAY['new','good','fair','poor','under_repair'])[floor(random()*5 + 1)::int],
680 CASE WHEN avail_qty = 0 THEN 'no' ELSE CASE WHEN random() < 0.75 THEN 'yes' ELSE 'no' END END
681FROM base;
682select * from equipment;
683
684
685
686--20. Reservation --------------------------------------------
687DROP TABLE IF EXISTS temp_reservation_data;
688
689CREATE TEMP TABLE temp_reservation_data AS
690WITH guest_array AS (
691 SELECT array_agg(user_id ORDER BY random()) AS guests
692 FROM "User"
693 WHERE type = 'guest'
694),
695promo_array AS (
696 SELECT array_agg(promotionId ORDER BY random()) AS promos
697 FROM Promotion
698),
699camp_array AS (
700 SELECT array_agg(campLocationId ORDER BY random()) AS camps
701 FROM CampLocation
702)
703SELECT
704 gs.n,
705 ga.guests[((gs.n - 1) % array_length(ga.guests, 1)) + 1] AS guest_id,
706 pa.promos[((gs.n - 1) % array_length(pa.promos, 1)) + 1] AS promo_id,
707 ca.camps[((gs.n - 1) % array_length(ca.camps, 1)) + 1] AS camp_id,
708 CURRENT_DATE - ((gs.n % 730) + 1) AS created_at
709FROM generate_series(1, 5000000) gs(n)
710CROSS JOIN guest_array ga
711CROSS JOIN promo_array pa
712CROSS JOIN camp_array ca;
713
714INSERT INTO Reservation (
715 GuestUseruserId,
716 PromotionpromotionId,
717 CampLocationcamplocationId,
718 number_of_guests,
719 reservation_status,
720 check_in_date,
721 check_out_date,
722 created_at,
723 total_price
724)
725WITH numbered AS (
726 SELECT
727 trd.*,
728 ROW_NUMBER() OVER (PARTITION BY camp_id ORDER BY n) AS rn
729 FROM temp_reservation_data trd
730),
731dates AS (
732 SELECT
733 n.*,
734 cl.opening_date,
735 cl.opening_date + (rn * 5) * INTERVAL '1 day' AS check_in_ts,
736 cl.opening_date
737 + (rn * 5) * INTERVAL '1 day'
738 + (2 + (rn % 3)) * INTERVAL '1 day' AS check_out_ts,
739 cl.closing_date
740 FROM numbered n
741 JOIN CampLocation cl ON cl.campLocationId = n.camp_id
742)
743SELECT
744 d.guest_id,
745 CASE WHEN d.n % 3 = 0 THEN d.promo_id ELSE NULL END,
746 d.camp_id,
747 (1 + (d.n % 5))::int,
748 CASE
749 WHEN d.check_in_ts::date > CURRENT_DATE THEN
750 (ARRAY['pending','confirmed'])[(d.n % 2) + 1]
751 ELSE
752 (ARRAY['pending','confirmed','cancelled','completed'])[(d.n % 4) + 1]
753 END,
754 d.check_in_ts::date,
755 d.check_out_ts::date,
756 LEAST(d.created_at, d.check_in_ts::date - 1),
757 (50 + (d.n % 500))::numeric(12,2)
758FROM dates d
759WHERE d.check_out_ts <= d.closing_date;
760
761UPDATE Reservation
762SET reservation_status = 'cancelled'
763WHERE reservationId % 7 = 0;
764
765--21. Payment ---------------------------------------
766INSERT INTO Payment (
767 ReservationreservationId,
768 amount,
769 payment_method,
770 payment_date,
771 transaction_reference,
772 payment_status
773)
774WITH res_array AS (
775 SELECT
776 array_agg(reservationId ORDER BY random()) AS ids,
777 array_agg(check_in_date ORDER BY random()) AS dates,
778 array_length(array_agg(reservationId), 1) AS cnt
779 FROM Reservation
780)
781SELECT
782 ids[1 + (gs.i % cnt)],
783 (50 + (abs(hashint4(gs.i * 7)) % 450))::numeric(12,2),
784 (ARRAY['online', 'cash'])[(abs(hashint4(gs.i * 3)) % 2) + 1],
785 LEAST(
786 CURRENT_DATE,
787 CURRENT_DATE - (abs(hashint4(gs.i * 11)) % 365)
788 ),
789 'TXN-' || upper(substr(md5(gs.i::text), 1, 10)),
790 (ARRAY['pending','completed','failed','refunded','partially_refunded'])[(abs(hashint4(gs.i * 13)) % 5) + 1]
791FROM generate_series(1, 15000000) gs(i)
792CROSS JOIN res_array;
793select * from Payment limit(100);
794
795--22.ReservationSttaus-------------------------------
796INSERT INTO ReservationStatus (
797 ReservationreservationId,
798 refund_amount,
799 cancellation_date
800)
801SELECT
802 r.reservationId,
803
804 CASE
805 WHEN r.reservation_status = 'cancelled'
806 THEN round((r.total_price * random())::numeric, 2)
807 ELSE 0
808 END,
809
810 CASE
811 WHEN r.reservation_status = 'cancelled'
812 THEN LEAST(
813 r.created_at + (floor(random() * 5))::int,
814 CURRENT_DATE
815 )
816 ELSE NULL
817 END
818
819FROM Reservation r;
820
821SELECT COUNT(*) FROM Reservation WHERE reservation_status = 'cancelled';
822select distinct count(cancellation_date) from reservationstatus;
823SELECT * FROM ReservationStatus WHERE cancellation_date IS NOT NULL LIMIT 100;
824
825--23. ReservationActivity------------------------------------
826WITH
827res AS (
828 SELECT array_agg(reservationId) AS arr, count(*) AS cnt
829 FROM Reservation
830),
831act AS (
832 SELECT array_agg(activity_id) AS arr, count(*) AS cnt
833 FROM Activity
834)
835
836INSERT INTO ReservationActivity (
837 Activityactivity_id,
838 ReservationreservationId,
839 number_of_participants,
840 total_price_for_activity,
841 status,
842 booking_date
843)
844SELECT
845 act.arr[1 + (gs.i % act.cnt)],
846 res.arr[1 + (gs.i % res.cnt)],
847
848 (1 + floor(random() * 5))::int,
849 round((10 + random() * 200)::numeric, 2),
850
851 (ARRAY['booked','completed','cancelled'])
852 [1 + floor(random() * 3)],
853
854 CURRENT_DATE - (floor(random() * 365))::int
855
856FROM generate_series(1, 15000000) gs(i)
857CROSS JOIN act
858CROSS JOIN res;
859
860select * from reservationactivity;
861
862--24. EquipmentRental--------------------------
863INSERT INTO EquipmentRental (
864 EquipmentequipmentId,
865 ReservationreservationId,
866 quantity,
867 start_date,
868 end_date,
869 total_price,
870 deposit_paid,
871 status
872)
873WITH
874eq AS (
875 SELECT array_agg(equipmentId) AS arr, count(*) AS cnt
876 FROM Equipment
877),
878res AS (
879 SELECT array_agg(reservationId) AS arr, count(*) AS cnt
880 FROM Reservation
881)
882SELECT
883 eq.arr[1 + (gs.i % eq.cnt::int)],
884 res.arr[1 + (gs.i % res.cnt::int)],
885 (1 + abs(hashint4(gs.i * 3)) % 5)::int,
886 CURRENT_DATE - (abs(hashint4(gs.i * 7)) % 365),
887 CURRENT_DATE - (abs(hashint4(gs.i * 7)) % 365) + (1 + abs(hashint4(gs.i * 11)) % 14),
888 (10 + abs(hashint4(gs.i * 13)) % 200)::numeric,
889 (abs(hashint4(gs.i * 17)) % 100)::numeric,
890 (ARRAY['active','returned','overdue','cancelled','damaged'])[(abs(hashint4(gs.i * 19)) % 5) + 1]
891FROM generate_series(1, 5000000) gs(i)
892CROSS JOIN eq
893CROSS JOIN res;
894
895--25.review---------------------------
896INSERT INTO Review (
897 CampLocationcampLocationId,
898 GuestUseruserId,
899 review_date,
900 comment,
901 rating
902)
903WITH guest_array AS (
904 SELECT array_agg(UseruserId) AS ids, count(*) AS cnt
905 FROM Guest
906),
907camp_array AS (
908 SELECT array_agg(campLocationId) AS ids, count(*) AS cnt
909 FROM CampLocation
910)
911SELECT
912 ca.ids[1 + (gs.i % ca.cnt::int)],
913 ga.ids[1 + (gs.i % ga.cnt::int)],
914 CURRENT_DATE - (abs(hashint4(gs.i * 7)) % 365),
915 CASE (abs(hashint4(gs.i * 3)) % 5) + 1
916 WHEN 5 THEN 'Amazing place! Everything was perfect'
917 WHEN 4 THEN 'Very nice location, enjoyed the stay'
918 WHEN 3 THEN 'Average experience'
919 WHEN 2 THEN 'Could be better'
920 WHEN 1 THEN 'Very disappointing stay'
921 END,
922 (abs(hashint4(gs.i * 3)) % 5) + 1
923FROM generate_series(1, 100000) gs(i)
924CROSS JOIN guest_array ga
925CROSS JOIN camp_array ca;
926
927
928
929--26. ActivityReview--------------------------
930INSERT INTO ActivityReview (
931 GuestUseruserId,
932 Activityactivity_id,
933 rating,
934 comment,
935 review_date
936)
937WITH guest_array AS (
938 SELECT array_agg(UseruserId) AS ids, count(*) AS cnt
939 FROM Guest
940),
941activity_array AS (
942 SELECT array_agg(activity_id) AS ids, count(*) AS cnt
943 FROM Activity
944)
945SELECT
946 ga.ids[1 + (gs.i % ga.cnt::int)],
947 aa.ids[1 + (gs.i % aa.cnt::int)],
948 (abs(hashint4(gs.i * 3)) % 5) + 1,
949 CASE (abs(hashint4(gs.i * 3)) % 5) + 1
950 WHEN 5 THEN 'Amazing experience! Highly recommended!'
951 WHEN 4 THEN 'Very good activity, would do again'
952 WHEN 3 THEN 'It was okay, nothing special'
953 WHEN 2 THEN 'Not very satisfied'
954 WHEN 1 THEN 'Terrible experience'
955 END,
956 CURRENT_DATE - (abs(hashint4(gs.i * 7)) % 365)
957FROM generate_series(1, 200000) gs(i)
958CROSS JOIN guest_array ga
959CROSS JOIN activity_array aa;
960
961
962--27. Message------------------------------
963INSERT INTO "Message" (
964 UseruserId,
965 senderId,
966 receiverId,
967 content,
968 sent_time
969)
970WITH u AS (
971 SELECT array_agg(user_id) AS arr, count(*) AS cnt
972 FROM "User"
973)
974SELECT
975 s.id,
976 s.id,
977 r.id,
978 'M-' || gs.i,
979 (now() - (abs(hashint4(gs.i * 7)) % 86400) * interval '1 second')::time
980FROM generate_series(1, 10000000) gs(i)
981CROSS JOIN u
982JOIN LATERAL (SELECT u.arr[1 + (gs.i % u.cnt::int)] AS id) s ON true
983JOIN LATERAL (SELECT u.arr[1 + ((gs.i + 7) % u.cnt::int)] AS id) r ON true
984WHERE s.id <> r.id;
985
986
987--28.user_message_receiver-----------------------------------
988INSERT INTO user_message_receiver (UseruserId, MessagemessageId)
989WITH user_array AS (
990 SELECT array_agg(user_id) AS ids, count(*) AS cnt
991 FROM "User"
992)
993SELECT
994 ids[1 + (m.messageId % cnt::int)],
995 m.messageId
996FROM "Message" m
997CROSS JOIN user_array;
998
999SELECT conname, contype
1000FROM pg_constraint
1001WHERE conrelid = 'payment'::regclass;