Why does MySQL ignore the data type length of a column?
P粉798343415
P粉798343415 2024-03-28 11:13:09
0
1
369

I'm using MySQL 8.0 and I'm trying to store a URL in a column. Every time I try to insert a URL that is longer than 191 characters, I get the following error:

1406 Data too long for column 'url' at row 1

The column data type is VARCHAR(1000), and the URL is approximately 450 characters. I've tried inserting other strings and anything under 191 characters, and anything below 191 characters works fine, but nothing over 191 characters.

I tried changing the data type of this column to other string formats such as TEXT, LONGTEXT, and BLOB. I also tried inserting strings longer than 191 characters in other tables and they were also limited to 191 characters.

I know that utf8mb4 causes VARCHAR(255) which is actually 191, but shouldn't I override it with VARCHAR(1000)? Is there a setting in the database that could cause this issue?

P粉798343415
P粉798343415

reply all(1)
P粉002546490

Taken from here:

On a database that uses a utf8mb collation, each character takes 4 bytes.
On innodb, indexes (the prefixes to be exact) can contain up to 767 bytes.

So, the max indexable length on utf8mb + innodb is 767/4=191 characters.

So you'd better use TEXT format for your URL.

renew:

According to this answer, the problem is with the character set, when it needs to be utf8, it is utf8mb4, same collation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template