简体   繁体   中英

is it possible to change column datatype to SET in mysql?

i downloaded imdb database and while i was able to import it into mysql, i coulden't put the genre column datatype to "SET", even tho imdb only has 21 genre's for movies, when i imported the data i set the genre data type to VARCHAR(255), and i can not change it to SET, is it possible? here's what i tried:

ALTER TABLE movies MODIFY genre SET('Drama', 'Comedy', 'Action', 'Crime', 'Romance', 'Thriller', 'Adventure', 'Horror', 'Mystery', 'Fantasy', 'Sci-Fi', 'Biography', 'Family', 'Animation', 'Music', 'History', 'War', 'Sport', 'Musical', 'Western', 'Film-Noir') NOT NULL;

and i get the error code 1265 "Data truncated for column 'genre' at row 2", I'm guessing that's because row 2 has more than only one genre, but i thought that's the difference between ENUM data type in SET, am i missing something?

In MySQL, the SET data type requires no spaces between the values.

You said your string is:

'Biography, Crime, Drama'

This means the latter two strings have a preceding space:

'Biography',' Crime',' Drama'

Since you don't have ' Crime' or ' Drama' (prepended with spaces) as valid choices in your SET definition, the result is that no bit is set for them in your SET value.

So before you can alter your table with the SET data type you show, you need to remove spaces:

UPDATE movies SET genre = REPLACE(genre, ' ', '');

It's convenient in your case that there are no spaces in the string data that you want to keep. That is, no genre name contains an intentional space character.

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