I have the following table:
DROP TABLE IF EXISTS employees;
CREATE TABLE cars (
model VARCHAR (50),
brand VARCHAR (50),
price INTEGER
);
Which looks like the following:
model brand price
Clio Renault 3000
Clio Renault 2700
Polo Volkswagen 4400
Golf Volkswagen 3400
I want to perform a CHECK (or other) operation to guarantee that a model cannot have multiple different brands. I am not sure how to insert information from another column when performing the check statement for model.
You can enforce your constraint with one additional step of normalization:
Now, the
PRIMARY KEYconstraint ofcar_modelenforces a single entry per model. And theFOREIGN KEYconstraint (with short notationREFERENCES ...) enforces the same combination ofmodelandbrandevery time.To get your original representation, you might add a
VIEWlike:You might do more. Like collect brands in a separate table ...