MySQL SETSET is a data type of String object that can hold zero or more, or any number of string values. They must be chosen from a predefined list of values specified during table creation. It is one of the rarely used data type in the MySQL database. It is referred to as a complex type because its implementation increases the complexity. The SET and ENUM data types work in a similar way because MySQL uses it with a predefined list of values. But one thing makes it different that are ENUM data type can hold only a single member of the predefined list of values, whereas SET data type allows us to store zero or any number of values together. If we define the SET column values with multiple set members, then it is required to separate it using the comma (,) operator. For example, we have defined a column as: Then, our column can have any of the following values: Storage of SET data typeA SET data type can store a maximum of 64 distinct members in its column. If the SET column contains duplicate values, it throws an error when the strict SQL mode is enabled. When we create a table and if the SET member values in the table definition have spaces, then they are automatically deleted from the table definition after the execution of the CREATE statement. The SET data type is stored as a numeric value within the MySQL tables. It occupies from one to eight( i.e., 1, 2, 3, 4, 8) bytes, which depends on the number of elements contains in a SET column. The following table explains the storage requirement for the SET data type in MySQL:
In MySQL, the SET element stored in the bitmap where each member represented by a single bit value. Let us look at the following table that illustrates it more clearly: In the above definition, each of the set members assigned a single bit that have following decimal and binary values:
Now, if we assign a value 9 to this column, in terms of binary, it will be 1001. So, it selects the first and fourth SET elements, and the resulting value is ''Java, PHP'', which will be the fifth member of the SET column. Sometimes our SET data type column has more than one element; in that case, there is no need to insert the value in a specific order. It means the element can be in any order. MySQL also allows us to insert the same element many times in the SET data type column. When we retrieved the value, it will return each element once based on the order in which they were listed during the time of table creation. Let us understand it with the help of following example where we are going to create a table named "myset_test" that contains a column SET('Java', 'Python', 'Android', 'PHP'): Next, we need to insert the values into the table: Now, when we retrieve the value from the table, we will get all these values to appear as "Java, PHP" so execute the below statement to see this: In the output, we can see that our SET column have only (Java, PHP) values: How to update SET dataMySQL SET data type can updates the data in three ways, which are given below: 1. By completely replacing the SET dataIf we want to replace the complete data into a SET, it is required to specify the new values. Execute the following statements where the first query updates row = 4 with values = 'Java, Python' and second query updates row = 5 with values = 11. Here, 11 means 8+2+1=Java, Python, and PHP. After the successful execution, execute the below command to verify the output: Output 2. By adding SET membersSometimes, there is a need to add the element into an existing SET column, then a CONCAT() or CONCAT_WS() function allows us to insert a new element in the comma-separated list. Execute this statement to understand it more clearly: After the successful execution, execute the below command to verify the output: Output 3. By removing SET membersIf you want to drop any element from the existing SET, then a REPLACE() function can be used to remove the element SET column. Execute this statement to understand it more clearly: Again, if you use the decimal value to remove the element from an existing set, use the bitwise AND(&) operator and bitwise NOT(~) operator. Execute the below statement: After the successful execution, execute the below command to verify the output: Output If we want to see the integer values corresponding to the SET member, then we need to add the "+0" with the column name. Let us see the following example: When we execute the above statement, the following result appears where integer values corresponding to the SET member included: Disadvantages of Using SET data typeThe SET data type does not recommend us to use it in our database because of the following disadvantages:
Why we use SET data typeThe following are reasons to use the SET data type in MySQL:
Next TopicMySQL Decimal |