CREATE TABLE knowledge_items ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, tenant_id BIGINT UNSIGNED NOT NULL, scenario_id BIGINT UNSIGNED NOT NULL, item_key VARCHAR(64) NOT NULL, name VARCHAR(128) NOT NULL, type VARCHAR(64) NOT NULL, content JSON NOT NULL, status VARCHAR(24) NOT NULL DEFAULT 'active', sort_order INT NOT NULL DEFAULT 0, created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), PRIMARY KEY (id), UNIQUE KEY uk_knowledge_items_key (scenario_id, item_key), KEY idx_knowledge_items_type (tenant_id, scenario_id, type, status), CONSTRAINT fk_knowledge_items_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id), CONSTRAINT fk_knowledge_items_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE knowledge_relations ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, tenant_id BIGINT UNSIGNED NOT NULL, scenario_id BIGINT UNSIGNED NOT NULL, from_knowledge_id BIGINT UNSIGNED NOT NULL, relation_type VARCHAR(64) NOT NULL, to_knowledge_id BIGINT UNSIGNED NOT NULL, `condition` JSON NOT NULL, sort_order INT NOT NULL DEFAULT 0, created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), PRIMARY KEY (id), UNIQUE KEY uk_knowledge_relation (scenario_id, from_knowledge_id, relation_type, to_knowledge_id), KEY idx_knowledge_relation_from (tenant_id, scenario_id, from_knowledge_id, relation_type), CONSTRAINT fk_knowledge_relations_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id), CONSTRAINT fk_knowledge_relations_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id) ON DELETE CASCADE, CONSTRAINT fk_knowledge_relations_from FOREIGN KEY (from_knowledge_id) REFERENCES knowledge_items(id) ON DELETE CASCADE, CONSTRAINT fk_knowledge_relations_to FOREIGN KEY (to_knowledge_id) REFERENCES knowledge_items(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;