0

I keep getting a syntax error even though I reviewed different formats that the CHECK constraint can be in. What am I doing wrong?

ALTER TABLE `eventsdataview`
ADD PitchArea CHAR(10) DEFAULT 'UNKNOWN' NOT NULL, CHECK (PitchArea IN ('Area1', 'Area2', 'Area3', 'Area4'));
ALTER TABLE `trackdataview`
ADD PitchArea CHAR(10) DEFAULT 'UNKNOWN' NOT NULL, CHECK (PitchArea IN ('Area1', 'Area2', 'Area3', 'Area4'));
2
  • 1
    What database are you using? The backticks suggest MySQL. If so, check constraints are not actually checked, so you can just drop them from the query. Commented Oct 21, 2019 at 12:07
  • DEFAULT 'UNKNOWN' NOT NULL looks a bit odd, as in ANSI/iSO SQL specs NULL actually means "unknown" Commented Oct 21, 2019 at 12:10

1 Answer 1

1

The correct syntax drops the comma, because you are trying to express an in-line constraint. So:

ALTER TABLE `eventsdataview`
    ADD PitchArea CHAR(10) DEFAULT 'UNKNOWN' NOT NULL
         CHECK (PitchArea IN ('Area1', 'Area2', 'Area3', 'Area4'));

As I mention in a comment, MySQL only very recently added support check constraints, so this may be for information purposes only. If you want to validate the values, use an enum or foreign key.

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

6 Comments

MySQL 8.0.16 finally added support for check constraints
@a_horse_with_no_name . . . I was hoping it would go into MySQL 8 from the get-go, so I wouldn't have to try to remember arcane versioning.
Yes, MySQL's policy to introduce new features with minor (patch) releases is really confusing.
"MySQL 8.0.16 finally added support for check constraints" @a_horse_with_no_name already "supported" CHECK for much longer but not in CREATE TABLE, you would need a create a updateble VIEW with check option..
@RaymondNijland: that's not a check constraint. The with check option only ensures that only the rows that the view returns can also be updated or deleted. That is something completely different
|

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.