imetting_backend/sql/migrations/create_user_logs_table.sql

36 lines
1.4 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- ============================================
-- 创建用户日志表 (user_logs)
-- 创建时间: 2025-01-26
-- 说明: 用于记录用户活动日志,包括登录、登出等操作
-- 支持查询用户最后登录时间等统计信息
-- ============================================
-- 创建 user_logs 表
CREATE TABLE IF NOT EXISTS user_logs (
log_id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '日志ID',
user_id INT(11) NOT NULL COMMENT '用户ID',
action_type VARCHAR(50) NOT NULL COMMENT '操作类型: login, logout, etc.',
ip_address VARCHAR(50) DEFAULT NULL COMMENT '用户IP地址',
user_agent TEXT DEFAULT NULL COMMENT '用户代理字符串(浏览器/设备信息)',
metadata JSON DEFAULT NULL COMMENT '额外的元数据JSON格式',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '日志创建时间',
-- 索引
INDEX idx_user_id (user_id),
INDEX idx_action_type (action_type),
INDEX idx_created_at (created_at),
INDEX idx_user_action (user_id, action_type),
-- 外键约束
CONSTRAINT fk_user_logs_user_id FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户活动日志表';
-- ============================================
-- 验证创建
-- ============================================
-- 查看表结构
-- DESCRIBE user_logs;
-- 查看索引
-- SHOW INDEX FROM user_logs;