Search code examples
mysqlsqlvarchar

What happens when you store a value in a VARCHAR, which is over the limit in SQL?


If I setup a varchar(255) column will there be an error if I try to insert more than 255 characters or will it concatenate the inserted value to the first 255 characters?


Solution

  • It depends on server configuration:

    If strict SQL mode is not enabled and you assign a value to a CHAR or VARCHAR column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. For truncation of nonspace characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode.

    More info here: The CHAR and VARCHAR Types