Here is my code.
CREATE TABLE CATEGORY
(
CategoryNo int IDENTITY(1,1) NOT NULL,
AutoIncCategoryNo AS 'CAT' + RIGHT('00000' + CAST(CategoryNo AS VARCHAR(5)),5) PERSISTED,
CategoryName varchar(100) NOT NULL,
PRIMARY KEY (AutoIncCategoryNo)
);
CREATE TABLE PRODUCT
(
ProductNo int IDENTITY(1,1) NOT NULL,
AutoIncProduct AS 'P' + RIGHT('00000' + CAST(ProductNo AS VARCHAR(5)),5) PERSISTED,
ProductName varchar(100) NOT NULL,
CategoryCode VARCHAR(5),
UnitPrice decimal(6,2),
PRIMARY KEY (AutoIncProduct),
CONSTRAINT CategoryCode_Constraint
FOREIGN KEY (CategoryCode)
REFERENCES CATEGORY(AutoIncCategoryNo)
);
I have 2 tables as shown above. And here is the error that I got.
Column 'CATEGORY.AutoIncCategoryNo' is not the same length or scale as referencing column 'PRODUCT.CategoryCode' in foreign key 'CategoryCode_Constraint'. Columns participating in a foreign key relationship must be defined with the same length and scale.
How can I fix it?