二手商品交易数据库sql语句
时间: 2023-06-12 17:08:04 浏览: 104
以下是一个简单的二手商品交易数据库的 SQL 语句,包含商品信息、用户信息和交易记录:
创建商品信息表:
```
CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
description TEXT,
seller_id INT NOT NULL,
FOREIGN KEY (seller_id) REFERENCES users (user_id)
);
```
创建用户信息表:
```
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(50) NOT NULL
);
```
创建交易记录表:
```
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY,
buyer_id INT NOT NULL,
seller_id INT NOT NULL,
product_id INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (buyer_id) REFERENCES users (user_id),
FOREIGN KEY (seller_id) REFERENCES users (user_id),
FOREIGN KEY (product_id) REFERENCES products (product_id)
);
```
插入商品信息:
```
INSERT INTO products (product_id, name, price, description, seller_id)
VALUES (1, 'iPhone X', 999.99, '64GB, space grey', 1);
INSERT INTO products (product_id, name, price, description, seller_id)
VALUES (2, 'AirPods', 149.99, 'Wireless earbuds', 2);
INSERT INTO products (product_id, name, price, description, seller_id)
VALUES (3, 'Nintendo Switch', 299.99, 'Console with Joy-Cons', 3);
```
插入用户信息:
```
INSERT INTO users (user_id, username, email, password)
VALUES (1, 'john', 'john@example.com', 'password123');
INSERT INTO users (user_id, username, email, password)
VALUES (2, 'jane', 'jane@example.com', 'password456');
INSERT INTO users (user_id, username, email, password)
VALUES (3, 'bob', 'bob@example.com', 'password789');
```
插入交易记录:
```
INSERT INTO transactions (transaction_id, buyer_id, seller_id, product_id, price)
VALUES (1, 2, 1, 1, 999.99);
INSERT INTO transactions (transaction_id, buyer_id, seller_id, product_id, price)
VALUES (2, 3, 1, 2, 149.99);
INSERT INTO transactions (transaction_id, buyer_id, seller_id, product_id, price)
VALUES (3, 1, 3, 3, 299.99);
```
以上只是一个简单的示例,实际的二手交易数据库可能需要更多的表和字段,以及更复杂的查询和操作。