-- 创建资源标签关联表
-- 用于存储资源（my_resource）和标签（tag）之间的多对多关系

CREATE TABLE IF NOT EXISTS `my_resource_tag` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '关联ID',
  `resource_id` INT NOT NULL COMMENT '资源ID（关联my_resource表的resource_id）',
  `tag_id` INT NOT NULL COMMENT '标签ID（关联tag表的tag_id）',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `uk_resource_tag` (`resource_id`, `tag_id`) USING BTREE COMMENT '唯一索引：确保同一资源不会重复关联同一标签',
  KEY `idx_resource_id` (`resource_id`) USING BTREE COMMENT '索引：按资源ID查询标签',
  KEY `idx_tag_id` (`tag_id`) USING BTREE COMMENT '索引：按标签ID查询资源',
  CONSTRAINT `fk_resource_tag_resource` FOREIGN KEY (`resource_id`) REFERENCES `my_resource` (`resource_id`) ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_resource_tag_tag` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`tag_id`) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='资源标签关联表';

