A comprehensive PostgreSQL database system for managing car dealerships, built with Node.js and PGlite. This project demonstrates advanced database operations including table creation, data manipulation, relationships, and complex queries for a multi-dealership car sales business.
- Features
- Database Schema
- Prerequisites
- Installation
- Usage
- Database Structure
- Sample Data
- SQL Operations
- Project Structure
- Contributing
- License
- Multi-Dealership Management: Track multiple dealership locations across different states
- Staff Management: Manage employees across different roles (CEO, Salesperson, Mechanic, etc.)
- Inventory Tracking: Comprehensive car inventory with condition ratings and pricing
- Sales Records: Track sold cars with seller information and sale prices
- Data Relationships: Fully normalized database with foreign key constraints
- CRUD Operations: Complete Create, Read, Update, Delete functionality
- Sample Data: Pre-populated with realistic dealership data for 70+ vintage and classic cars
- In-Memory Database: Uses PGlite for fast, serverless PostgreSQL operations
The system consists of four main tables with the following relationships:
dealerships (1) ββ (many) staff
dealerships (1) ββ (many) cars
cars (1) ββ (1) sold_cars
staff (1) ββ (many) sold_cars
Before running this project, make sure you have:
- Node.js (version 18 or higher)
- npm (comes with Node.js)
-
Clone the repository
git clone https://github.com/bakadja/postgresql.git cd postgresql
-
Install dependencies
npm install
-
Run the application
npm start # or npm run dev # or node index.js
The application automatically executes the following sequence:
- Database Setup: Creates all necessary database tables
- Data Population: Populates tables with sample dealership and car data
- CRUD Operations: Demonstrates various database operations
- Table Modifications: Adds constraints and foreign key relationships
- Custom Queries: Executes queries from
query.sql
file - Results Display: Shows query results in a formatted console table
To execute your own queries:
- Edit the
query.sql
file with your SQL commands - Run the application:
npm start
The results will be displayed in your console as a formatted table.
Stores information about dealership locations.
Column | Type | Constraints | Description |
---|---|---|---|
id | SERIAL | PRIMARY KEY | Unique dealership identifier |
city | TEXT | NOT NULL | Dealership city location |
state | CHAR(2) | NOT NULL | Two-letter state code |
established | DATE | NOT NULL | Date dealership was established |
Manages employee information for each dealership.
Column | Type | Constraints | Description |
---|---|---|---|
id | SERIAL | PRIMARY KEY | Unique staff identifier |
dealership_id | INTEGER | NOT NULL, FOREIGN KEY | Reference to dealership |
name | TEXT | NOT NULL | Employee full name |
role | TEXT | NOT NULL | Employee job role |
Comprehensive car inventory across all dealerships.
Column | Type | Constraints | Description |
---|---|---|---|
id | SERIAL | PRIMARY KEY | Unique car identifier |
brand | TEXT | NOT NULL | Car manufacturer |
model | TEXT | NOT NULL | Car model name |
year | INTEGER | NOT NULL | Manufacturing year |
price | INTEGER | NOT NULL | Listed price in USD |
color | TEXT | NOT NULL | Car color |
condition | INTEGER | NOT NULL (0-10 scale) | Car condition rating |
sold | BOOLEAN | NOT NULL | Sale status |
dealership_id | INTEGER | NOT NULL, FOREIGN KEY | Reference to dealership |
Records of completed car sales.
Column | Type | Constraints | Description |
---|---|---|---|
id | SERIAL | PRIMARY KEY | Unique sale identifier |
cars_id | INTEGER | NOT NULL, FOREIGN KEY | Reference to sold car |
seller | INTEGER | NOT NULL, FOREIGN KEY | Reference to selling staff |
sold_date | DATE | NOT NULL | Date of sale |
sold_price | INTEGER | NOT NULL | Final sale price in USD |
The database comes pre-populated with:
- 4 Dealerships across different states (IL, GA, MI, PA)
- 15+ Staff Members with various roles including:
- CEO, Accountant, HR Officer
- Salespersons, Mechanics
- Data Administrator
- 75+ Classic and Vintage Cars from premium brands
- 14+ Sales Records with complete transaction details
Luxury & Sports: Aston Martin, Ferrari, Lamborghini, Rolls-Royce, Bentley, Maserati
German Engineering: BMW, Mercedes-Benz, Porsche, Volkswagen
American Classics: Ford, Chevrolet, Dodge, AMC, Pontiac, Lincoln
British Heritage: Jaguar, Lotus, Triumph
Italian Style: Alfa Romeo, Fiat
Japanese Legends: Toyota, Nissan
The project demonstrates comprehensive SQL operations:
- Create: Insert new dealerships, staff, and car inventory
- Read: Complex queries with joins, aggregations, and filtering
- Update: Modify car prices, conditions, and sales status
- Delete: Remove records based on various conditions
- Foreign Key Relationships: Ensures data integrity across tables
- Table Alterations: Dynamic schema modifications and constraint additions
- Data Validation: Enforces business rules through database constraints
- Complex Queries: Multi-table joins and analytical operations
-- Find all unsold luxury cars over $100,000
SELECT c.brand, c.model, c.year, c.price, d.city, d.state
FROM cars c
JOIN dealerships d ON c.dealership_id = d.id
WHERE c.sold = FALSE AND c.price > 100000
ORDER BY c.price DESC;
-- Get sales performance by dealership
SELECT d.city, d.state, COUNT(sc.id) as total_sales,
AVG(sc.sold_price) as avg_sale_price
FROM dealerships d
LEFT JOIN cars c ON d.id = c.dealership_id
LEFT JOIN sold_cars sc ON c.id = sc.cars_id
GROUP BY d.id, d.city, d.state
ORDER BY total_sales DESC;
postgresql/
βββ index.js # Main application entry point
βββ create-tables.sql # Database schema creation
βββ populate-tables.sql # Initial dealership and staff data
βββ insert-cars-data.sql # Classic car inventory data
βββ insert-new-data.sql # Additional data and sales records
βββ crud-operations.sql # CRUD operation demonstrations
βββ alter-table.sql # Schema modifications and constraints
βββ query.sql # Custom queries (edit this file)
βββ package.json # Node.js project configuration
βββ LICENSE # MIT license file
βββ .gitignore # Git ignore rules
βββ .gitattributes # Git attributes configuration
βββ README.md # Project documentation
- Database Layer: SQL files containing schema, data, and operations
- Application Layer: Node.js runtime with PGlite integration
- Configuration: Package management and environment setup
Contributions are welcome! Here's how you can help:
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Database Schema: Enhance table structures or add new entities
- Sample Data: Add more realistic dealership and car data
- Query Examples: Contribute useful SQL query patterns
- Documentation: Improve README or add code comments
- Testing: Add unit tests for database operations
- Follow existing code style and conventions
- Add appropriate comments to SQL files
- Update README if adding new features
- Test your changes thoroughly
This project is licensed under the MIT License - see the LICENSE file for details.
- β Commercial use
- β Modification
- β Distribution
- β Private use
- β Liability
- β Warranty
Built with β€οΈ using Node.js and PGlite
Report Bug β’ Request Feature β’ View Demo