CHAR field may only have certain values - CONTRAINT
How do I make a constraint in a table for a field (CHAR(20)) that it may only accept, for example, "car" or "bike".
CREATE TABLE Things (
id INTEGER NOT NULL,
thing CHAR(30) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT thingcheck CHECK (thing = "car" OR thing = "bike")
);
If I insert (1, "laptop") it still inserts! Any ideas?
Thanks very much for your time.
--开发者_Go百科- EDIT ---
As John pointed out, MySQL ignored CHECK clauses. Thanks!
how about:
ENUM('car', 'bike')
You could create a table that contains a list of the allowed values, and then set up a FOREIGN KEY
constraint against it. But, really, why not just use an ENUM
?
How about this,
CREATE TABLE Things (
id INTEGER NOT NULL,
thing CHAR(30) NOT NULL CHECK (thing = 'car' or thing='bike'),
PRIMARY KEY (id),
);
You should give the strings in single quote !!
精彩评论