CREATE TABLE Countries (
CountryCode CHAR(2) PRIMARY KEY,
CountryName VARCHAR(255),
);
CREATE TABLE Companies (
CompanyID SERIAL,
CountryCode CHAR(2) REFERENCES Countries (CountryCode),
-- etc.
);
CREATE TABLE Documents (
CountryCode CHAR(2) REFERENCES Countries (CountryCode),
DocumentID INT UNSIGNED,
DocumentName VARCHAR(255),
-- etc.
PRIMARY KEY (CountryCode, DocumentID)
);
CREATE TABLE CompanyDocuments (
CompanyID BIGINT UNSIGNED REFERENCES Companies (DocumentID),
CountryCode CHAR(2) REFERENCES Countries (CountryCode),
DocumentID INT UNSIGNED,
Expiry DATE,
PRIMARY KEY (CompanyID, DocumentID),
FOREIGN KEY (CountryCode, DocumentID) REFERENCES
Documents (CountryCode, DocumentID)
);