You are on page 1of 7

To do the exercises use LANGUAGE plpgsql.

0.- Download the files:


● instr2.sql
● EMPLOYEESDB.sql

1.- Create a new database ‘exercises’ with a schema ‘p08’ (all owned by user alumne)
and import instr2.sql and EMPLOYEESDB.sql inside that schema. Set the appropiate
permissions.
Create database exercises;
\c exercises
create schema p08;
\li C:/Users/Ignacio/Desktop/DataBase/instr2.sql
\li C:/Users/Ignacio/Desktop/DataBase/EMPLOYEESDB.sql
2.- Connect to the database as ‘alumne’ and to the rest of exercise using this
connection.
pgcli -h 192.168.62.25 -p 5432 -U alumne exercises
Alter database exercises owner to alumne;
alter schema p08 owner to alumne;
3.- Run the following query (first read the source code of the function):
select INSTR2(E.surname, 'A') from EMPLOYEES E;

4.- Create a function ‘myadd’ that receives two integers and returns the result of their
addition.
create or replace function myadd(var1 integer,var2 integer) returns integer as $$
begin
return var1+var2;
end;
$$ language plpgsql;
5.- Create a function ‘add_one’ that receives an integer and returns that number
incremented by one.

create or replace function add_one(var1 integer) returns integer as $$


begin
return var1+1;
end;
$$ language plpgsql;
6.- Create a function ‘myconcat’ that receives two text parameters and returns the
result of its union.

create or replace function myconcat(txt1 text, txt2 text) returns text as $$


begin
return concat(txt1,txt2);
end;
$$ language plpgsql;
7.- Create 3 new functions MYTRIM, MYRTRIM, and MYLTRIM implementing the
corresponding Postgres functions described here. Consider only a single string
parameter. Obviously, to check if your functions work you must insert data with
whitespaces. Finally, you can only use to implement the functions LENGTH and
SUBSTR.
create or replace function mytrim(var1 text, var2 text, var3 text) returns text as $$
begin
return concat($1,trim($2),$3);
end;
$$ language plpgsql;

create or replace function myrtrim(var1 text, var2 text, var3 text) returns text as $$
begin
return concat($1,rtrim($2),$3);
end;
$$ language plpgsql;

create or replace function myltrim(var1 text, var2 text, var3 text) returns text as $$
begin
return concat($1,ltrim($2),$3);
end;
$$ language plpgsql;

8.- Implement the function CONCAT2 using the operator ||. Remember that this
function can accept a variable number of arguments. It’s mandatory to use FOREACH.
Clues:
● http://www.postgresqltutorial.com/plpgsql-function-parameters/
● https://www.postgresql.org/docs/current/plpgsql-control-structures.html
9.- Write a function ‘mysign’ to check whether a number is positive, negative or zero.

create or replace function mysign(var1 integer) returns text as $$


begin
if var1 < 0 then
return '-';
elseif var1 > 0 then
return '+';
else
return '0';
end if;
end;
$$ language plpgsql;
10.- Write a function ‘myevenodd’ to check whether a number is even or odd.

create or replace function myevenodd(var1 integer) returns text as $$


begin
if var1%2 = 0 then
return 'even';
else
return 'odd';
end if;
end;
$$ language plpgsql;
11.- Write a function ‘ischarordigit’ to check whether a given character is letter or
digit. Use overload.
create or replace function ischarordigit(var1 char) returns text as $$
begin
if (var1 >='A' and var1<='Z')or(var1 >='a' and var1<='z') then
return 'char';
else
return 'digit';
end if;
end;
$$ language plpgsql;
12.- Write a program in PL/SQL to check whether a number is prime or not (use a for
loop).

13.- Write a factorial function. Make a recursive version and an iterative version.

CREATE OR REPLACE FUNCTION factorial_it (var1 numeric)


RETURNS numeric AS $$
DECLARE
tmp numeric; result numeric;
BEGIN
result := 1;
FOR tmp IN 1 .. var1 LOOP
result := result * tmp;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
------------------
CREATE OR REPLACE FUNCTION factorial_rec (var1 numeric)
RETURNS numeric AS $$
BEGIN
IF var1 = 0 THEN
RETURN 1;
ELSIF var1 = 1 THEN
RETURN 1;
ELSE
RETURN var1 * factorial(var1 - 1);
END IF;
END;
$$ LANGUAGE plpgsql;
14.- Write a function to check if the salaries of the employees are good or bad. Return
the number of employees with good salary. Check RAISE NOTICE here:
https://www.postgresql.org/docs/current/plpgsql-errors-and-messages.html

15.- List all the functions that you have created.

En SQL Shell
\c exercises
\df p08.
16.- Delete all the functions you created in this list.

drop function p08.instr2(varchar, char);

drop function p08.filteremployee(inout integer, out text, out text, out integer);

drop function p08.numrows(text);

drop function p08.myadd(integer, integer);

drop function p08.add_one(integer);

drop function p08.myconcat(text, text);

drop function p08.mytrim(text, text, text);

drop function p08.myrtrim(text, text, text);

drop function p08.myltrim(text, text, text);

You might also like