I want to create postgres schema for the following use case. [code]
- User
I want to create postgres schema for the following use case.
One user can be have multiple restaurants(organizations) One restaurant can have multiple employees. - Assistant
I want to create postgres schema for the following use case.
One user can be have multiple restaurants(organizations)
One restaurant can have multiple employees.
To design a PostgreSQL schema for your use case—where one user can own multiple restaurants (organizations), and each restaurant can have multiple employees—you'll need to create three primary tables:
Below is a step-by-step guide to creating this schema, including SQL CREATE TABLE statements with appropriate relationships and constraints.
This table stores information about the users who own restaurants.
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Explanation:
user_id: Unique identifier for each user.username & email: Must be unique to prevent duplicates.password_hash: Stores hashed passwords for security.created_at: Timestamp of when the user was created.This table stores information about the restaurants owned by users.
CREATE TABLE restaurants (
restaurant_id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
name VARCHAR(100) NOT NULL,
address VARCHAR(255),
phone VARCHAR(20),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
Explanation:
restaurant_id: Unique identifier for each restaurant.user_id: References the users table to establish ownership.name, address, phone: Basic details about the restaurant.created_at & updated_at: Timestamps for tracking creation and updates.FOREIGN KEY: Ensures that each restaurant is associated with a valid user. ON DELETE CASCADE means that if a user is deleted, all their restaurants are also deleted.This table stores information about employees working at each restaurant.
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
restaurant_id INTEGER NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
phone VARCHAR(20),
position VARCHAR(50),
hired_at DATE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (restaurant_id) REFERENCES restaurants(restaurant_id) ON DELETE CASCADE
);
Explanation:
employee_id: Unique identifier for each employee.restaurant_id: References the restaurants table to establish employment at a specific restaurant.first_name, last_name: Employee's name.email: Unique to prevent duplicate entries.phone, position, hired_at: Additional employee details.created_at & updated_at: Timestamps for tracking creation and updates.FOREIGN KEY: Ensures that each employee is associated with a valid restaurant. ON DELETE CASCADE means that if a restaurant is deleted, all its employees are also deleted.Indexes:
CREATE INDEX idx_restaurants_user_id ON restaurants(user_id);
CREATE INDEX idx_employees_restaurant_id ON employees(restaurant_id);
Data Integrity:
CHECK constraints if there are specific rules for certain fields. For example, ensuring phone numbers follow a particular format.Auditing:
updated_by or audit trails can be added if tracking changes is necessary.Roles and Permissions:
Here’s the complete SQL script combining all the tables and indexes:
-- Users Table
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Restaurants Table
CREATE TABLE restaurants (
restaurant_id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
name VARCHAR(100) NOT NULL,
address VARCHAR(255),
phone VARCHAR(20),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
-- Employees Table
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
restaurant_id INTEGER NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
phone VARCHAR(20),
position VARCHAR(50),
hired_at DATE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (restaurant_id) REFERENCES restaurants(restaurant_id) ON DELETE CASCADE
);
-- Indexes
CREATE INDEX idx_restaurants_user_id ON restaurants(user_id);
CREATE INDEX idx_employees_restaurant_id ON employees(restaurant_id);
For better visualization, here’s how the tables relate to each other:
Users
-----
user_id (PK)
username
email
password_hash
created_at
1
|
| owns
|
N
Restaurants
-----------
restaurant_id (PK)
user_id (FK)
name
address
phone
created_at
updated_at
1
|
| employs
|
N
Employees
---------
employee_id (PK)
restaurant_id (FK)
first_name
last_name
email
phone
position
hired_at
created_at
updated_at
Here’s how you might insert sample data into these tables:
-- Insert a user
INSERT INTO users (username, email, password_hash)
VALUES ('john_doe', 'john@example.com', 'hashed_password');
-- Insert a restaurant for the user
INSERT INTO restaurants (user_id, name, address, phone)
VALUES (1, 'John’s Pizza', '123 Main St', '555-1234');
-- Insert employees for the restaurant
INSERT INTO employees (restaurant_id, first_name, last_name, email, phone, position, hired_at)
VALUES
(1, 'Alice', 'Smith', 'alice@example.com', '555-5678', 'Chef', '2023-01-15'),
(1, 'Bob', 'Johnson', 'bob@example.com', '555-8765', 'Server', '2023-02-20');
This schema effectively models the relationships where users own multiple restaurants, and each restaurant employs multiple employees. You can extend this schema based on additional requirements, such as adding more attributes, implementing more complex relationships, or enforcing more constraints to maintain data integrity.
Feel free to adjust the data types and constraints based on the specific needs and context of your application.