有关MySQL中类型的问题 [英] Questions about types in MySQL

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

问题描述

因此,我正在创建一个用于评论的表,我在想.什么是评论详细信息的好类型?我放长文本.那么,如果长文本可以处理,为什么人们会需要varchar?另外,我要为用户名使用哪种类型?

So I was creating a table for comments, and I was wondering. What would be a good type for comment details? I put longtext. Well then, why would people need varchar if longtext can handle it? Also, which type would I want for usernames?

主要"索引的目的是什么?索引的目的是什么?

What is the purpose of "primary" for index? What is the purpose of index?

更新: 假设评论实际上是评论.

Update: Let's say a comment was actually a review.

推荐答案

的确,TEXT可以处理您要在VARCHAR或CHAR字段中输入的任何输入.实际上,TEXT可以处理和您可能希望将其放入DECIMAL,INT或几乎任何其他类型的数据.按照这种逻辑,我们最好将每一列都设为TEXT类型.

It is true that TEXT can handle any input you'd place in VARCHAR or CHAR field. In fact TEXT could handle and data you might want to put in DECIMAL, INT, or almost any other type as well. Following this logic we might as well make every column a TEXT type.

但这将是一个错误.为什么?因为为期望的输入使用适当的列类型可以使数据库更好地优化查询,使用更少的磁盘空间并使数据模型更易于理解和维护.

But this would be a mistake. Why? Because using the appropriate column type for the expected input allows the database to better optimize queries, uses less disk space and makes the data model easier to understand and maintain.

关于以下问题:用户名列应使用VARCHAR(20),因为您希望并期望大多数用户名都较短,通常不超过10至20个字符.对于评论专栏(例如电影评论或书评),TEXT类型是合适的,因为评论可以将单个段落跨越到多个页面.

In regards to the questions: a username column should use VARCHAR(20), since you would want and expect that most usernames are going to short, usually no more than 10 - 20 characters long. For a review column (like a movie review or book review) a TEXT type would be appropriate as reviews can span a single paragraph to several pages.

关于索引,请尝试以下链接:

In regards to indexes, try this link:

http://20bits.com/articles/interview-questions-database-索引/

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

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