简体   繁体   English

我应该为 mysql db 选择什么数据类型来存储包含特殊字符的段落文本?

[英]What datatype should i choose for mysql db to store a paragraph text which includes special characters as well?

In my MySql DB I want to store some text which will be a mimimun of 1000 words, it will contain some special characters like "",'()<>-.;;在我的 MySql 数据库中,我想存储一些最少 1000 个单词的文本,其中将包含一些特殊字符,如"",'()<>-.;; data will be passed using php.数据将使用 php 传递。

VARCHAR columns are variable-length strings. VARCHAR 列是可变长度的字符串。 The length can be specified as a value from 0 to 65,535.长度可以指定为 0 到 65,535 之间的值。 The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns in a tabel) and the character set used. VARCHAR 的有效最大长度取决于最大行大小(65,535 字节,在表中的所有列之间共享)和使用的字符集。 VARCHAR is easier to use. VARCHAR 更容易使用。

TEXT data type can hold up to 64KB. TEXT 数据类型最多可容纳 64KB。 Don't forget to escape the ' character (as in \') in TEXT field data.不要忘记在 TEXT 字段数据中转义 ' 字符(如 \')。

you can use BLOB datatype to store your text.您可以使用BLOB数据类型来存储您的文本。 it can handle up to 64KB of data which is around 64000 characters.它可以处理多达 64KB 的数据,大约 64000 个字符。

There are several datatypes that hold text.有几种保存文本的数据类型。 When you declare any of them you can also specify their collation and character set.当您声明它们中的任何一个时,您还可以指定它们的排序规则和字符集。 For example:例如:

CREATE OR REPLACE TABLE mytable {
    mytext VARCHAR(2000) COLLATE utf8mb4_general_ci
)

This gives you a place to store up to 2000 characters of Unicode text.这使您可以存储多达 2000 个字符的 Unicode 文本。 Each Unicode character can take from 1 to 4 bytes of storage.每个 Unicode 字符可以占用 1 到 4 个字节的存储空间。 Unicode, as you know, handles many different human scripts, not to mention emojis and math symbols.如您所知,Unicode 可以处理许多不同的人类文字,更不用说表情符号和数学符号了。

To store text you have要存储您拥有的文本

  • the CHAR() data type if you know upfront exactly how many characters you must store. CHAR()数据类型,如果您预先知道必须存储多少个字符。
  • the VARCHAR() data type. VARCHAR()数据类型。 It has best performance especially if you want to filter on it.它具有最佳性能,特别是如果您想对其进行过滤。
  • the so-called LOB (large object) data types :所谓的LOB (大对象) 数据类型
    • LONGTEXT ... up to 4GiB in your text. LONGTEXT ...文本中最多 4GiB。 This is what WordPress uses for content, for what it's worth.这就是 WordPress 用于内容的,物有所值。
    • MEDIUMTEXT ... up to 16MiB中文本 ... 高达MEDIUMTEXT
    • TEXT ... up to 64KiB TEXT ... 高达 64KiB
    • TINYTEXT ... up to 255 bytes (useless, use VARCHAR(255) instead) TINYTEXT ... 最多 255 个字节(没用,改用VARCHAR(255)

These all have reasonable overhead in terms of disk space used.这些都在使用的磁盘空间方面具有合理的开销。

If you want to put indexes on VARCHAR() columns limit their size to VARCHAR(768) .如果要在VARCHAR()列上放置索引,请将它们的大小限制为VARCHAR(768)

Pro tip : Avoid the LOB data types unless you absolutely need them.专业提示:避免使用 LOB 数据类型,除非您绝对需要它们。 They make the server do extra work when you use them in SELECT or WHERE clauses.当您在 SELECT 或 WHERE 子句中使用它们时,它们会使服务器做额外的工作。 Use VARCHAR() instead.请改用VARCHAR()

Mediumtext has known bugs associated with inserting into a mediumtext field, so I would recommend staying away from that, as a paragraph is not enough to warrant it. Mediumtext 已知存在与插入 mediumtext 字段相关的错误,因此我建议远离它,因为段落不足以保证它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用什么数据类型在MySQL中为我的应用程序存储time()? - What datatype should I use to store time() in MySQL for my application? 我应该使用哪种数据类型在MySQL上存储较长的加密消息? - What datatype should I use to store long encrypted messages on MySQL? 我应该选择哪个版本的EntityFramework for MySQL? - Which version of EntityFramework for MySQL should I choose? 在 MySQL 中,我应该选择哪种排序规则? - In MySQL, which collation should I choose? 我应该使用哪种数据类型来以毫秒为单位存储 Days 小时和分钟? - Which datatype should I use to store Days hours and minutes in milliseconds? 在MySQL数据库中存储特殊字符的最佳方法是什么? - What is the best way to store special characters in a MySQL database? 我应该将电子邮件存储在数据库还是文本文件中? - Should I store emails in a DB or in a text file? 我应该在MySQL二进制数据类型列上使用哪个索引 - Which index should I use on binary datatype column mysql MySQL:在哪种情况下我应该选择MyIsam而不是innoDB? - MySQL: In which case should I choose MyIsam over innoDB? MySQL 与 PostgreSQL? 我应该为我的 Django 项目选择哪个? - MySQL vs PostgreSQL? Which should I choose for my Django project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM