CREATE TABLE IF NOT EXISTS parks (
    park_id CHAR(5) NOT NULL,
    park_name VARCHAR,
    lf_fence_distance SMALLINT UNSIGNED,
    lf_fence_height SMALLINT UNSIGNED,
    ct_fence_distance SMALLINT UNSIGNED,
    ct_fence_height SMALLINT UNSIGNED,
    rf_fence_distance SMALLINT UNSIGNED,
    rf_fence_height SMALLINT UNSIGNED,
    has_roof BIT,
    latitude FLOAT,
    longitude FLOAT,
    elevation SMALLINT,
    PRIMARY KEY (park_id)
);

CREATE TABLE IF NOT EXISTS teams (
    team CHAR(3) NOT NULL,
    park_id CHAR(5) NOT NULL,
    league CHAR(2) NOT NULL,
    city VARCHAR NOT NULL,
    team_name VARCHAR NOT NULL,
    FOREIGN KEY (park_id) REFERENCES parks(park_id),
    PRIMARY KEY (team)
);

CREATE TABLE IF NOT EXISTS games (
    id INTEGER NOT NULL,
    game_date date NOT NULL,
    game_number TINYINT UNSIGNED NOT NULL,
    day_of_week TINYINT UNSIGNED NOT NULL,
    length_in_outs TINYINT UNSIGNED NOT NULL,
    day_night BIT NOT NULL,
    completion_info VARCHAR,
    forfeit CHAR(1),
    protest CHAR(1),
    park_id CHAR(5) NOT NULL,
    attendence INT UNSIGNED NOT NULL,
    length_in_minutes INT UNSIGNED NOT NULL,
    home_plate_ump_id CHAR(8),
    home_plate_ump_name VARCHAR,
    b1_ump_id CHAR(8),
    b1_ump_name VARCHAR,
    b2_ump_id CHAR(8),
    b2_ump_name VARCHAR,
    b3_ump_id CHAR(8),
    b3_ump_name VARCHAR,
    lf_ump_id CHAR(8),
    lf_ump_name VARCHAR,
    rf_ump_id CHAR(8),
    rf_ump_name VARCHAR,
    FOREIGN KEY (park_id) REFERENCES parks(park_id)
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS team_game (
    game INTEGER NOT NULL,
    team CHAR(3) NOT NULL,
    game_num TINYINT UNSIGNED,
    score TINYINT UNSIGNED,
    line_score CHAR(9),
    win BIT,
    home BIT,

    at_bats TINYINT UNSIGNED,
    hits TINYINT UNSIGNED,
    doubles TINYINT UNSIGNED,
    triples TINYINT UNSIGNED,
    homeruns TINYINT UNSIGNED,
    rbis TINYINT UNSIGNED,
    sacrifice_hits TINYINT UNSIGNED,
    sacrifice_flies TINYINT UNSIGNED,
    hit_by_pitch TINYINT UNSIGNED,
    walks TINYINT UNSIGNED,
    intentional_walks TINYINT UNSIGNED,
    strikeouts TINYINT UNSIGNED,
    stolen_bases TINYINT UNSIGNED,
    caught_stealing TINYINT UNSIGNED,
    grounded_double TINYINT UNSIGNED,
    interference TINYINT UNSIGNED,
    left_on_base TINYINT UNSIGNED,
    pitchers_used TINYINT UNSIGNED,
    individual_earned_runs TINYINT UNSIGNED,
    earned_runs TINYINT UNSIGNED,
    wild_pitches TINYINT UNSIGNED,
    balks TINYINT UNSIGNED,
    putouts TINYINT UNSIGNED,
    assists TINYINT UNSIGNED,
    errors TINYINT UNSIGNED,
    passed TINYINT UNSIGNED,
    double_play TINYINT UNSIGNED,
    triple_play TINYINT UNSIGNED,

    manager_id CHAR(8),
    manager_name VARCHAR,

    starting_pitcher_id CHAR(8),
    starting_pitcher_name VARCHAR,

    starting_1_id CHAR(8),
    starting_1_name VARCHAR,
    starting_1_position TINYINT UNSIGNED,

    starting_2_id CHAR(8),
    starting_2_name VARCHAR,
    starting_2_position TINYINT UNSIGNED,

    starting_3_id CHAR(8),
    starting_3_name VARCHAR,
    starting_3_position TINYINT UNSIGNED,

    starting_4_id CHAR(8),
    starting_4_name VARCHAR,
    starting_4_position TINYINT UNSIGNED,

    starting_5_id CHAR(8),
    starting_5_name VARCHAR,
    starting_5_position TINYINT UNSIGNED,

    starting_6_id CHAR(8),
    starting_6_name VARCHAR,
    starting_6_position TINYINT UNSIGNED,

    starting_7_id CHAR(8),
    starting_7_name VARCHAR,
    starting_7_position TINYINT UNSIGNED,

    starting_8_id CHAR(8),
    starting_8_name VARCHAR,
    starting_8_position TINYINT UNSIGNED,

    starting_9_id CHAR(8),
    starting_9_name VARCHAR,
    starting_9_position TINYINT UNSIGNED,

    FOREIGN KEY (game) REFERENCES games(id),
    FOREIGN KEY (team) REFERENCES teams(team)
    PRIMARY KEY (game, team)
);

CREATE TABLE IF NOT EXISTS weather (
    game_id INTEGER NOT NULL,
    temperature FLOAT,
    humidity SMALLINT UNSIGNED,
    dew_point FLOAT,
    apparent_temperature FLOAT,
    air_pressure FLOAT,
    precipitation FLOAT,
    rain FLOAT,
    snowfall FLOAT,
    cloud_cover SMALLINT UNSIGNED,
    wind_speed FLOAT,
    wind_direction SMALLINT UNSIGNED,
    wind_gusts SMALLINT UNSIGNED,

    sun_rise TIME,
    sun_set TIME,
    moon_phase TINYINT UNSIGNED,
    FOREIGN KEY (game_id) REFERENCES games(id),
    PRIMARY KEY (game_id)
);
