But i still don't like that "CapitalID" in "Countries"-Table.
Kind of like a Foreign Key pointing back to Cities...
Haha, I kind of like it like that.
Because with a Capital flag (boolean) you can accidentally get multiple capitals per country (or you need to set a constraint on that).
Plus it takes more space (a INTEGER per City) while having a foreign key pointing back is just an INTEGER per country.
Imagine the real tables have millions of entries (because this was just an example).
In my eyes there is nothing wrong with pointing back to cities inside country, especially because it's just ONE field/city specific per country.
Of course... you could also normalize this further and make a third table CAPITALS.
CREATE TABLE CAPITALS
ID INTEGER PRIMARY KEY,
Country_ID INTEGER NOT NULL,
City_ID INTEGER NOT NULL,
FOREIGN KEY (Country_ID) REFERENCES Countries(ID) ON DELETE CASCADE,
FOREIGN KEY (City_ID) REFERENCES Cities(ID) ON DELETE CASCADE
);
BTW ON UPDATE CASCADE shouldn't be needed for ID foreign keys because ID can't (or shouldn't) change, you only use those for CHAR fields
But to do this further normalization, for just one field/entity, I find that a bit of overkill
Nevermind, how the Queries behave, if CapitalID is NULL.....
Remember: TS has the Countries/Cities only as a Scenario
I have the same similar constructions in my own CRM and because that field is just one field, being NULL doesn't matter in the country table when joining (same as some other field being NULL).
But yes, you always need to mind NULL values, especially in JOINs... but that kind of becomes second nature