-- create table for gym group

CREATE TABLE GGroup (
  id INTEGER NOT NULL PRIMARY KEY,
  group_name VARCHAR(100) NOT NULL
);

-- create table for gym
CREATE TABLE Gym (
  id INTEGER NOT NULL PRIMARY KEY,
  id_group INTEGER NOT NULL,
  gym_name VARCHAR(100) NOT NULL,
  gym_owner VARCHAR(100) NOT NULL,
  Location_in_Moscow VARCHAR(150) NOT NULL,
  no_of_clients INTEGER NOT NULL,
  regulated_by VARCHAR(8),
  year_founded DATE,
  foreign key (id_group) references GGroup(id)
);

-- create table for classes
CREATE TABLE Classes (
  id INTEGER NOT NULL PRIMARY KEY,
  class_name VARCHAR(150) NOT NULL
);

-- create table for location
CREATE TABLE Gym_Class (
  id_gym INTEGER NOT NULL,
  id_classes INTEGER NOT NULL,
  primary key (id_gym, id_classes),
  foreign key (id_classes) references Classes(id),
  foreign key (id_gym) references Gym(id)
);

-- insert data into the GGroup table
INSERT INTO GGroup VALUES (1, 'Bronze');
INSERT INTO GGroup VALUES (2, 'Silver');
INSERT INTO GGroup VALUES (3, 'Gold');

-- insert data into the Gym table
INSERT INTO Gym VALUES (1001, 1, 'ABCD', 'Khayelihle Nyathi','East', 150, 'MIPS', '1993-09-12');
INSERT INTO Gym VALUES (1002, 1, 'Ten Times', 'Lanceline Ayiv', 'East', 80, 'Unknown', '1992-06-24');
INSERT INTO Gym VALUES (1003, 1, 'YesMan', 'Jacobs Jacobs', 'South', 55, 'ICAS', '2018-03-12');
INSERT INTO Gym VALUES (1004, 3, 'NoMan', 'Vivovich Alexandra', 'North', 500, 'ICAS', '1996-06-23');
INSERT INTO Gym VALUES (1005, 3, 'Bossed', 'Susan Ndlovu', 'Central', 1262, 'BSCD', '2002-01-08');
INSERT INTO Gym VALUES (1006, 3, 'Gravity', 'Aunty P', 'Central', 767, 'MIPS', '2010-02-17');
INSERT INTO Gym VALUES (1007, 2, 'Oxygen', 'Khayelihle Nyathi', 'West', 226, 'MIPS', '2016-07-22');
INSERT INTO Gym VALUES (1008, 2, 'Apple Red', 'Francis Ayivi', 'West', 246, 'ICAS', '2020-05-12');
INSERT INTO Gym VALUES (1009, 1, 'Blue Balloon', 'Alpha Jennifer', 'West', 83, 'BSOD', '2007-10-14');
INSERT INTO Gym VALUES (1010, 3, 'Banana Curve', 'Khayelihle Nyathi', 'South', 400, 'MIPS', '2012-11-05');
INSERT INTO Gym VALUES (1011, 1, 'Greatness', 'Gracious Sibanda', 'North', 46, 'ICAS', '2022-12-27');
INSERT INTO Gym VALUES (1012, 1, 'Strength', 'Khayelihle Nyathi', 'North', 147, 'MIPS', '2004-08-08');

-- insert data into the table for Classes
INSERT INTO Classes VALUES (201, 'Dancing');
INSERT INTO Classes VALUES (202, 'Wrestling');
INSERT INTO Classes VALUES (203, 'Boxing');
INSERT INTO Classes VALUES (204, 'Martial Arts');
INSERT INTO Classes VALUES (205, 'Yogo');

-- insert data into the table for Gym_Class
INSERT INTO Gym_Class VALUES (1001, 201);
INSERT INTO Gym_Class VALUES (1002, 201);
INSERT INTO Gym_Class VALUES (1003, 202);
INSERT INTO Gym_Class VALUES (1004, 203);
INSERT INTO Gym_Class VALUES (1005, 205);
INSERT INTO Gym_Class VALUES (1006, 204);
INSERT INTO Gym_Class VALUES (1007, 202);
INSERT INTO Gym_Class VALUES (1008, 204);
INSERT INTO Gym_Class VALUES (1009, 201);
INSERT INTO Gym_Class VALUES (1010, 204);
INSERT INTO Gym_Class VALUES (1011, 202);
INSERT INTO Gym_Class VALUES (1012, 205);

SELECT * FROM GGroup;
SELECT * FROM Gym;
SELECT * FROM Classes;
SELECT * FROM Gym_Class;

-- developing five different queries for the database
SELECT gym_name FROM Gym WHERE no_of_clients >200;

SELECT DISTINCT gym_owner FROM Gym;

SELECT * FROM Gym ORDER BY id_group;

UPDATE Gym SET no_of_clients=155, id_group=2 WHERE id=1009; 
UPDATE Gym_Class SET id_classes=204 WHERE id_gym = 1009;

INSERT INTO Gym VALUES (1013, 2, 'Purple Bush', 'Jacob Jacobs', 'South', 123, 'ICAS', '2023-01-22');
INSERT INTO Gym_Class VALUES (1013, 205)



 
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;