You are on page 1of 9

Designing a database schema for an online merch store involves identifying the entities, their attributes,

and the relationships between them. Here's a simplified example of a database schema for an online
merch store:

Entities:

1. **Users:**

- UserID (Primary Key)

- Username

- Password

- Email

- FirstName

- LastName

- Address

- PhoneNumber

2. **Products:**

- ProductID (Primary Key)

- ProductName

- Description

- Price

- StockQuantity

3. **Categories:**

- CategoryID (Primary Key)

- CategoryName

4. **Orders:**

- OrderID (Primary Key)


- UserID (Foreign Key referencing Users.UserID)

- OrderDate

- TotalAmount

5. **OrderItems:**

- OrderItemID (Primary Key)

- OrderID (Foreign Key referencing Orders.OrderID)

- ProductID (Foreign Key referencing Products.ProductID)

- Quantity

- Subtotal

Now, let's define the relationships:

- Users can place many orders, but each order belongs to one user. (One-to-Many relationship between
Users and Orders)

- Each order can have multiple order items, and each order item belongs to one order. (One-to-Many
relationship between Orders and OrderItems)

- Products can belong to multiple categories, and each category can have multiple products. (Many-to-
Many relationship between Products and Categories, often implemented using an associative table)

Tables:

1. **Users:**

```

CREATE TABLE Users (

UserID INT PRIMARY KEY,

Username VARCHAR(255) NOT NULL,

Password VARCHAR(255) NOT NULL,

Email VARCHAR(255) NOT NULL,

FirstName VARCHAR(255),
LastName VARCHAR(255),

Address VARCHAR(255),

PhoneNumber VARCHAR(15)

);

```

2. **Products:**

```

CREATE TABLE Products (

ProductID INT PRIMARY KEY,

ProductName VARCHAR(255) NOT NULL,

Description TEXT,

Price DECIMAL(10, 2) NOT NULL,

StockQuantity INT NOT NULL

);

```

3. **Categories:**

```

CREATE TABLE Categories (

CategoryID INT PRIMARY KEY,

CategoryName VARCHAR(255) NOT NULL

);

```

4. **Orders:**

```

CREATE TABLE Orders (

OrderID INT PRIMARY KEY,


UserID INT,

OrderDate DATE NOT NULL,

TotalAmount DECIMAL(10, 2) NOT NULL,

FOREIGN KEY (UserID) REFERENCES Users(UserID)

);

```

5. **OrderItems:**

```

CREATE TABLE OrderItems (

OrderItemID INT PRIMARY KEY,

OrderID INT,

ProductID INT,

Quantity INT NOT NULL,

Subtotal DECIMAL(10, 2) NOT NULL,

FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),

FOREIGN KEY (ProductID) REFERENCES Products(ProductID)

);

```

6. **ProductCategories (Associative Table):**

```

CREATE TABLE ProductCategories (

ProductID INT,

CategoryID INT,

PRIMARY KEY (ProductID, CategoryID),

FOREIGN KEY (ProductID) REFERENCES Products(ProductID),

FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)

);
```

This schema provides a foundation for an online merch store, and you can extend or modify it based on
your specific requirements. Keep in mind that this is a simplified example, and in a real-world scenario,
you might need to consider additional factors such as security, indexing, and optimization based on the
database management system you are using. Designing a database schema for an online merch store
involves identifying the entities, their attributes, and the relationships between them. Here's a
simplified example of a database schema for an online merch store:

Entities:

1. **Users:**

- UserID (Primary Key)

- Username

- Password

- Email

- FirstName

- LastName

- Address

- PhoneNumber

2. **Products:**

- ProductID (Primary Key)

- ProductName

- Description

- Price

- StockQuantity

3. **Categories:**

- CategoryID (Primary Key)


- CategoryName

4. **Orders:**

- OrderID (Primary Key)

- UserID (Foreign Key referencing Users.UserID)

- OrderDate

- TotalAmount

5. **OrderItems:**

- OrderItemID (Primary Key)

- OrderID (Foreign Key referencing Orders.OrderID)

- ProductID (Foreign Key referencing Products.ProductID)

- Quantity

- Subtotal

Now, let's define the relationships:

- Users can place many orders, but each order belongs to one user. (One-to-Many relationship between
Users and Orders)

- Each order can have multiple order items, and each order item belongs to one order. (One-to-Many
relationship between Orders and OrderItems)

- Products can belong to multiple categories, and each category can have multiple products. (Many-to-
Many relationship between Products and Categories, often implemented using an associative table)

Tables:

1. **Users:**

```

CREATE TABLE Users (

UserID INT PRIMARY KEY,


Username VARCHAR(255) NOT NULL,

Password VARCHAR(255) NOT NULL,

Email VARCHAR(255) NOT NULL,

FirstName VARCHAR(255),

LastName VARCHAR(255),

Address VARCHAR(255),

PhoneNumber VARCHAR(15)

);

```

2. **Products:**

```

CREATE TABLE Products (

ProductID INT PRIMARY KEY,

ProductName VARCHAR(255) NOT NULL,

Description TEXT,

Price DECIMAL(10, 2) NOT NULL,

StockQuantity INT NOT NULL

);

```

3. **Categories:**

```

CREATE TABLE Categories (

CategoryID INT PRIMARY KEY,

CategoryName VARCHAR(255) NOT NULL

);

```
4. **Orders:**

```

CREATE TABLE Orders (

OrderID INT PRIMARY KEY,

UserID INT,

OrderDate DATE NOT NULL,

TotalAmount DECIMAL(10, 2) NOT NULL,

FOREIGN KEY (UserID) REFERENCES Users(UserID)

);

```

5. **OrderItems:**

```

CREATE TABLE OrderItems (

OrderItemID INT PRIMARY KEY,

OrderID INT,

ProductID INT,

Quantity INT NOT NULL,

Subtotal DECIMAL(10, 2) NOT NULL,

FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),

FOREIGN KEY (ProductID) REFERENCES Products(ProductID)

);

```

6. **ProductCategories (Associative Table):**

```

CREATE TABLE ProductCategories (

ProductID INT,

CategoryID INT,
PRIMARY KEY (ProductID, CategoryID),

FOREIGN KEY (ProductID) REFERENCES Products(ProductID),

FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)

);

```

This schema provides a foundation for an online merch store, and you can extend or modify it based on
your specific requirements. Keep in mind that this is a simplified example, and in a real-world scenario,
you might need to consider additional factors such as security, indexing, and optimization based on the
database management system you are using.

You might also like