stramin
stramin

Reputation: 2390

How to set more than 1 default value for the SET type in MySQL?

I have a column called 'responsable', this is a set type data which can have these values:

`responsable` SET('creator', 'owner', 'amdmin', 'dev', 'client', 'operator')

By default this field to select 'client' and 'operator', so I tried something like this:

CHANGE COLUMN `responsable` `responsable`
SET('creator', 'owner', 'amdmin', 'dev', 'client', 'operator')
NULL DEFAULT 'client','operator';

Which doesn't work (error near 'operator')

How to set more than 1 default value in set type?

Upvotes: 1

Views: 2159

Answers (1)

Paul Spiegel
Paul Spiegel

Reputation: 31772

A SET is represented by a single string value.

A SET is a string object that can have zero or more values, each of which must be chosen from a list of permitted values specified when the table is created. SET column values that consist of multiple set members are specified with members separated by commas (,). A consequence of this is that SET member values should not themselves contain commas.

Try this:

ALTER TABLE my_table CHANGE COLUMN `responsable` `responsable`
  SET('creator', 'owner', 'amdmin', 'dev', 'client', 'operator')
    NULL DEFAULT 'client,operator';

db-fiddle demo

However - I would advise to not use SET data type at all. Since it is nothing else but a comma delimited list, most disadvantages described here aplay to it. Consider to normalize the schema and use a separate table responsibilities.

For example: If a user with user_id = 253 has the responsibilities client and operator, you would have two entries in the responsibilities table:

user_id | responsibility
--------|---------------
    253 | client
    253 | operator

Upvotes: 1

Related Questions