简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM