October 24, 2016

MySQL about constraint

J.Doe’s Question:

Good afternoon,

Can anyone tell me what’s wrong with my code on PHP MY ADMIN, I’m trying to write a CONSTRAINT and create values for the car color in the beginning ( with table creation)

CREATE TABLE T_CAR
(CAR_ID                INTEGER       NOT NULL PRIMARY KEY,
 CAR_MARK            CHAR(32)      NOT NULL,
 CAR_MODEL            VARCHAR(16),
 CAR_NUMBER   CHAR(10)      NOT NULL,
 CAR_COLOR           CHAR(16)      CHECK (VALUE IN ('white', 'black', 'red', 'green', 'blue')))

The problem is with the last line (error message syntax not known).
Thanks in advance.

MySQL ignores check expression.

Manual: Create Table

The CHECK clause is parsed but ignored by all storage engines.

Try Enum:

CREATE TABLE T_CAR (
    CAR_ID INTEGER NOT NULL PRIMARY KEY,
    CAR_MARK CHAR(32) NOT NULL,
    CAR_MODEL VARCHAR(16),
    CAR_NUMBER CHAR(10) NOT NULL,
    CAR_COLOR ENUM('white', 'black', 'red', 'green', 'blue') NOT NULL
)

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!