CREATE TABLE DEPT(deptno VARCHAR(10) PRIMARY KEY,dept_name CHAR(20));

 INSERT INTO DEPT VALUES(101,'TECHNICAL');
 INSERT INTO DEPT VALUES(102,'HEALTH');
 INSERT INTO DEPT VALUES(103,'FINANCIAL');
 INSERT INTO DEPT VALUES(104,'MANAGEMENT');
 INSERT INTO DEPT VALUES(105,'HR');

SELECT * FROM DEPT;



 CREATE TABLE SALESMAN(sno INT PRIMARY KEY,deptno VARCHAR REFERENCES DEPT(deptno) ON UPDATE CASCADE ON DELETE CASCADE,s_name CHAR(20),start_year INT);
 INSERT INTO SALESMAN VALUES(201,101,'DIVYA',2022);
 INSERT INTO SALESMAN VALUES(202,102,'SHRADDHA',2022);
 INSERT INTO SALESMAN VALUES(203,103,'BHAKTI',2022);
 INSERT INTO SALESMAN VALUES(204,104,'ABC',2019);
 INSERT INTO SALESMAN VALUES(205,105,'XYZ',2018);
 
 SELECT * FROM SALESMAN;
 
 
 CREATE TABLE EXPENSE(eid INT PRIMARY KEY,amount MONEY);
 
 INSERT INTO EXPENSE VALUES(401,25000);
 INSERT INTO EXPENSE VALUES(402,45000);
INSERT INTO EXPENSE VALUES(403,40000);
 INSERT INTO EXPENSE VALUES(404,70000);
 INSERT INTO EXPENSE VALUES(405,30000);
 
 SELECT * FROM EXPENSE;
 
 CREATE TABLE TRIP(tno INT PRIMARY KEY,sno INT REFERENCES SALESMAN(sno) ON UPDATE CASCADE ON DELETE CASCADE,eid INT REFERENCES EXPENSE(eid) ON UPDATE CASCADE ON DELETE CASCADE,from_city CHAR(20),to_city CHAR(20),departure_date DATE,return DATE);
 
 
 INSERT INTO TRIP VALUES(301,201,401,'PUNE','MUMBAI','2022-09-19','2019-12-25');
 INSERT INTO TRIP VALUES(302,202,402,'PUNE','MAHABALESHWAR','2022-10-29','2022-11-5');
 INSERT INTO TRIP VALUES(303,203,403,'PUNE','OOTY','2022-11-6','2022-11-15');
INSERT INTO TRIP VALUES(304,204,404,'MUMBAI','PUNE','2019-12-25','2020-11-25');
INSERT INTO TRIP VALUES(305,205,405,'LONAVALA','PUNE','2023-11-6','2023-11-15');

SELECT * FROM TRIP;


 CREATE OR REPLACE FUNCTION Q1() RETURNS VOID AS' DECLARE tn INT; fcity VARCHAR(20); tcity VARCHAR(20); BEGIN SELECT TRIP.tno, from_city, to_city INTO tn, fcity, tcity FROM TRIP, EXPENSE WHERE TRIP.eid=EXPENSE.eid AND amount=(SELECT MAX(amount) FROM EXPENSE); RAISE NOTICE ''tno = % , from_city = % , to_city = %'',tn, fcity, tcity; END; 'LANGUAGE'plpgsql';

select Q1();



 CREATE OR REPLACE FUNCTION Q4() RETURNS VOID AS' DECLARE tn INT; fcity VARCHAR(20); tcity VARCHAR(20); ddate DATE; rdate DATE; amt MONEY; BEGIN SELECT TRIP.tno, from_city, to_city, departure_date, return, amount INTO tn, fcity, tcity, ddate, rdate,amt FROM SALESMAN, DEPT, TRIP, EXPENSE WHERE SALESMAN.deptno=DEPT.deptno AND SALESMAN.sno=TRIP.sno AND TRIP.eid=EXPENSE.eid AND DEPT.dept_name=''HR''; RAISE NOTICE ''tno = % , from_city = % , to_city = % , departure = % , return = % , amount = % '', tn, fcity, tcity, ddate, rdate, amt; END; 'LANGUAGE'plpgsql';
select Q4();




create or replace function  f1() returns void as' declare trip_cur cursor is select 
tno, from_city,to_city,departure_date,TRIP.eid,amount from TRIP, EXPENSE WHERE TRIP.eid
=EXPENSE.eid and to_city="MUMBAI"; trip_rec TRIP%rowtype; begin open
trip_cur; loop fetch trip_cur into trip_rec; exit when not found; raise notice''tno= %%,
from_city=%,to_city=%,departure_date=%,return=%,amount=%'',trip_rec.tno,trip_rec.
from_city,trip_rec.to_city.trip_rec.departure_date,trip_rec.return,trip_rec.amount;
end loop;end;'LANGUAGE'plpgsql';

Select f1(); 
by

PostgreSQL online editor

Write, Run & Share PostgreSQL queries online using OneCompiler's PostgreSQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for PostgreSQL. Getting started with the OneCompiler's PostgreSQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'PostgreSQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About PostgreSQL

PostgreSQL is a open source relational database system and is also knows as Postgres.

Key Features:

  • Postgres is not only free and open-source but also it is highly extensible.
  • Custom Data types and funtions from various programming languaues can be introduced and the good part is compiling entire database is not required.
  • ACID(Atomicity, Consistency, Isolation, Durability) compliant.
  • First DBMS which implemented Multi-version concurrency control (MVCC) feature.
  • It's the default database server for MacOS.
  • It supports all major operating systems like Linux, Windows, OpenBSD,FreeBSD etc.

Syntax help

1. CREATE

CREATE command is used to create a table, schema or an index.

Syntax:

         CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

2. ALTER

ALTER command is used to add, modify or delete columns or constraints from the database table.

Syntax

ALTER TABLE Table_name ADD column_name datatype;

3. TRUNCATE:

TRUNCATE command is used to delete the data present in the table but this will not delete the table.

Syntax

TRUNCATE table table_name;

4. DROP

DROP command is used to delete the table along with its data.

Syntax

DROP TABLE table_name;

5. RENAME

RENAME command is used to rename the table name.

Syntax

ALTER TABLE table_name1 RENAME to new_table_name1; 

6. INSERT

INSERT Statement is used to insert new records into the database table.

Syntax

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

7. SELECT

Select statement is used to select data from database tables.

Syntax:

SELECT column1, column2, ...
FROM table_name; 

8. UPDATE

UPDATE statement is used to modify the existing values of records present in the database table.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

9. DELETE

DELETE statement is used to delete the existing records present in the database table.

Syntax

DELETE FROM table_name where condition;