Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

varchar vs text - MySQL

Tags:

php

mysql

xss

in my project an user can write comment [plain text], and view others comment, can delete own comment, but can not update comment !
In this case which would should i use ?

Text or Varchar(4048) ?
What is the advantage and disadvantage of Text and Varchar(large like 4000) ?
Is it secure enough if i replace only '<' with '& lt;' and '>' with '& gt;' to make sure everything is fine ?
[i dont want to convert all those like ' " & ..., to save space, i just want to make sure user can not write javascript]

There will be a limit on the front end

like image 853
Sourav Avatar asked Jan 25 '26 08:01

Sourav


2 Answers

Varchar is usually faster in retrieval when the size is reasonable, as it is stored within the table, where as TEXT is stored off the table with a pointer to location.

Thanks

like image 189
Just a PHP Programmer Avatar answered Jan 26 '26 22:01

Just a PHP Programmer


(You have multiple questions; I will address the one that is in the title.)

The only difference between VARCHAR(4000) and TEXT is that an INSERT will truncate to either 4000 characters or 65536 bytes, respectively.

For smaller values than 4000, there are cases where the temp table in a complex SELECT will run faster with, for example, VARCHAR(255) than TINYTEXT. For that reason, I feel that one should never use TINYTEXT.

like image 41
Rick James Avatar answered Jan 26 '26 22:01

Rick James