CREATE TABLE IF NOT EXISTS topics ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, parent_id BIGINT UNSIGNED NULL, name VARCHAR(191) NOT NULL, sort_order INT NOT NULL DEFAULT 1000, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT fk_topics_parent FOREIGN KEY (parent_id) REFERENCES topics(id) ON DELETE CASCADE, KEY idx_topics_parent_sort (parent_id, sort_order, id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS photo_topics ( photo_id BIGINT UNSIGNED NOT NULL, topic_id BIGINT UNSIGNED NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (photo_id, topic_id), CONSTRAINT fk_photo_topics_photo FOREIGN KEY (photo_id) REFERENCES photos(id) ON DELETE CASCADE, CONSTRAINT fk_photo_topics_topic FOREIGN KEY (topic_id) REFERENCES topics(id) ON DELETE CASCADE, KEY idx_photo_topics_topic (topic_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;