What are some best practices for designing a database for a shop with different types of products, such as web hosting and email services, using MySQL?
When designing a database for a shop with different types of products, it is important to create a flexible and scalable structure that can accommodate various product types. One way to achieve this is by using a relational database like MySQL and creating separate tables for different product categories. Each table should have a unique identifier for the product, as well as columns for specific attributes such as price, description, and availability. By normalizing the database design and using foreign keys to establish relationships between tables, you can ensure efficient data retrieval and maintenance.
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
description TEXT,
availability BOOLEAN NOT NULL
);
CREATE TABLE web_hosting_products (
id INT PRIMARY KEY,
storage_capacity INT,
bandwidth INT
);
CREATE TABLE email_products (
id INT PRIMARY KEY,
max_accounts INT,
max_storage INT
);
ALTER TABLE web_hosting_products
ADD FOREIGN KEY (id) REFERENCES products(id);
ALTER TABLE email_products
ADD FOREIGN KEY (id) REFERENCES products(id);