varchar vs text-MySQL [英] varchar vs text - MySQL

查看:65
本文介绍了varchar vs text-MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,用户可以写评论[纯文本],并查看其他评论,可以删除自己的评论,但不能更新评论!
在这种情况下,我应该使用哪个?

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 ?

文本或Varchar(4048)吗?
Text和Varchar(大如4000)的优缺点是什么??
如果我仅替换'<'是否足够安全?与'& lt;'和'>'与'& gt;'确保一切正常?
[我不想转换所有类似'& ...的文件,以节省空间,我只想确保用户无法编写javascript]

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]

前端会有限制

推荐答案

要保护自己免受XSS攻击,请使用htmlentities函数对其进行编码.

To protect yourself from XSS attack, encode it using the htmlentities function.

除此之外,数据类型的选择与内容的大小有很大关系.如果它可能超过4048个字符,则使用文本数据类型.如果很多文章很大,使用text数据类型可能会减少浪费的数据空间,并且可能比巨型varchar稍好一些,但这取决于您的情况,您最好测试一下替代方法.

Other than that, the choice of datatype has most to do with how big the content will be. If it may exceed 4048 characters, then use a text datatype. If many posts will be large, using a text datatype may reduce wasted data space and may perform slightly better than a giant varchar, but it depends upon your situation, you would be best to test the alternatives.

我通常更喜欢varchar,因为从编码的角度来看,它很容易处理(如果没有其他问题),并且如果内容可能超过varchar的大小,则可以退回到文本.

I generally prefer varchar because it's easier to deal with from a coding perspective, if nothing else, and fall back to text if the contents may exceed the size of a varchar.

这篇关于varchar vs text-MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆