I have two tables: countries and regions.
CREATE TABLE Countries(
id SERIAL,
name VARCHAR(40) NOT NULL,
PRIMARY KEY(id)
)
CREATE TABLE Regions(
id SERIAL,
countryId SERIAL,
name VARCHAR(40) NOT NULL,
PRIMARY KEY(id ),
FOREIGN KEY(countryId) REFERENCES Countries(id)
)
When I insert into regions, I would hope that if I fail to mention countryId
, I would be stopped, however, countryId
is automatically incremented. Is there any way I can be stopped automatically to make this insertion?
Following table where I set countryID
as SERIAL NOT NULL
doesn't solve the issue.
CREATE TABLE Pigeons(
id SERIAL,
countryId SERIAL NOT NULL,
name VARCHAR(40) NOT NULL,
PRIMARY KEY(id ),
FOREIGN KEY(countryId) REFERENCES Countries(id)
)
The following solves the problem but I would think that it's technically incorrect because my serial could be bigger than 2^31, but int will never be >= 2^31.
CREATE TABLE Legions(
id SERIAL,
countryId INT NOT NULL,
name VARCHAR(40) NOT NULL
PRIMARY KEY(id ),
FOREIGN KEY(countryId) REFERENCES Countries(id)
)
What's the right approach here?