Skip to content
Advertisement

How would i restrict the company table to only allow each companyID to 10 countries?

This is my answer at this point, if this is correct can someone give a more in depth explanation as I am trying to teach myself SQL/ or point out how wrong I am

CREATE TABLE locations(
companyID INT NOT NULL UNIQUE CHECK (companyID Between 1 and 6),
countries CHAR(20),
primary key(countries),
FOREIGN KEY (companyID) REFERENCES company (companyID)
);

kind regards, new SQL user.

PS. I understand i can use a check constraint or a check trigger but I am trying to do this in CREATE TABLE.

Advertisement

Answer

CREATE TABLE locations(
companyID INT NOT NULL UNIQUE CHECK (companyID Between 1 and 6), countries CHAR(20),
primary key(countries),
CONSTRAINT ‘locations_companyID_foreign’ FOREIGN KEY (companyID) REFERENCES company (companyID) ON UPDATE RESTRICT ON DELETE RESTRICT
);

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement