CREATE TABLE IF NOT EXISTS clubs ( club_serial BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, club_name VARCHAR(128) NOT NULL, club_status ENUM('active','archived') NOT NULL DEFAULT 'active', created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY (club_name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS club_memberships ( membership_serial BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, club_serial BIGINT UNSIGNED NOT NULL, user_serial BIGINT UNSIGNED NOT NULL, membership_status ENUM('active','inactive') NOT NULL DEFAULT 'active', joined_on DATE NOT NULL, UNIQUE KEY (club_serial,user_serial), UNIQUE KEY (membership_serial,club_serial), FOREIGN KEY (club_serial) REFERENCES clubs(club_serial) ON DELETE RESTRICT, FOREIGN KEY (user_serial) REFERENCES users(user_serial) ON DELETE RESTRICT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS club_seasons ( season_serial BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, club_serial BIGINT UNSIGNED NOT NULL, season_name VARCHAR(100) NOT NULL, starts_on DATE NOT NULL, ends_on DATE NOT NULL, UNIQUE KEY (club_serial,season_name), UNIQUE KEY (season_serial,club_serial), FOREIGN KEY (club_serial) REFERENCES clubs(club_serial) ON DELETE RESTRICT, CHECK (ends_on>=starts_on) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS club_dues ( due_serial BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, club_serial BIGINT UNSIGNED NOT NULL, membership_serial BIGINT UNSIGNED NOT NULL, season_serial BIGINT UNSIGNED NOT NULL, amount_cents BIGINT UNSIGNED NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY (membership_serial,season_serial), FOREIGN KEY (membership_serial,club_serial) REFERENCES club_memberships(membership_serial,club_serial) ON DELETE RESTRICT, FOREIGN KEY (season_serial,club_serial) REFERENCES club_seasons(season_serial,club_serial) ON DELETE RESTRICT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS club_payments ( payment_serial BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, due_serial BIGINT UNSIGNED NOT NULL, amount_cents BIGINT UNSIGNED NOT NULL, paid_on DATE NOT NULL, request_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL UNIQUE, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (due_serial) REFERENCES club_dues(due_serial) ON DELETE RESTRICT, CHECK (amount_cents>0) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;