MySQL数据库,1.新建一个数据库taobao db,建一个商品分类表tb goodscate,商品信息表tb goods,客户信息表tb customer );2.添加数据 商品分类表500种商品分类 ,商品表50000种商品必须满足十种商品分类,客户信息表 添加1000个客户信息
时间: 2023-12-22 21:04:57 浏览: 143
MySQL数据库,用于从零开始搭建使用,利用Navicat运行一下sql文件即可!
好的,针对你的问题,我可以给你提供以下 SQL 语句来完成你的要求:
1. 新建数据库 taobao_db,商品分类表 tb_goodscate,商品信息表 tb_goods,客户信息表 tb_customer:
```
-- 创建 taobao_db 数据库
CREATE DATABASE taobao_db;
-- 使用 taobao_db 数据库
USE taobao_db;
-- 创建商品分类表 tb_goodscate
CREATE TABLE tb_goodscate (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL
);
-- 创建商品信息表 tb_goods
CREATE TABLE tb_goods (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
price DECIMAL(10,2) NOT NULL,
category_id INT NOT NULL,
CONSTRAINT fk_category_id FOREIGN KEY (category_id) REFERENCES tb_goodscate(id)
);
-- 创建客户信息表 tb_customer
CREATE TABLE tb_customer (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
phone VARCHAR(20) NOT NULL
);
```
2. 添加数据,商品分类表添加 500 种商品分类,商品表添加 50000 种商品必须满足十种商品分类,客户信息表添加 1000 个客户信息:
```
-- 在 tb_goodscate 表中添加 500 种商品分类
INSERT INTO tb_goodscate (name) VALUES
('家用电器'), ('手机数码'), ('电脑办公'), ('家居家装'), ('美妆个护'),
('服饰鞋帽'), ('母婴玩具'), ('食品饮料'), ('珠宝配饰'), ('运动户外');
-- 随机从 tb_goodscate 表中选取 10 种分类,添加 50000 种商品到 tb_goods 表中
INSERT INTO tb_goods (name, price, category_id)
SELECT CONCAT('商品', ROW_NUMBER() OVER(ORDER BY RAND())) AS name,
ROUND(RAND() * 1000, 2) AS price,
category_id
FROM (
SELECT id AS category_id
FROM tb_goodscate
ORDER BY RAND()
LIMIT 10
) t1, (
SELECT 1 AS cnt UNION ALL
SELECT 2 AS cnt UNION ALL
SELECT 3 AS cnt UNION ALL
SELECT 4 AS cnt UNION ALL
SELECT 5 AS cnt
) t2
LIMIT 50000;
-- 在 tb_customer 表中添加 1000 个客户信息
INSERT INTO tb_customer (name, age, gender, phone)
SELECT CONCAT('客户', ROW_NUMBER() OVER(ORDER BY RAND())) AS name,
FLOOR(RAND() * 60 + 18) AS age,
IF(RAND() > 0.5, '男', '女') AS gender,
CONCAT('138', LPAD(FLOOR(RAND() * 100000000), 8, '0')) AS phone
FROM (
SELECT 1 AS cnt UNION ALL
SELECT 2 AS cnt UNION ALL
SELECT 3 AS cnt UNION ALL
SELECT 4 AS cnt UNION ALL
SELECT 5 AS cnt UNION ALL
SELECT 6 AS cnt UNION ALL
SELECT 7 AS cnt UNION ALL
SELECT 8 AS cnt UNION ALL
SELECT 9 AS cnt UNION ALL
SELECT 10 AS cnt
) t;
```
这些 SQL 语句可以在 MySQL 数据库中执行,即可完成你的要求。注意,如果你想要修改表结构或者添加更多数据,可以根据需要进行调整。
阅读全文