Skip to content
Advertisement

Error Code: 3813. Column check constraint ‘customer_details_chk_2’ references other column

CREATE TABLE Customer_Details
(
    Customer_Id int(4) auto_increment,
    C_FirstName char(20),
    C_LastName char(20),
    PRIMARY KEY (Customer_Id),
    C_Temperature decimal(5,2),
    Mail_Id char(20) unique not null,
    Customer_Ref_Number char(10) , CONSTRAINT CHECK_UC
    CHECK(BINARY Customer_Ref_Number = UPPER(Customer_Ref_Number)),
    Processing_Cost int check(Processing_Cost>500),
    Service_Charge numeric check(.12*Processing_Cost>=Service_Charge)
);

Advertisement

Answer

MySQL only started supporting check constraints in MySQL 8.0.16 — released in April 2019.

Prior to that, the syntax was allowed (although perhaps with limitations), but nothing happened.

What is happening in your code is that you have:

Processing_Cost int check(Processing_Cost>500),
Service_Charge numeric check(.12*Processing_Cost>=Service_Charge)

These are inline check constraints. That means they are part of the column definition. You can fix this just by adding commas:

Processing_Cost int,
check (Processing_Cost > 500),
Service_Charge numeric,
check (0.12*Processing_Cost >= Service_Charge)

I like to make my constraints obvious, so I would put them after the columns and give them names:

Processing_Cost int,
Service_Charge numeric,
constraint chk_customer_details_processing_cost check (Processing_Cost > 500),
constraint chk_customer_details_service_charge check (0.12*Processing_Cost >= Service_Charge)

Here is a db<>fiddle.

Note: Don’t use char() for column types — it pads the string with spaces. Use varchar().

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