CREATE TABLE Hospital( hos_ID VARCHAR(04) PRIMARY KEY, host_name VARCHAR(20), location VARCHAR(10) ); CREATE TABLE Medicine( medicine_ID VARCHAR(04) PRIMARY KEY, med_name VARCHAR(20), description VARCHAR(80), hos_ID VARCHAR(04), CONSTRAINT fk_Hospital FOREIGN KEY (hos_ID) REFERENCES Hospital(hos_ID) ); CREATE TABLE Doctor( doc_ID VARCHAR(04) PRIMARY KEY, doc_name VARCHAR(20), doc_contact_num INTEGER NOT NULL, age INTEGER, salary FLOAT NOT NULL, hos_ID VARCHAR, CONSTRAINT fk_Hospital FOREIGN KEY (hos_ID) REFERENCES Hospital(hos_ID) ); CREATE TABLE Patient( patient_ID VARCHAR(06) PRIMARY KEY, patient_name VARCHAR(20) NOT NULL, p_contact_num INTEGER NOT NULL, DOB DATE, age INTEGER, hos_ID VARCHAR(04), doc_ID VARCHAR(04), CONSTRAINT fk_Hospital FOREIGN KEY (hos_ID) REFERENCES Hospital(hos_ID), CONSTRAINT fk_Doctor FOREIGN KEY (doc_ID) REFERENCES Doctor(doc_ID) ); CREATE TABLE Bill( patient_ID VARCHAR, issue_date DATE NOT NULL, barcode_no VARCHAR PRIMARY KEY, bill_amount FLOAT, CONSTRAINT fk_Patient FOREIGN KEY (patient_ID) REFERENCES Patient(patient_ID) ); CREATE TABLE Patient_Medicine( medicine_ID VARCHAR(04), patient_ID VARCHAR(06), PRIMARY KEY(medicine_ID,patient_ID), CONSTRAINT fk_Patient FOREIGN KEY (patient_ID) REFERENCES Patient(patient_ID), CONSTRAINT fk_Medicine FOREIGN KEY (medicine_ID) REFERENCES Medicine(medicine_ID) ); CREATE TABLE Doc_qualification( doc_ID VARCHAR(04) PRIMARY KEY, qualification VARCHAR(50), CONSTRAINT fk_Doctor FOREIGN KEY (doc_ID) REFERENCES Doctor(doc_ID) ); CREATE TABLE Doc_Email( doc_ID VARCHAR(04) PRIMARY KEY, email VARCHAR(20), CONSTRAINT fk_Doctor FOREIGN KEY (doc_ID) REFERENCES Doctor(doc_ID) ); INSERT INTO Hospital VALUES ('H001','ABC hospital','Colombo'); INSERT INTO Medicine VALUES ('M011', 'Acetaminophen', 'Relieves pain and fever', 'H001'), ('M012', 'Paracetamol', 'Treat pain and reduce a high temperature', 'H001'), ('M013', 'Simethicone', 'Relieve the painful symptoms of too much gas in the stomach and intestines.', 'H001'), ('M014', 'Panadol', 'paracetamol-based painkiller.', 'H001'); INSERT INTO Doctor VALUES ( 'D100', ' Dr.S.A.Fernando ', '0724523675' , 32, 100000.00, 'H001'), ( 'D200', ' Dr.G.B.Perera ', '0754433656', 41, 100000.00, 'H001'), ('D300', ' Dr.A.W.Kosala ', '0776894368', 38, 120000.00, 'H001'); INSERT INTO Patient VALUES ( 'PI0032', 'Miss.S.Anne', '0765863321' , '2000-03-18', 22, 'H001','D100'), ( 'PI0033', 'Mrs.P.Jane' , '0765543301', '1992-04-06', 30, 'H001', 'D300'), ( 'PI0034', 'Mr.T.Peter', '0711163551', '1999-09-26', 23, 'H001', 'D100'); INSERT INTO Bill VALUES ('PI0032', '2022-12-22', '1_23452_35376_9', 2350.00), ('PI0033', '2022-12-22', '2_67343_76392_0', 1540.00), ('PI0034', '2022-12-22', '3_87642_28935_8', 1540.00); INSERT INTO Patient_Medicine VALUES ( 'M011', 'PI0032' ), ( 'M012', 'PI0033' ), ( 'M012', 'PI0032' ); INSERT INTO Doc_qualification VALUES ( 'D100', ' MBBS, MD and PhD ' ), ( 'D200', ' MBBS ' ), ( 'D300', ' MBBS, MD and PhD ' ); INSERT INTO Doc_Email VALUES ( 'D100', ', [email protected] ' ), ( 'D200', ' [email protected] ' ), ( 'D300', ' [email protected] ' ); SELECT * FROM Patient; SELECT doc_name, doc_contact_num FROM Doctor; SELECT DISTINCT patient_name, age FROM Patient; SELECT COUNT(*) FROM Patient; SELECT * FROM Bill WHERE bill_amount=1540.00; SELECT * FROM Medicine ORDER BY med_name DESC; SELECT patient_name,p_contact_num,med_name FROM Patient, Medicine WHERE Patient.hos_ID = Medicine.hos_ID ;
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;