-- 删除现有表（如果存在）
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS regions;

CREATE DATABASE IF NOT EXISTS family CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE family;

-- 确保表使用 utf8mb4 字符集
ALTER DATABASE family CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 设置默认字符集
SET NAMES utf8mb4;

-- 创建 regions 表
CREATE TABLE IF NOT EXISTS regions (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建 users 表
CREATE TABLE IF NOT EXISTS users (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    full_name VARCHAR(100) NOT NULL,
    region_id BIGINT UNSIGNED NOT NULL,
    mobile VARCHAR(20) NOT NULL UNIQUE,
    password VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    CONSTRAINT fk_users_region FOREIGN KEY (region_id) REFERENCES regions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 

-- 插入数据 regions 表

INSERT INTO regions (id, name) VALUES (1, '余北山');
INSERT INTO regions (id, name) VALUES (2, '余冲');
INSERT INTO regions (id, name) VALUES (3, '双庙关');
INSERT INTO regions (id, name) VALUES (4, '锡房湾');
INSERT INTO regions (id, name) VALUES (5, '长塘湾');
INSERT INTO regions (id, name) VALUES (6, '大塘湾');
INSERT INTO regions (id, name) VALUES (7, '板桥湾');
INSERT INTO regions (id, name) VALUES (8, '齐头山');
INSERT INTO regions (id, name) VALUES (9, '江西');
INSERT INTO regions (id, name) VALUES (10, '木厂河');
INSERT INTO regions (id, name) VALUES (11, '团风');
INSERT INTO regions (id, name) VALUES (12, '黄陂');
