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();
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.
PostgreSQL is a open source relational database system and is also knows as Postgres.
CREATE command is used to create a table, schema or an index.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
ALTER command is used to add, modify or delete columns or constraints from the database table.
ALTER TABLE Table_name ADD column_name datatype;
TRUNCATE command is used to delete the data present in the table but this will not delete the table.
TRUNCATE table table_name;
DROP command is used to delete the table along with its data.
DROP TABLE table_name;
RENAME command is used to rename the table name.
ALTER TABLE table_name1 RENAME to new_table_name1;
INSERT Statement is used to insert new records into the database table.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Select statement is used to select data from database tables.
SELECT column1, column2, ...
FROM table_name;
UPDATE statement is used to modify the existing values of records present in the database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE statement is used to delete the existing records present in the database table.
DELETE FROM table_name where condition;