How to insert a check constraint

95 views Asked by At

The aim is to insert a check constraint for the row titles, 'ppl_tshirt_size' that limits it to a few values (sm,med,lg,xl,xxl) but not an enum data type.

What am I not understanding? Additionally, what is the difference between using an enum data type vs check constraint? Which one is considered to be more mobile in your perspective?

Below are the two queries I constructed but were deemed incorrect:

ALTER TABLE 'people'
ADD CONSTRAINT chk_ppl_tshirt_size 
CHECK(pp _tshirt_size IN('sm', 'med', 'lg', 'xl', 'xxl');

When that failed, I decided to hardcode the entire thing hoping it was the GUI that perhaps messed up:

CREATE TABLE 'people.1'
(ppl_id VARCHAR (5),
ppl_country VARCHAR(5),
ppl_tshirt_size VARCHAR(5),
primary key(ppl_id),
CHECK(ppl_tshirt_size IN('sm','med','lg','xl','xxl')); 

The database is MariaDB 10.4.28.

1

There are 1 answers

0
danblack On

CHECK applies to the column:

CREATE TABLE `people.1`
(ppl_id VARCHAR (5),
ppl_country VARCHAR(5),
ppl_tshirt_size VARCHAR(5) CHECK(ppl_tshirt_size IN('sm','med','lg','xl','xxl')),
primary key(ppl_id)); 

dbfiddle: https://dbfiddle.uk/fEuIGjvR