用mysql语句创建一个电商平台的数据库
时间: 2023-12-30 19:03:55 浏览: 86
CREATE DATABASE ecommerce;
USE ecommerce;
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
address VARCHAR(200),
phone VARCHAR(20),
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY (id)
);
CREATE TABLE products (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
description TEXT,
price DECIMAL(10,2) NOT NULL,
image_url VARCHAR(200),
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY (id)
);
CREATE TABLE orders (
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE order_items (
id INT(11) NOT NULL AUTO_INCREMENT,
order_id INT(11) NOT NULL,
product_id INT(11) NOT NULL,
quantity INT(11) NOT NULL,
price DECIMAL(10,2) NOT NULL,
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY (id),
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
阅读全文