CREATE TABLE IF NOT EXISTS invitation_codes ( id BIGINT NOT NULL AUTO_INCREMENT, code_hash CHAR(64) NOT NULL, code_prefix VARCHAR(20) NOT NULL, max_uses INT UNSIGNED NOT NULL DEFAULT 1, used_count INT UNSIGNED NOT NULL DEFAULT 0, expires_at DATETIME NULL, status ENUM('active','revoked') NOT NULL DEFAULT 'active', note VARCHAR(255) NULL, created_by BIGINT NULL, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_invitation_codes_hash (code_hash), KEY idx_invitation_codes_created_at (created_at) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS invitation_code_usages ( id BIGINT NOT NULL AUTO_INCREMENT, invitation_code_id BIGINT NOT NULL, user_id BIGINT NOT NULL, email VARCHAR(100) NOT NULL, used_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_invitation_usage_code_user (invitation_code_id, user_id), KEY idx_invitation_usage_user (user_id), CONSTRAINT fk_invitation_usage_code FOREIGN KEY (invitation_code_id) REFERENCES invitation_codes(id), CONSTRAINT fk_invitation_usage_user FOREIGN KEY (user_id) REFERENCES users(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;