Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Last active December 2, 2024 13:01
Show Gist options
  • Save luisjunco/a3c7dc8a8a8c3468bd387e4f3acbd107 to your computer and use it in GitHub Desktop.
Save luisjunco/a3c7dc8a8a8c3468bd387e4f3acbd107 to your computer and use it in GitHub Desktop.

PostgreSQL Example DB

Users Table

CREATE TABLE users (
    user_id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


INSERT INTO users (name, email, created_at) VALUES ('Alice Smith', '[email protected]', '2024-01-15');
INSERT INTO users (name, email, created_at) VALUES ('Bob Johnson', '[email protected]', '2024-02-20');
INSERT INTO users (name, email, created_at) VALUES ('Carol Brown', '[email protected]', '2024-03-10');
INSERT INTO users (name, email, created_at) VALUES ('Diana Green', '[email protected]', '2024-04-05');

Products Table

CREATE TABLE products (
  product_id SERIAL PRIMARY KEY,
  title VARCHAR(100) UNIQUE NOT NULL,
  description TEXT,
  price DECIMAL CHECK (price >= 0) NOT NULL,
  stock_quantity INT DEFAULT 0 CHECK (stock_quantity >= 0)
);


INSERT INTO products (title, description, price, stock_quantity) VALUES ('Wireless Mouse', '', 25.99, 50);
INSERT INTO products (title, description, price, stock_quantity) VALUES ('Keyboard', '', 45.00, 20);
INSERT INTO products (title, description, price, stock_quantity) VALUES ('Speakers', '', 45.00, 6);
INSERT INTO products (title, description, price, stock_quantity) VALUES ('Monitor', '', 150.00, 10);
INSERT INTO products (title, description, price) VALUES ('USB-C Cable', '', 10.50);

Orders Table

CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    user_id INT NOT NULL REFERENCES users(user_id),
    product_id INT NOT NULL REFERENCES products(product_id),
    quantity INT NOT NULL CHECK (quantity > 0),
    total_amount DECIMAL NOT NULL CHECK (total_amount >= 0),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO orders (user_id, product_id, quantity, total_amount) 
VALUES
(1, 1, 2, 51.98),
(2, 2, 1, 45.00),
(2, 4, 1, 150.00),
(3, 5, 2, 21.00);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment