CREATE DATABASE IF NOT EXISTS example_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE example_db;

CREATE TABLE IF NOT EXISTS market_data (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    variety VARCHAR(50) NOT NULL COMMENT '商品品种',
    trade_date DATE NOT NULL COMMENT '交易日期',
    open_price DECIMAL(18,4) COMMENT '开盘价',
    close_price DECIMAL(18,4) NOT NULL COMMENT '收盘价',
    high_price DECIMAL(18,4) COMMENT '最高价',
    low_price DECIMAL(18,4) COMMENT '最低价',
    volume DECIMAL(20,4) COMMENT '成交量',
    change_rate DECIMAL(10,4) COMMENT '涨跌幅(%)',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    source VARCHAR(50) COMMENT '数据来源',
    UNIQUE KEY uk_date_variety (trade_date, variety)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='行情数据表';

CREATE TABLE IF NOT EXISTS index_data (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    index_name VARCHAR(100) NOT NULL COMMENT '指数名称',
    date DATE NOT NULL COMMENT '日期',
    index_value DECIMAL(18,4) NOT NULL COMMENT '指数值',
    change_rate DECIMAL(10,4) COMMENT '涨跌幅(%)',
    stock_name VARCHAR(100) COMMENT '概念股名称',
    stock_price DECIMAL(18,4) COMMENT '股价',
    turnover_rate DECIMAL(10,4) COMMENT '换手率(%)',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    source VARCHAR(50) COMMENT '数据来源',
    UNIQUE KEY uk_date_index (date, index_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='指数数据表';

CREATE TABLE IF NOT EXISTS news_data (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(500) NOT NULL COMMENT '新闻标题',
    content TEXT COMMENT '新闻内容',
    publish_time DATETIME NOT NULL COMMENT '发布时间',
    related_commodity VARCHAR(50) COMMENT '关联商品',
    sentiment VARCHAR(10) NOT NULL COMMENT '舆情倾向(利好/利空/中性)',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    source VARCHAR(50) COMMENT '数据来源',
    UNIQUE KEY uk_title_time (title, publish_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='舆情数据表';
