-1

I tried to find answer but no luck. I need to create constraint on a table A, that when column X value = 'id' and column Y value is already saved. In other words column Y must be unique only when column X='id'. I created function to check if exists and then tried that: "alter table A add constraint C CHECK(functionName(Y)=0)" but got

Error Code: 1901. Function or expression cannot be used in the CHECK clause

.

1
  • updated ur question with a similar fiddle hope it helps you Commented Aug 23, 2022 at 19:37

1 Answer 1

2

Wish you had a fiddle, try the constrain below on the create or alter table.

Constraint should be: UNIQUE (Y, (CASE WHEN X = 'idVal' THEN 'idVal' END))

Here is a Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

dbfiddle.uk/… adaptable to the OP question.
Code: alter table cc_fluentform_entry_details add constraint calliduniq UNIQUE (field_value, (CASE WHEN field_name = 'call_id' THEN 'call_id' END)); I am getting: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(CASE WHEN field_name = 'call_id' THEN 'call_id' END))' at line 1
Mind to create a fiddle and provide the link ?
Fiddle works. Mysql does not. Could it be restriction by wordpress?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.