CREATE TABLE cars (
  car_id SERIAL PRIMARY KEY,
  car_number VARCHAR(10) NOT NULL UNIQUE,
  manufacturer VARCHAR(50) NOT NULL,
  model VARCHAR(50) NOT NULL,
  type VARCHAR(20) NOT NULL,
  num_seats SMALLINT NOT NULL,
  color VARCHAR(20) NOT NULL,
  daily_rate DECIMAL(10,2) NOT NULL CHECK (daily_rate > 0)
);

CREATE TABLE customers (
  customer_id SERIAL PRIMARY KEY,
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  email VARCHAR(100) UNIQUE,
  phone CHAR(12),
  driver_license VARCHAR(20) UNIQUE
);

CREATE TABLE agreements (
  agreement_id SERIAL PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  car_id SMALLINT NOT NULL REFERENCES cars(car_id),
  rental_start_date DATE NOT NULL,
  rental_end_date DATE NOT NULL CHECK (rental_end_date > rental_start_date),
  agreement_amount DECIMAL(10,2) NOT NULL,
  agreement_date DATE NOT NULL DEFAULT CURRENT_DATE,
  CONSTRAINT agreements_fk0 FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
  CONSTRAINT agreements_fk1 FOREIGN KEY (car_id) REFERENCES cars(car_id)
);

CREATE TABLE payments (
  payment_id SERIAL PRIMARY KEY,
  agreement_id SMALLINT NOT NULL,
  payment_method VARCHAR(20) NOT NULL,
  payment_amount DECIMAL(10,2) NOT NULL CHECK (payment_amount > 0), 
  CONSTRAINT payments_fk0 FOREIGN KEY (agreement_id) REFERENCES agreements(agreement_id)
);

-- Insert example data into cars table

INSERT INTO cars (car_number, manufacturer, model, type, num_seats, color, daily_rate)
VALUES ('CR123', 'Toyota', 'Camry', 'Sedan', 5, 'Silver', 59.99),
       ('CR456', 'Honda', 'Civic', 'Sedan', 4, 'Red', 49.95),
       ('CR789', 'Ford', 'Escape', 'SUV', 5, 'Blue', 74.50),
       ('CR012', 'Nissan', 'Altima', 'Sedan', 5, 'Black', 62.75),
       ('CR345', 'Hyundai', 'Elantra', 'Sedan', 5, 'Gray', 45.00),
       ('CR678', 'Chevrolet', 'Malibu', 'Sedan', 5, 'White', 57.99),
       ('CR901', 'Kia', 'Sportage', 'SUV', 5, 'Green', 69.99),
       ('CR234', 'Volkswagen', 'Jetta', 'Sedan', 5, 'Yellow', 52.50),
       ('CR567', 'Subaru', 'Legacy', 'Sedan', 5, 'Gold', 65.25),
       ('CR890', 'Tesla', 'Model 3', 'Electric', 5, 'Black', 129.95);

-- Insert example data into customers table

INSERT INTO customers (first_name, last_name, email, phone, driver_license)
VALUES ('John', 'Doe', '[email protected]', '123-456-7890', 'DL12345'),
       ('Jane', 'Smith', '[email protected]', '567-890-1234', 'DL54321'),
       ('Michael', 'Johnson', '[email protected]', '987-654-3210', 'DL98765'),
       ('Sarah', 'Lee', '[email protected]', '456-123-7890', 'DL23456'),
       ('David', 'Miller', '[email protected]', '876-543-2109', 'DL87654'),
       ('Emily', 'Garcia', '[email protected]', '321-098-7654', 'DL32109'),
       ('Matthew', 'Davis', '[email protected]', '765-432-1098', 'DL76543'),
       ('Jennifer', 'Rodriguez', '[email protected]', '210-987-6543', 'DL65432'),
       ('Joshua', 'Wilson', '[email protected]', '654-321-0987', 'DL43210'),
       ('Amanda', 'Brown', '[email protected]', '109-876-5432', 'DL10987');


INSERT INTO agreements (customer_id, car_id, rental_start_date, rental_end_date, agreement_amount, agreement_date)
VALUES
  (10, 2, '2024-04-20', '2024-04-27', 250.50, '2024-04-18'),
  (9, 5, '2024-04-15', '2024-04-22', 310.75, '2024-04-10'),
  (6, 1, '2024-04-25', '2024-05-02', 185.25, '2024-04-23'),
  (4, 7, '2024-04-12', '2024-04-19', 429.99, '2024-04-08'),
  (8, 3, '2024-04-18', '2024-04-25', 278.00, '2024-04-16'),
  (3, 8, '2024-04-22', '2024-04-29', 395.65, '2024-04-20'),
  (7, 4, '2024-04-10', '2024-04-17', 198.90, '2024-04-05'),
  (1, 6, '2024-04-27', '2024-05-04', 212.35, '2024-04-25'),
  (5, 9, '2024-04-16', '2024-04-23', 487.50, '2024-04-13'),
  (2, 10, '2024-04-20', '2024-04-27', 369.10, '2024-04-18');

INSERT INTO payments (agreement_id, payment_method, payment_amount)
VALUES
  (3, 'Credit Card', 287.95),
  (7, 'Debit Card', 142.50),
  (5, 'Cash', 310.00),
  (2, 'Credit Card', 459.70),
  (1, 'Cash', 198.25),
  (9, 'Debit Card', 365.40),
  (8, 'Credit Card', 221.15),
  (6, 'Cash', 167.80),
  (4, 'Debit Card', 482.90),
  (10, 'Credit Card', 394.65);


SELECT * FROM cars
LIMIT 3;

SELECT * FROM customers
LIMIT 3;

SELECT * FROM agreements
LIMIT 3;

SELECT * FROM payments
LIMIT 3; 

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;