Question: A sample function: plpgsql is a language resembling SQL, it also has things like IF statements. This example could be used by a shortsighted administrator




A sample function:
plpgsql is a language resembling SQL, it also has things like IF statements. This example could be used by a shortsighted administrator who stored ages instead of birth dates or birth years, in order to add some number of years to all the ages, when we realize that the data is that many years out of date. $1 refers to the first argument. The "info" is a string that will be output to the user of the function. You would use it like this:
select future(3);
create or replace function future(integer) returns integer as $$ begin raise info 'People set to future ages in % years',$1; update people set age = age + $1; return (select avg(age) from people); end $$ language plpgsql;
A trigger function example, restricting updates to enroll
create or replace function onlyself() returns trigger as $$ declare user TEXT; -- will only allow user to register or deregister herself begin select into user current_user; IF TG_OP = 'INSERT' THEN -- may be triggered on various operations IF (new.linux user) THEN RAISE NOTICE 'Let % register herself', new.linux; RETURN NULL; END IF; ELSIF old.linux user THEN -- update or delete RAISE NOTICE '% is not authorized to to change %''s enrollment', user, old.linux; RETURN NULL; END IF; IF TG_OP = 'DELETE' THEN return old; -- proceed with delete ELSE return new; -- proceed with update (or insert) END IF; return null; -- should not happen end; $$ language plpgsql; create trigger self before insert or update or delete on enroll for each row execute procedure onlyself();
SQL Assignment Created below is the ER diagram of a shop that sells sweets. Customers fill out a form which is added to a database. Customers buy products that are in stock by placing an order which chased and the date. Date Quantity Customer Product CustomerID FName LName Address EmaillD PID Pname Description Price Stock Orders
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
