This example will include tables for Customers
, Products
, Orders
, and OrderDetails
. Each table will have primary keys, appropriate foreign keys, and some additional constraints to ensure data integrity.
Here’s a description of the database schema:
-- Create Customers table
CREATE TABLE Customers (
CustomerID INT AUTO_INCREMENT,
FirstName VARCHAR(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(255) UNIQUE NOT NULL,
Phone VARCHAR(15),
PRIMARY KEY (CustomerID)
);
-- Create Products table
CREATE TABLE Products (
ProductID INT AUTO_INCREMENT,
ProductName VARCHAR(255) NOT NULL,
Price DECIMAL(10, 2) NOT NULL CHECK (Price > 0),
Stock INT NOT NULL CHECK (Stock >= 0),
PRIMARY KEY (ProductID)
);
-- Create Orders table
CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT,
CustomerID INT,
OrderDate DATE NOT NULL,
TotalAmount DECIMAL(10, 2),
PRIMARY KEY (OrderID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
ON DELETE SET NULL ON UPDATE CASCADE
);
-- Create OrderDetails table
CREATE TABLE OrderDetails (
OrderDetailID INT AUTO_INCREMENT,
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL CHECK (Quantity > 0),
PRIMARY KEY (OrderDetailID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
ON DELETE RESTRICT ON UPDATE CASCADE,
UNIQUE (OrderID, ProductID)
);
Each table is designed to serve a specific function within the database, ensuring data integrity and relationships between different types of data.
CREATE TABLE Customers (
CustomerID INT AUTO_INCREMENT,
FirstName VARCHAR(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(255) UNIQUE NOT NULL,
Phone VARCHAR(15),
PRIMARY KEY (CustomerID)
);
AUTO_INCREMENT
attribute ensures that every new customer gets a unique ID automatically.NOT NULL
).UNIQUE
constraint prevents two customers from having the same email address, which is important for things like login or contact information.NOT NULL
constraint. It can store the customer’s phone number.CREATE TABLE Products (
ProductID INT AUTO_INCREMENT,
ProductName VARCHAR(255) NOT NULL,
Price DECIMAL(10, 2) NOT NULL CHECK (Price > 0),
Stock INT NOT NULL CHECK (Stock >= 0),
PRIMARY KEY (ProductID)
);
CHECK (Price > 0)
), ensuring that no products are entered with a zero or negative price.CHECK (Stock >= 0)
constraint ensures that stock levels do not fall below zero, which would be nonsensical in a real-world scenario.CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT,
CustomerID INT,
OrderDate DATE NOT NULL,
TotalAmount DECIMAL(10, 2),
PRIMARY KEY (OrderID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
ON DELETE SET NULL ON UPDATE CASCADE
);
CustomerID
in Orders is set to NULL
(ON DELETE SET NULL
), and updates to CustomerID
will be reflected in Orders (ON UPDATE CASCADE
).CREATE TABLE OrderDetails (
OrderDetailID INT AUTO_INCREMENT,
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL CHECK (Quantity > 0),
PRIMARY KEY (OrderDetailID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
ON DELETE RESTRICT ON UPDATE CASCADE,
UNIQUE (OrderID, ProductID)
);
CASCADE
for OrderID
means that deleting or updating an order will cascade these changes to the OrderDetails. RESTRICT
on ProductID
prevents deletion of a product that is part of an order.CHECK (Quantity > 0)
).Each table includes constraints and keys designed to maintain data integrity and define the relationships between different entities in the database. This structure helps in managing data accurately and efficiently within an e-commerce context.