add more check constraints

This commit is contained in:
Nick Griffey 2024-02-01 20:48:12 -06:00
parent 16ff759879
commit 0f99098de5
9 changed files with 36 additions and 8 deletions

View File

@ -53,7 +53,7 @@ create table if not exists "allstars" (
"year" text, "year" text,
"gameNum" text, "gameNum" text,
"team" text, "team" text,
"GP" numeric, "GP" numeric check ("GP" in (0, 1)),
primary key("player","year","gameNum"), primary key("player","year","gameNum"),
foreign key("year","gameNum") references "allstargames"("year","gameNum"), foreign key("year","gameNum") references "allstargames"("year","gameNum"),
foreign key("player","year","team") references "appearances"("player","year","team") foreign key("player","year","team") references "appearances"("player","year","team")

View File

@ -43,7 +43,7 @@ CREATE TABLE "allstarstartingpos" (
"year" NUMERIC, "year" NUMERIC,
"gameNum" NUMERIC, "gameNum" NUMERIC,
"league" NUMERIC, "league" NUMERIC,
"startingPos" NUMERIC, "startingPos" NUMERIC check ("startingPos" in (1,2,3,4,5,6,7,8,9,10)),
primary key("year","gameNum","league","startingPos"), primary key("year","gameNum","league","startingPos"),
unique("year","gameNum","league","startingPos"), unique("year","gameNum","league","startingPos"),
foreign key("player","year","gameNum") references "allstars"("player","year","gameNum"), foreign key("player","year","gameNum") references "allstars"("player","year","gameNum"),

View File

@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS "awardssharemanagers" (
"year" NUMERIC, "year" NUMERIC,
"league" TEXT, "league" TEXT,
"manager" TEXT, "manager" TEXT,
"pointsWon" NUMERIC check (pointsWon <= pointsMax), "pointsWon" NUMERIC check (pointsWon <= pointsMax),
"pointsMax" NUMERIC, "pointsMax" NUMERIC,
"votesFirst" NUMERIC, "votesFirst" NUMERIC,
PRIMARY KEY("manager","award","year","league"), PRIMARY KEY("manager","award","year","league"),

View File

@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS main."awardsshareplayers" (
"year" NUMERIC, "year" NUMERIC,
"league" TEXT, "league" TEXT,
"player" TEXT, "player" TEXT,
"pointsWon" NUMERIC check (pointsWon <= pointsMax), "pointsWon" NUMERIC check (pointsWon <= pointsMax),
"pointsMax" NUMERIC, "pointsMax" NUMERIC,
"votesFirst" NUMERIC, "votesFirst" NUMERIC,
PRIMARY KEY("award","year","player","league"), PRIMARY KEY("award","year","player","league"),

View File

@ -6,10 +6,14 @@ attach database 'baseball-raw.db' as 'raw';
create temp table franchises as create temp table franchises as
select * from "raw".teamsfranchises; select * from "raw".teamsfranchises;
update franchises
set active = 'N'
where active = 'NA';
CREATE TABLE IF NOT EXISTS main."franchises" ( CREATE TABLE IF NOT EXISTS main."franchises" (
"ID" TEXT, "ID" TEXT,
"name" TEXT, "name" TEXT,
"active" TEXT, "active" TEXT check ("active" in ('Y','N')),
"NAassoc" TEXT, "NAassoc" TEXT,
PRIMARY KEY("ID") PRIMARY KEY("ID")
); );

View File

@ -23,7 +23,7 @@ CREATE TABLE IF NOT EXISTS main."halloffame" (
"year" NUMERIC, "year" NUMERIC,
"votedBy" TEXT, "votedBy" TEXT,
"votes" NUMERIC, "votes" NUMERIC,
"inducted" TEXT, "inducted" TEXT check ("inducted" in ('Y','N')),
"category" TEXT, "category" TEXT,
PRIMARY KEY("player","year","votedBy") PRIMARY KEY("player","year","votedBy")
foreign key("player") references "people"("ID"), foreign key("player") references "people"("ID"),

View File

@ -14,11 +14,19 @@ insert into halloffamereqs
select distinct yearid, votedby, ballots, needed, "needed_note" select distinct yearid, votedby, ballots, needed, "needed_note"
from "raw".halloffame; from "raw".halloffame;
update halloffamereqs
set ballots = null
where ballots = "";
update halloffamereqs
set needed = null
where needed = "";
create table if not exists main."halloffamereqs" ( create table if not exists main."halloffamereqs" (
"year" numeric, "year" numeric,
"votedBy" text, "votedBy" text,
"ballots" numeric, "ballots" numeric,
"needed" numeric, "needed" numeric check ("needed" <= "ballots"),
"needed_note" text, "needed_note" text,
primary key("year","votedBy"), primary key("year","votedBy"),
foreign key("year") references "seasons"("year") foreign key("year") references "seasons"("year")

View File

@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS main."homegames" (
"year" NUMERIC, "year" NUMERIC,
"team" TEXT, "team" TEXT,
"park" TEXT, "park" TEXT,
"first" TEXT check(unixepoch("first") <= unixepoch("last")), "first" TEXT check(unixepoch("first") <= unixepoch("last")),
"last" TEXT, "last" TEXT,
"games" NUMERIC, "games" NUMERIC,
"openings" NUMERIC, "openings" NUMERIC,

View File

@ -14,6 +14,22 @@ update teamseasons
set teamid = 'PHP' set teamid = 'PHP'
where teamid = 'PH4' and franchid = 'PHQ'; where teamid = 'PH4' and franchid = 'PHQ';
update teamseasons
set divwin = null
where divwin = '';
update teamseasons
set wcwin = null
where wcwin = '';
update teamseasons
set lgwin = null
where lgwin = '';
update teamseasons
set wswin = null
where wswin = '';
alter table "teamseasons" drop column "franchID"; alter table "teamseasons" drop column "franchID";
create table if not exists main."teamseasons" ( create table if not exists main."teamseasons" (