Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update the members of a MySQL SET Type?

I have a table of values, with one of the columns being of type SET.

If it currently has the members ('a', 'b', 'c', 'd'), how do I add 'e' to the possible values?

I realize that using a SET type is a little strange, and I'm unclear why one would use it instead of a foreign key to another table with the values of the set, but I didn't design the database in question, and can't change it that much.

Thanks for your help!

UPDATE: I want to update the SET type for all rows, not just one, if that helps.

like image 300
Riddari Avatar asked Mar 17 '10 19:03

Riddari


People also ask

What are different set membership available in MySQL?

6 The SET Type. 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 ( , ).

Can you use set in MySQL?

The MySQL SET datatype allows us to compare multiple values without using complex JOIN operations. We can manipulate the set with binary functions to do complex comparisons by comparing bit values on a single column instead of comparing multiple rows of multiple tables.


1 Answers

To add an element to an existing SET use the CONCAT() function to add the new element to your comma separated list. To work with decimal values, we can use the bitwise OR operator |.

  UPDATE set_test SET myset = CONCAT(myset,",Travel")
      WHERE rowid = 3;

or

  UPDATE set_test SET myset = myset | 1 WHERE rowid = 3;

You can also use the CONCAT_WS() function, which handles list separators for us:

  UPDATE set_test SET myset = CONCAT_WS(',',myset,'Dancing')
      WHERE rowid = 6;
like image 54
northpole Avatar answered Oct 23 '22 20:10

northpole