add check constraints

This commit is contained in:
Nick Griffey 2024-02-04 14:59:16 -06:00
parent b67dec0630
commit 48dabfd691
3 changed files with 109 additions and 42 deletions

View File

@ -13,6 +13,18 @@ alter table batting drop column "teamID";
delete from batting delete from batting
where playerid = 'smithbu01' and yearid = 1911; where playerid = 'smithbu01' and yearid = 1911;
update batting
set
RBI = nullif(RBI,''),
SB = nullif(SB,''),
CS = nullif(CS,''),
SO = nullif(SO,''),
IBB = nullif(IBB,''),
HBP = nullif(HBP,''),
SH = nullif(SH,''),
SF = nullif(SF,''),
GIDP = nullif(GIDP,'');
CREATE TABLE IF NOT EXISTS main."batting" ( CREATE TABLE IF NOT EXISTS main."batting" (
"player" TEXT, "player" TEXT,
"year" NUMERIC, "year" NUMERIC,
@ -20,20 +32,20 @@ CREATE TABLE IF NOT EXISTS main."batting" (
"G" NUMERIC, "G" NUMERIC,
"AB" NUMERIC, "AB" NUMERIC,
"R" NUMERIC, "R" NUMERIC,
"H" NUMERIC, "H" NUMERIC check (H <= AB),
"2B" NUMERIC, "2B" NUMERIC check ("2B" <= H),
"3B" NUMERIC, "3B" NUMERIC check ("3B" <= H),
"HR" NUMERIC, "HR" NUMERIC check ("HR" <= H),
"RBI" NUMERIC, "RBI" NUMERIC,
"SB" NUMERIC, "SB" NUMERIC,
"CS" NUMERIC, "CS" NUMERIC,
"BB" NUMERIC, "BB" NUMERIC,
"SO" NUMERIC, "SO" NUMERIC check (SO <= AB),
"IBB" NUMERIC, "IBB" NUMERIC check (IBB <= BB),
"HBP" NUMERIC, "HBP" NUMERIC,
"SH" NUMERIC, "SH" NUMERIC,
"SF" NUMERIC, "SF" NUMERIC,
"GIDP" NUMERIC, "GIDP" NUMERIC check (GIDP <= AB),
PRIMARY KEY("player","year","stint"), PRIMARY KEY("player","year","stint"),
foreign key("year","player","stint") references "playerstints"("year","player","stint") foreign key("year","player","stint") references "playerstints"("year","player","stint")
); );

View File

@ -9,6 +9,18 @@ select * from "raw".battingpost;
alter table battingpost drop column "lgID"; alter table battingpost drop column "lgID";
alter table battingpost drop column "teamID"; alter table battingpost drop column "teamID";
update battingpost
set
RBI = nullif(RBI,''),
SB = nullif(SB,''),
CS = nullif(CS,''),
SO = nullif(SO,''),
IBB = nullif(IBB,''),
HBP = nullif(HBP,''),
SH = nullif(SH,''),
SF = nullif(SF,''),
GIDP = nullif(GIDP,'');
CREATE TABLE IF NOT EXISTS main."battingpost" ( CREATE TABLE IF NOT EXISTS main."battingpost" (
"year" NUMERIC, "year" NUMERIC,
"round" NUMERIC, "round" NUMERIC,
@ -16,20 +28,20 @@ CREATE TABLE IF NOT EXISTS main."battingpost" (
"G" NUMERIC, "G" NUMERIC,
"AB" NUMERIC, "AB" NUMERIC,
"R" NUMERIC, "R" NUMERIC,
"H" NUMERIC, "H" NUMERIC check (H <= AB),
"2B" NUMERIC, "2B" NUMERIC check ("2B" <= H),
"3B" NUMERIC, "3B" NUMERIC check ("3B" <= H),
"HR" NUMERIC, "HR" NUMERIC check ("HR" <= H),
"RBI" NUMERIC, "RBI" NUMERIC,
"SB" NUMERIC, "SB" NUMERIC,
"CS" NUMERIC, "CS" NUMERIC,
"BB" NUMERIC, "BB" NUMERIC,
"SO" NUMERIC, "SO" NUMERIC check (SO <= AB),
"IBB" NUMERIC, "IBB" NUMERIC check (IBB <= BB),
"HBP" NUMERIC, "HBP" NUMERIC,
"SH" NUMERIC, "SH" NUMERIC,
"SF" NUMERIC, "SF" NUMERIC,
"GIDP" NUMERIC, "GIDP" NUMERIC check (GIDP <= AB),
PRIMARY KEY("year","player","round"), PRIMARY KEY("year","player","round"),
foreign key("player","year") references "appearancespost"("player","year"), foreign key("player","year") references "appearancespost"("player","year"),
foreign key("year","round") references "seriespost"("year","round") foreign key("year","round") references "seriespost"("year","round")

View File

@ -14,21 +14,64 @@ update teamseasons
set teamid = 'PHP' set teamid = 'PHP'
where teamid = 'PH4' and franchid = 'PHQ'; where teamid = 'PH4' and franchid = 'PHQ';
-- set game totals from bbref
update teamseasons update teamseasons
set divwin = null set G = 160
where divwin = ''; where yearid = 1979 and teamid = 'CHA';
update teamseasons update teamseasons
set wcwin = null set G = 161
where wcwin = ''; where yearid = 1979 and teamid = 'DET';
update teamseasons update teamseasons
set lgwin = null set G = 132
where lgwin = ''; where yearid = 1897 and teamid = 'CL4';
update teamseasons update teamseasons
set wswin = null set G = 155
where wswin = ''; where yearid = 1892 and teamid = 'PIT';
update teamseasons
set G = 18
where yearid = 1884 and teamid = 'WIL';
update teamseasons
set G = 105
where yearid = 1884 and teamid = 'CNU';
update teamseasons
set G = 80
where yearid = 1882 and teamid = 'SL4';
update teamseasons
set G = 80
where yearid = 1882 and teamid = 'LS2';
update teamseasons
set
G = nullif(G, ''),
Ghome = nullif(Ghome, ''),
W = nullif(W, ''),
L = nullif(L, ''),
divwin = nullif(divwin,''),
wcwin = nullif(wcwin,''),
lgwin = nullif(lgwin,''),
wswin = nullif(wswin,''),
CG = nullif(CG, ''),
SHO = nullif(SHO, ''),
SV = nullif(SV, ''),
IPouts = nullif(IPouts, ''),
H = nullif(H, ''),
"2B" = nullif("2B", ''),
"3B" = nullif("3B", ''),
HR = nullif(HR, ''),
ER = nullif(ER, ''),
HR = nullif(HR, ''),
BB = nullif(BB, ''),
SO = nullif(SO, ''),
HBP = nullif(HBP, ''),
R = nullif(R, ''),
SF = nullif(SF, '');
alter table "teamseasons" drop column "franchID"; alter table "teamseasons" drop column "franchID";
@ -38,39 +81,39 @@ create table if not exists main."teamseasons" (
"team" TEXT, "team" TEXT,
"division" TEXT, "division" TEXT,
"Rank" NUMERIC, "Rank" NUMERIC,
"G" NUMERIC, "G" NUMERIC check (W + L <= G),
"Ghome" NUMERIC, "Ghome" NUMERIC check (GHome <= G),
"W" NUMERIC, "W" NUMERIC check (W <= G),
"L" NUMERIC, "L" NUMERIC check (L <= G),
"DivWin" NUMERIC, "DivWin" NUMERIC check (divwin in ('Y','N')),
"WCWin" NUMERIC, "WCWin" NUMERIC check (wcwin in ('Y','N')),
"LgWin" NUMERIC, "LgWin" NUMERIC check (lgwin in ('Y','N')),
"WSWin" NUMERIC, "WSWin" NUMERIC check (wswin in ('Y','N')),
"R" NUMERIC, "R" NUMERIC,
"AB" NUMERIC, "AB" NUMERIC,
"H" NUMERIC, "H" NUMERIC check (H <= AB),
"2B" NUMERIC, "2B" NUMERIC check ("2B" <= H),
"3B" NUMERIC, "3B" NUMERIC check ("3B" <= H),
"HR" NUMERIC, "HR" NUMERIC check (HR <= H),
"BB" NUMERIC, "BB" NUMERIC,
"SO" NUMERIC, "SO" NUMERIC check (SO <= AB),
"SB" NUMERIC, "SB" NUMERIC,
"CS" NUMERIC, "CS" NUMERIC,
"HBP" NUMERIC, "HBP" NUMERIC,
"SF" NUMERIC, "SF" NUMERIC,
"RA" NUMERIC, "RA" NUMERIC,
"ER" NUMERIC, "ER" NUMERIC check (ER <= RA),
"ERA" NUMERIC, "ERA" NUMERIC,
"CG" NUMERIC, "CG" NUMERIC check (CG <= G),
"SHO" NUMERIC, "SHO" NUMERIC check (SHO <= G),
"SV" NUMERIC, "SV" NUMERIC check (SV <= G),
"IPouts" NUMERIC, "IPouts" NUMERIC,
"HA" NUMERIC, "HA" NUMERIC,
"HRA" NUMERIC, "HRA" NUMERIC check (HRA <= HA),
"BBA" NUMERIC, "BBA" NUMERIC,
"SOA" NUMERIC, "SOA" NUMERIC check (SOA <= IPouts),
"E" NUMERIC, "E" NUMERIC,
"DP" NUMERIC, "DP" NUMERIC check (2 * DP <= IPouts),
"FP" NUMERIC, "FP" NUMERIC,
"name" NUMERIC, "name" NUMERIC,
"park" NUMERIC, "park" NUMERIC,