-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql.sql
More file actions
884 lines (841 loc) · 23.7 KB
/
Copy pathpgsql.sql
File metadata and controls
884 lines (841 loc) · 23.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
-- how to concatenate
-- select concat(first_name, ' ', last_name) as Name, phone, state
-- from customer
-- where state = 'TX'
-- get the toal value of all business shoes that are in our inventory
-- select product_id, sum(price) as Total
-- from item
-- where product_id = 1
-- group by product_id;
-- get a list of states we have customers in
-- select distinct state
-- from customer
-- order by state;
-- find all states where we have customers excluding CA
-- select distinct state
-- from customer
-- where state != 'CA';
-- just list CA and NJ
-- select distinct state
-- from customer
-- where state in ('CA', 'NJ');
-- find the number of customers in CA and NJ
-- select count(state) as state_count, state
-- from customer
-- where state in ('CA', 'NJ')
-- group by state;
-- inner join
-- list items that are ordered
-- use join condition to find IDs that are equal in the tables
-- select item_id, price
-- from item inner join sales_item
-- on item.id = sales_item.id and price > 120.00
-- order by item_id;
-- join 3 tables: the quantity and the total sales
-- sales_items.sales_order_id matches up with sales_order.id, item.id matches up with sales_item.id
-- use tables sales_order, sales_item, item
-- select sales_order.id, sales_item.quantity, item.price,
-- (sales_item.quantity * item.price) as total
-- from sales_order
-- join sales_item
-- on sales_order.id = sales_item.sales_order_id
-- join item
-- on sales_item.item_id = item.id
-- order by sales_order.id;
-- select item_id, price
-- from item, sales_item
-- where item.id = sales_item.item_id
-- and price > 120.00
-- order by item_id
-- outer join
-- return all of the rows from one of the tables being joined even if NO MATCHES are found
-- left outer join: return all rows from the table being joined on the left
-- select name, supplier, price
-- from product
-- left join item
-- on product.id = item.product_id
-- order by name;
-- cross join: include data from each row in both tables
-- select sales_order_id, quantity, product_id
-- from item
-- cross join sales_item
-- order by sales_order_id;
-- unions
-- combine results of two or more select statements into 1 result
-- each result MUST return the same number of columns + each column MUST have the same data type
-- eg:send birthday cards to all of customers and sales people for the month of December
-- select first_name, last_name, street, city, zip, birth_date
-- from customer
-- where extract(month from birth_date) = 12
-- union
-- select first_name, last_name, street, city, zip, birth_date
-- from sales_person
-- where extract(month from birth_date) = 12
-- order by birth_date
-- select product_id, price
-- from item
-- where price = NULL
-- where price is not NULL
-- string match
-- 1. search any customer whose name begins with M
-- select concat(first_name, ' ', last_name)
-- from customer
-- where first_name similar to 'M%';
-- %:match 0 or more chars
-- 2. match someone whose name begins with A and have 5 cahrs after that
-- select concat(first_name, ' ', last_name)
-- from customer
-- where first_name like 'A_____';
-- _: placeholder
-- 3. match people whose first name begins with D or last name ends with N
-- select concat(first_name, ' ', last_name)
-- from customer
-- where first_name similar to 'D%' or last_name similar to '%n';
-- 4. first name that starts with Ma
-- select concat(first_name, ' ', last_name)
-- from customer
-- where first_name ~ '^Ma';
-- 5. last name that ends with ez
-- select concat(first_name, ' ', last_name)
-- from customer
-- where last_name ~ 'ez$|son$';
-- 6. last name that contains x, y or z
-- select concat(first_name, ' ', last_name)
-- from customer
-- where last_name ~ '[w-z]';
-- group by
-- how results are grouped
--
-- find how many customers have birthdays in certain months
-- select extract(month from birth_date) as month, count(*) as amount
-- from customer
-- group by month
-- order by month;
-- having
-- narrow the results based off of a condition
-- get months if more than 1 person have birthday that month
-- select extract(month from birth_date) as month, count(*) as amount
-- from customer
-- group by month
-- having count(*) > 1
-- order by month;
-- aggregate funcs
-- 1. get sum price on item
-- select sum(price)
-- from item;
-- 2.more funcs
-- select count(*) as items, sum(price) as value, round(avg(price), 2) as avg,
-- min(price) as min, max(price) as max
-- from item;
-- views
-- select statemtns that's result is stored in database
-- 1. create a view that contains our main purchase order info
-- create view purchase_order_overview as
-- select sales_order.purchase_order_number, customer.company,
-- sales_item.quantity, product.supplier, product.name, item.price,
-- (sales_item.quantity * item.price) as total,
-- concat(sales_person.first_name, ' ', sales_person.last_name) as salesperson
-- from sales_order
-- join sales_item on sales_item.sales_order_id = sales_order.id
-- join item on item.id = sales_item.item_id
-- join product on product.id = item.product_id
-- join customer on customer.id = sales_order.cust_id
-- join sales_person on sales_person.id = sales_order.sales_person_id
-- order by purchase_order_number;
--
-- select * from purchase_order_overview;
-- create view purchase_order_overview_2 as
-- select sales_order.purchase_order_number, customer.company,
-- sales_item.quantity, product.supplier, product.name, item.price,
-- concat(sales_person.first_name, ' ', sales_person.last_name) as salesperson
-- from sales_order
-- join sales_item on sales_item.sales_order_id = sales_order.id
-- join item on item.id = sales_item.item_id
-- join product on product.id = item.product_id
-- join customer on customer.id = sales_order.cust_id
-- join sales_person on sales_person.id = sales_order.sales_person_id
-- order by purchase_order_number;
-- select *, (quantity * price) as total from purchase_order_overview_2;
-- drop view purchase_order_overview_2;
-- SQL funcs
-- create or replace func function_name() returns void as 'SQL commands'
-- $1 the very first value inside of
-- create or replace function fn_add_ints(int ,int)
-- returns int as
-- '
-- select $1 + $2;;
-- '
-- language sql
--
-- select fn_add_ints(2, 3);
--
-- create or replace function fn_add_ints(int, int)
-- returns int as
-- $body$
-- select $1 + $2
-- $body$
-- language sql;
-- select fn_add_ints(2, 14);
--
-- void
-- check if a salesperson's state is assigned, if not assign 'PA' to state column
-- create or replace function fn_update_employee_state()
-- returns void as
-- $body$
-- update sales_person
-- set state = 'PA'
-- where state is null
-- $body$
-- language sql;
-- select fn_update_employee_state();
--
-- get max price in item
-- create or replace function fn_max_product_price()
-- returns numeric as
-- $body$
-- select max(price)
-- from item
-- $body$
-- language sql;
-- select fn_max_product_price();
--
-- get the total inventory value
-- create or replace function fn_get_value_inventory()
-- returns numeric as
-- $body$
-- select sum(price)
-- from item;
-- $body$
-- language sql;
-- select fn_get_value_inventory();
--
-- get the total num of customers
-- create or replace function fn_get_number_customers()
-- returns numeric as
-- $body$
-- select count(*)
-- from customer;
-- $body$
-- language sql;
-- select fn_get_number_customers();
--
-- get the number of customers who do not have a phone number
-- create or replace function fn_get_customers_no_phone()
-- returns numeric as
-- $body$
-- select count(*)
-- from customer
-- where phone is NULL;
-- $body$
-- language sql;
-- select fn_get_customers_no_phone();
--
-- get the number of customers from Texas and use the name parameter as "Texas"
-- create or replace function fn_get_number_customers_from_state(state_name char(2))
-- returns numeric as
-- $body$
-- select count(*)
-- from customer
-- where state = state_name;
-- $body$
-- language sql;
-- select fn_get_number_customers_from_state('TX');
--
--
-- select count(*)
-- from sales_order
-- natural join customer
-- where customer.first_name = 'Christopher' AND customer.last_name = 'Jones';
--
-- create or replace function fn_get_number_orders_from_customer(cus_fname varchar, cus_lname varchar)
-- returns numeric as
-- $body$
-- select count(*)
-- from sales_order
-- natural join customer
-- -- on customer.id = sales_order.cust_id
-- where customer.first_name = cus_fname and customer.last_name = cus_lname
-- $body$
-- language sql;
-- select fn_get_number_orders_from_customer('Christopher', 'Jones');
--
-- composite
-- return a row
-- /*get the latest order*/
-- create or replace function fn_get_last_order()
-- returns sales_order as
-- $body$
-- select *
-- from sales_order
-- order by time_order_taken desc
-- limit 1;
-- $body$
-- language sql;
-- select fn_get_last_order();
-- select (fn_get_last_order()).*; -- make the result in a table format
--
-- time_stamp: 2:03:40 https://youtu.be/85pG_pDkITY?si=huZNTtkVBamHmmF6
--
-- /*get all rows of every employee inside California*/
-- create or replace function fn_get_employees_location(loc varchar)
-- returns setof sales_person as -- setof: returns rows of results
-- $body$
-- select *
-- from sales_person
-- where state = loc;
-- $body$
-- language sql;
-- select (fn_get_employees_location('CA')).*; /* return results in table format */
--
-- /* select specific columns from results of a function */
-- select first_name, last_name, phone
-- from fn_get_employees_location('CA');
-- /* pl/pgsql SQL funtion */
--
-- /* get product price by name */
-- create or replace function func_get_price_product_name(prod_name varchar)
-- returns numeric(6,2) as
-- $body$
-- begin
-- return price
-- from item
-- join product
-- on item.product_id = product.id
-- where product.name = prod_name
-- limit 1;
-- end
-- $body$
-- language plpgsql;
-- select func_get_price_product_name('Grandview');
--
-- declare a variable
-- := assign
-- create or replace function fn_get_sum(val1 int, val2 int)
-- returns int as
-- $body$
-- declare
-- ans int;
-- begin
-- ans := val1 + val2 ;
-- return ans;
-- end
-- $body$
-- language plpgsql;
-- select fn_get_sum(1, 2);
--
-- declare a varaible with a query
-- create or replace function fn_get_random_number(min_val int, max_val int)
-- returns int as
-- $body$
-- declare
-- rand int;
-- begin
-- select random()*(max_val - min_val) + min_val into rand;
-- return rand;
-- end
-- $body$
-- language plpgsql;
-- select fn_get_random_number(1, 2);
--
-- get a random sales person's name
-- record type: store data
-- create or replace function fn_get_random_salesperson()
-- returns varchar as
-- $$
-- declare
-- rand int;
-- emp record;
-- begin
-- select random()*(5-1)+1 into rand;
-- select *
-- from sales_person
-- into emp
-- where id = rand;
-- return concat(emp.first_name, ' ', emp.last_name);
-- end
-- $$
-- language plpgsql;
-- select fn_get_random_salesperson();
--
-- IN INOUT and OUT: automatically return OUT. no need to decalre variable.
-- create or replace function fn_get_sum_2(in v1 int, in v2 int, out ans int)
-- as
-- $$
-- begin
-- ans := v1 + v2;
-- end;
-- $$
-- language plpgsql;
-- select fn_get_sum_2(2, 3);
--
-- create or replace function fn_get_cust_birthday(in the_month int,
-- out bd_month int, out bd_day int,
-- out f_name varchar, out l_name varchar)
-- as
-- $$
-- begin
-- -- EXTRACT() function extracts a part from a given date
-- select extract(month from birth_date), extract(day from birth_date),
-- first_name, last_name
-- into bd_month, bd_day, f_name, l_name
-- from customer
-- where extract(month from birth_date) = the_month
-- limit 1;
-- end;
-- $$
-- language plpgsql;
-- select fn_get_cust_birthday(12);
--
-- 2:25:56 Return Query Results
-- create or replace function fn_get_sales_people()
-- returns setof sales_person as
-- -- type of: an SQL function can be declared to return a set (that is, multiple rows) by specifying the function's return type as SETOF sometype, or equivalently by declaring it as RETURNS TABLE(columns). In this case all rows of the last query's result are returned.
-- -- https://stackoverflow.com/questions/22423958/sql-function-return-type-table-vs-setof-records
-- $$
-- begin
-- return query
-- select * from sales_person;
-- end;
-- $$
-- language plpgsql;
-- select fn_get_sales_people(); -- the output doesn't have field, not in table format
-- select (fn_get_sales_people()).*; -- in table format
-- select (fn_get_sales_people()).street; -- get the result of a specific column
--
-- return specific data from a query using multiple tables
-- get top 10 most expensive products
-- create or replace function fn_get_10_expensive_products()
-- returns table (
-- name varchar,
-- supplier varchar,
-- price numeric
-- )
-- as
-- $$
-- begin
-- return query
-- select product.name, product.supplier, item.price
-- from item
-- natural join product
-- order by item.price desc
-- limit 10;
-- end;
-- $$
-- language plpgsql;
-- select (fn_get_10_expensive_products()).*;
--
-- if else
-- create or replace function fn_check_month_orders(the_month int)
-- returns varchar as
-- $$
-- declare
-- total_orders int;
-- begin
-- select count(purchase_order_number)
-- into total_orders
-- from sales_order
-- where extract(month from time_order_taken) = the_month;
-- if total_orders > 5 then
-- return concat(total_orders, ' orders: doing good');
-- elseif total_orders < 5 then
-- return concat(total_orders, ' orders: doing bad');
-- else
-- return concat(total_orders, ' orders: on target');
-- end if; --anytime you put an if inside here you have to end the if condition block with an endif
-- end;
-- $$
-- language plpgsql;
-- select fn_check_month_orders(12);
--
-- -- case statement
-- create or replace function fn_check_month_orders(the_month int)
-- returns varchar as
-- $$
-- declare
-- total_orders int;
-- begin
-- select count(purchase_order_number)
-- into total_orders
-- from sales_order
-- where extract(month from time_order_taken) = the_month;
-- case
-- when total_orders < 1 then
-- return concat(total_orders, ' orders: terrible');
-- when total_orders > 1 and total_orders < 5 then
-- return concat(total_orders, ' orders: on target');
-- else
-- return concat(total_orders, ' orders: doing good');
-- end case;
-- end;
-- $$
-- language plpgsql;
-- select fn_check_month_orders(12);
--
-- loop
-- create or replace function fn_loop_test(max_num int)
-- returns int as
-- $$
-- declare
-- j int default 1;
-- tot_sum int default 0;
-- begin
-- loop
-- tot_sum := tot_sum + j;
-- j := j + 1;
-- exit when j > max_num;
-- end loop;
-- return tot_sum;
-- end
-- $$
-- language plpgsql;
-- select fn_loop_test(3);
--
-- for loop
/*
for counter_name in start value .. end_value by stepping -- stepping: how much you add to start_value as you cycle through your looping
loop
statement
end loop;
*/
-- create or replace function fn_for_test(max_num int)
-- returns int as
-- $$
-- declare
-- tot_sum int default 0;
-- begin
-- for i in 1 .. max_num by 2
-- loop
-- tot_sum := tot_sum + i;
-- end loop;
-- return tot_sum;
-- end
-- $$
-- language plpgsql;
-- select fn_for_test(5)
--
-- reverse loop
-- create or replace function fn_for_test_reverse(max_num int)
-- returns int as
-- $$
-- declare
-- tot_sum int default 0;
-- begin
-- for i in reverse max_num .. 1 by 2
-- loop
-- tot_sum := tot_sum + i;
-- end loop;
-- return tot_sum;
-- end
-- $$
-- language plpgsql;
-- select fn_for_test_reverse(5)
--
-- do block
-- do
-- $$
-- --outout all of my sales people's name using for loop
-- declare
-- rec record;
-- begin
-- for rec in
-- select first_name, last_name
-- from sales_person
-- limit 5
-- loop
-- raise notice '% %', rec.first_name, rec.last_name;
-- end loop;
-- end
-- $$
-- language plpgsql;
--
-- for each var in array
-- do
-- $$
-- declare
-- arr1 int[] := array[1,2,3];
-- i int;
-- begin
-- foreach i in array arr1
-- loop
-- raise notice '%', i;
-- end loop;
-- end;
-- $$
-- language plpgsql;
--
-- while loop
-- do
-- $$
-- declare
-- j int default 1;
-- tot_sum int default 0;
-- begin
-- while j <= 10
-- loop
-- tot_sum := tot_sum + j;
-- j := j + 1;
-- end loop;
-- raise notice '%', tot_sum;
-- end;
-- $$
-- language plpgsql
--
-- continue
-- do
-- $$
-- declare
-- i int default 1;
-- begin
-- loop
-- i := i + 1;
-- exit when i > 10;
-- continue when mod(i, 2) = 0;
-- raise notice 'Num : %', i;
-- end loop;
-- end;
-- $$
-- language plpgsql
--
-- return inventory value by providing a supplier name
-- create or replace function fn_get_supplier_value(the_supplier varchar)
-- returns varchar as
-- $$
-- declare
-- supplier_name varchar;
-- price_sum numeric;
-- begin
-- select product.supplier, sum(item.price)
-- into supplier_name, price_sum
-- from product, item
-- where product.supplier = the_supplier
-- group by product.supplier; -- paired with sum
-- return concat(supplier_name, ' inventory value : $', price_sum);
-- end;
-- $$
-- language plpgsql;
-- select fn_get_supplier_value('Nike');
--
/*
3:01:43 store procedures
stored procedures:
1. can be exucted by an application that has access to your database
2. can also excute transactions which you can't do with functions
3. can't return values but there is a workaround using INOUT
4. can't be called by select, using EXECUTE or CALL instead // we are using parameteres whenever we use excute command
5. if a stored procedures doesn't have parameters, it's called a static procedure; those with parameters are called dynamic procedures
*/
-- create a sample table that is going to store customer IDs with balances due
-- CREATE TABLE past_due(
-- id serial primary key,
-- cust_id integer not null,
-- balance numeric(6,2) not null
-- )
-- insert into past_due(cust_id, balance)
-- values
-- (1, 123.45),
-- (2, 324.50);
-- create or replace procedure pr_debt_paid(
-- past_due_id int,
-- payment numeric
-- )
-- as
-- $$
-- declare
-- begin
-- update past_due
-- set balance = balance - payment
-- where id = past_due_id;
-- commit; -- use commit to run this update
-- end;
-- $$
-- language plpgsql;
-- call pr_debt_paid(1, 10.00);
-- select * from past_due;
-- use INOUT to return value
-- create or replace procedure pr_debt_paid(
-- past_due_id int,
-- payment numeric,
-- INOUT msg varchar
-- ) as
-- $$
-- declare
-- begin
-- update past_due
-- set balance = balance - payment
-- where id = past_due_id;
-- commit;
-- end;
-- $$
-- language plpgsql;
-- call pr_debt_paid(1, 10.00);
-- select * from past_due;
/*
Triggers
features:
1. when you want an action to automatically occur when another event occurs, common events include using commands such as insert, update, delete, trucate
2. associated with tables, foerign tables and views
3. can execute before or after an event executes
4. can also execute instead of another event
5. can put multiple triggers on a table and they execute in alphabetical order
6. they can't be triggered manually by a user
6. don't receive parameters
7. if the trigger is row level, the trigger is called for each row that is modified; if a state level, the trigger is called once
pros:
1. can be used for auditing/logging, if something is deleted the trigger could save it in case it is needed later
2. validate data
3. make certain events always happen
4. ensure integrity between diff databases
5. call functions or procedures
6. trigger are recursive
cons:
1. add execution overhead
2. nested/recursive trigger errors can be hard to debug
3. invisible to the client which can cause confusion when actoons aren't allowed
*/
-- want log changes to a table that is called distributor
-- create table distributor(
-- id serial primary key,
-- name varchar(100)
-- )
-- insert into distributor (name) values
-- ('Parawholesale'),
-- ('J & B Sales'),
-- ('Steel City Clothing');
-- select * from distributor;
-- create another table and store changes to distributor
-- create table distributor_audit(
-- id serial primary key,
-- dist_id int not null,
-- name varchar(100) not null,
-- edit_date timestamp not null
-- );
-- -- create trigger func
-- create or replace function fn_log_dist_name_change()
-- returns trigger
-- language plpgsql
-- as
-- $$
-- begin
-- -- check if a name change has occurred
-- if new.name <> old.name then -- <>: not equal
-- insert into distributor_audit
-- (dist_id, name, edit_date)
-- values
-- (old.id, old.name, now());
-- end if;
-- -- trigger info variables
-- raise notice 'Trigger Name: %', tg_name;
-- raise notice 'Table Name: %', tg_table_name;
-- raise notice 'Operation Name: %', tg_op;
-- raise notice 'When Executed: %', tg_when;
-- raise notice 'Row or Statement: %', tg_level;
-- raise notice 'Table Schema: %', tg_table_schema;
-- return new;
-- end;
-- $$;
-- bind the trigger function above to a trigger
-- create trigger tr_dist_name_changed
-- before update on distributor
-- for each row
-- execute procedure fn_log_dist_name_change();
-- update distributor
-- set name = 'Western Clothing'
-- where id = 2;
-- select * from distributor_audit;
/* conditional triggers
revoke delete on tables for some users through the use of triggers
*/
-- -- set up oour system in a way that is not going to allow people to update the records on weekend
-- -- create a trigger func
-- create or replace function fn_block_weekend_changes()
-- returns trigger
-- language plpgsql
-- as
-- $$
-- begin
-- raise notice 'No database changes allowed on the weekend';
-- return null;
-- end;
-- $$;
-- -- create a triiger
-- create trigger tr_block_weekend_changes
-- before update or insert or delete or truncate
-- on distributor
-- for each statement
-- when(
-- extract('DOW' from current_timestamp) = 0 --DOW: day of the week, execute the trigger when it's sunday (0, not 7)
-- )
-- execute procedure fn_block_weekend_changes();
-- update distributor
-- set name = 'Western Clothing'
-- where id = 2;
-- drop event trigger tr_block_weekend_changes;
-- DROP TRIGGER IF EXISTS tr_block_weekend_changes ON distributor;
/*
cursor:
step backwards or forwards through rows of data, pointed at a row and select, update or delete
*/
-- Do
-- $$
-- declare
-- msg text default '';
-- rec_customer record;
-- cursor_customers cursor
-- for -- assign this cursor to for
-- select * from customer;
-- begin
-- open cursor_customers; -- open a cursor
-- loop
-- fetch cursor_customers into rec_customer;
-- exit when not found; --exit when there is no customers found
-- msg := msg || rec_customer.first_name || ' ' || rec_customer.last_name || ', ';
-- /*
-- 1. := assignment operator
-- 2. || concatenation operator
-- 3. repeating msg after ":=" can ensure the accumulation of customer names
-- */
-- end loop;
-- raise notice 'customers: % ', msg;
-- end;
-- $$;
--
-- -- using cursors with functions
create or replace function fn_get_cust_by_state(c_state varchar)
returns text
language plpgsql
as
$$
declare
cust_names text default '';
rec_customer record;
--CURSOR DECLARATION--
-- declare 'cur_cust_by_state' that selects rows from 'customer' table based on the specified state 'c_state'
cur_cust_by_state cursor (c_state varchar)
for
select first_name, last_name, state
from customer
where state = c_state;
--CURSOR DECLARATION--
begin
open cur_cust_by_state(c_state);
loop
fetch cur_cust_by_state into rec_customer; -- fetches rows into the rec_customer record
exit when not found;
cust_names := cust_names || rec_customer.first_name || ' ' || rec_customer.last_name || ', ';
end loop;
close cur_cust_by_state;
return cust_names;
end;
$$;
select fn_get_cust_by_state('CA');
/*
lessons learnt
common mistakes that can trigger errors:
1. forget ; after 'end' in function
2. forget ; after 'language plpgsql'
3. forget ; after '$'
4. forget ; after 'end'
5. forget 'end loop;'
6. highligh the wrong section of code when excute
7. using sum() without 'group by'
*/