SQL schema conventions for PSRDatabase. When writing SQL schemas for PSRDatabase, follow these conventions strictly.
Naming:
ThermalPlant, GaugingStation)Required structure:
id INTEGER PRIMARY KEY AUTOINCREMENTSTRICTConfiguration tableCREATE TABLE CollectionName (
id INTEGER PRIMARY KEY AUTOINCREMENT,
-- columns here
) STRICT;
Naming:
minimum_generation, some_example_value)Special columns:
label: Must be TEXT UNIQUE NOT NULLdate_* columns: Must be TEXT (mapped to DateTime)Relations to other collections:
targetcollection_relationtype (e.g., gaugingstation_id, plant_spill_to)INTEGER (never NOT NULL)ON UPDATE CASCADE ON DELETE CASCADEON UPDATE SET NULL ON DELETE CASCADECREATE TABLE Plant(
id INTEGER PRIMARY KEY AUTOINCREMENT,
label TEXT UNIQUE NOT NULL,
capacity REAL NOT NULL,
gaugingstation_id INTEGER,
plant_spill_to INTEGER,
FOREIGN KEY(gaugingstation_id) REFERENCES GaugingStation(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY(plant_spill_to) REFERENCES Plant(id) ON UPDATE SET NULL ON DELETE CASCADE
) STRICT;
Table naming: COLLECTION_vector_GROUPNAME
Required structure:
id INTEGER (references parent)vector_index INTEGER NOT NULL(id, vector_index)CREATE TABLE ThermalPlant_vector_costs(
id INTEGER,
vector_index INTEGER NOT NULL,
cost_value REAL NOT NULL,
FOREIGN KEY (id) REFERENCES ThermalPlant(id) ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY (id, vector_index)
) STRICT;
Table naming: COLLECTION_set_GROUPNAME
Required structure:
id INTEGER (references parent)CREATE TABLE HydroPlant_set_gaugingstations(
id INTEGER,
conversion_factor REAL NOT NULL,
gaugingstation_id INTEGER,
FOREIGN KEY (gaugingstation_id) REFERENCES GaugingStation(id) ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE (id, conversion_factor, gaugingstation_id)
) STRICT;
Table naming: COLLECTION_time_series_GROUPNAME
Required structure:
id INTEGER (references parent)date_time TEXT NOT NULL(id, date_time)CREATE TABLE Resource_time_series_generation (
id INTEGER,
date_time TEXT NOT NULL,
power_output REAL,
efficiency REAL,
FOREIGN KEY(id) REFERENCES Resource(id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id, date_time)
) STRICT;
Table naming: COLLECTION_time_series_files
Structure:
TEXTCREATE TABLE Plant_time_series_files (
generation TEXT,
cost TEXT
) STRICT;
| Element | Naming Pattern | Key Points |
|---|---|---|
| Collection | PascalCase singular | id INTEGER PRIMARY KEY AUTOINCREMENT, STRICT |
| Attribute | snake_case singular | label is TEXT UNIQUE NOT NULL |
| Relation | collection_relationtype | INTEGER, nullable, CASCADE |
| Vector table | Collection_vector_group | PK: (id, vector_index) |
| Set table | Collection_set_group | No PK, UNIQUE on all columns |
| Time series | Collection_time_series_group | PK: (id, date_time) |
| TS files | Collection_time_series_files | Columns store file paths |
Plants, right: Plant)STRICT at the endNOT NULLON DELETE SET NULL for non-self-referential relationsvector_index in vector tables