以下是锋迷商城建表的SQL语句,适用于设计商城的数据库结构。每个表的创建语句包括字段定义及其数据类型、主键和外键等详细信息。
1. 用户表(users)
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
2. 商品表(products)
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
3. 订单表(orders)
CREATE TABLE orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total DECIMAL(10, 2),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
4. 订单详情表(order_details)
CREATE TABLE order_details (
detail_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT,
product_id INT,
quantity INT,
price DECIMAL(10, 2),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
5. 库存日志表(stock_logs)
CREATE TABLE stock_logs (
log_id INT PRIMARY KEY AUTO_INCREMENT,
product_id INT,
change_amount INT,
change_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
以上SQL语句用于在锋迷商城中建立核心数据表,方便商城功能的实现和数据管理。