Aman
Aman

Reputation: 56

My Sql Query to check country name is restricted

CREATE TABLE
IF NOT EXISTS countrie (
    Country_id VARCHAR (30) NOT NULL,
    Country_name VARCHAR (40) CHECK (
        Country_name IN ('Italy', 'India', 'China')
    ),
    region_id DECIMAL (10, 2) NOT NULL
);

I have written this query where user will not be able to enter country name except Italy,India & China.But I am able to enter other country into my table. Please guide me on this query.

Upvotes: 0

Views: 566

Answers (2)

Dipanwita Kundu
Dipanwita Kundu

Reputation: 1667

Set the Country_name enum with value ('Italy','India','China')

Upvotes: 1

Nirpendra Patel
Nirpendra Patel

Reputation: 679

The CHECK clause is parsed but ignored by all storage engines. See Section 12.1.17, “CREATE TABLE Syntax”. The reason for accepting but ignoring syntax clauses is for compatibility, to make it easier to port code from other SQL servers, and to run applications that create tables with references. See Section 1.8.5, “MySQL Differences from Standard SQL”.

try using trigger . more answers in here : CHECK constraint in MySQL is not working

Upvotes: 0

Related Questions