create table a(aid int primary key,aname text, type text check (type in('urban','rural'))); create table p(pno int primary key,name text,birthdate date, income money,aid int references a(aid)); insert into a values('12','aa','rural'),('11','bb','rural'),('13','cc','urban'); insert into p values('1','rutu','09/09/2005','9000000','11'),('2','ruhi','09/08/2005','8000000','13'), ('3','rutuja','08/09/2005','7000000','12'); select * from a; select * from p; create or replace function r() returns void as' begin update p set income=income+income*''0.10'' where aid in(select aid from a where type=''rural''); end; 'language plpgsql; select r(); select * from p; create or replace function t() returns trigger as' begin raise notice''person record is being deleted''; return old; end; 'language plpgsql; create trigger t before delete on p for each row execute procedure t(); delete from p where pno='1'; select * from p; create or replace function n() returns text as' declare r p.name%type; c cursor for select p.name from p,a where p.aid=a.aid and a.type=''urban''; begin open c; loop fetch c into r; exit when not found; raise notice ''pname=% '',r; end loop; close c; end; 'language plpgsql; select n();
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;