IberoMedia
IberoMedia

Reputation: 2304

mysql contrain column values to preset values

I would like to constrain the values of a column, a good analogy is the select form field element, where there is a list of values and only one of these can be selected.

I remember seeing a table's structure with something like this, this is how I became aware that this can be done.

Can anybody write up the mysql query to create a column that has this characteristic?

table name = some_table column name =some_column value 1 = value_1 value 2 = value_2

Once this table is created, what is the query I would use to choose one value or another

Thank you

Upvotes: 0

Views: 201

Answers (1)

Scott Presnell
Scott Presnell

Reputation: 1538

You can do mutually exclusive values with enum, and sets with multiple values using set. For example:

CREATE TABLE Car
(
   ID SMALLINT UNSIGNED,
   Model VARCHAR(40),
   Color ENUM('red', 'blue', 'green', 'yellow'),
   Options SET('power-doors', 'power-windows', 'automatic', 'disc-brakes')
 );

Queries work like normal queries.

Upvotes: 1

Related Questions