什么数据类型最适合在SQL数据库中存储文章?

时间:2022-10-17 22:57:10

I'm creating a custom CMS for my site, and I'm going to store articles (Text and images per article) in a mysql database. what datatype is best suited for that task?

我正在为我的网站创建一个自定义CMS,我将在mysql数据库中存储文章(每篇文章的文本和图像)。什么数据类型最适合该任务?

6 个解决方案

#1


5  

You shouldn't try to store text and images in a single field, and in this circumstance you probably shouldn't store images in your database at all.

您不应该尝试将文本和图像存储在单个字段中,在这种情况下,您可能根本不应将图像存储在数据库中。

The best solution for this would be to use some kind of markup system in your articles - at it's simplest this could be a filtered subset of html - that is stored as plain text in your database and then parsed in the browser in some way. Obviously if you use filtered html you would not need to write any special code to parse it, but taking this approach does raise possible security issues.

对此最好的解决方案是在文章中使用某种标记系统 - 最简单的可能是html的过滤子集 - 在数据库中以纯文本形式存储,然后以某种方式在浏览器中进行解析。显然,如果您使用过滤后的html,则不需要编写任何特殊代码来解析它,但采用这种方法确实会引发可能的安全问题。

Other options to investigate include Markdown (the system used by this site) as well as BBCode (mainly used by online forums), as well as many others.

其他调查选项包括Markdown(本网站使用的系统)以及BBCode(主要由在线论坛使用),以及许多其他选项。

To summarise - don't store images and text in one field. Store text, and interpret that text to load images and other media in your articles as appropriate.

总结一下 - 不要将图像和文本存储在一个字段中。存储文本,并根据需要解释该文本以在文章中加载图像和其他媒体。

#2


1  

Save the images as VARCHAR, but only the image name (and/or location, depending on how big is your cms), save the text as - TEXT.

将图像保存为VARCHAR,但只保存图像名称(和/或位置,具体取决于您的cms有多大),将文本另存为 - TEXT。

#3


1  

In my oppinion use text for articles, containing HTML characters, thats what i use. But there are many other things to consider. It depends on what content of your articles. In your images its up to you, you will just store the path of the picture, unless you plan to store the picture itself.

在我的意见中使用包含HTML字符的文章的文本,这就是我使用的内容。但还有许多其他事情需要考虑。这取决于您的文章的内容。在您的图像中由您自己决定,您将只存储图片的路径,除非您打算存储图片本身。

#4


1  

Please see this article in which Microsoft insists the text datatype is going to disappear and therefore should not be used in new development. The alternatives are varchar and nvarchar for text and varbinary for images.

请参阅这篇文章,其中Microsoft坚持文本数据类型将消失,因此不应在新开发中使用。替代方案是text的varchar和nvarchar以及图像的varbinary。

#5


0  

Store images and text separately. Assuming you are trying to store the binary images in the database, I recommend storing images as a BLOB datatype and TEXT for the text.

分别存储图像和文本。假设您正在尝试将二进制映像存储在数据库中,我建议将映像存储为BLOB数据类型和文本的TEXT。

Don't use VARCHAR for article text because it does not dynamically expand past the size you make it.

不要将VARCHAR用于文章文本,因为它不会动态扩展超过您创建它的大小。

#6


0  

VARCHAR would be the best choice for storing text-articles as text and images data types would disappear in the future.

VARCHAR将是存储文本文章的最佳选择,因为文本和图像数据类型将来会消失。

For images you can store them in a folder similar to what you use for css/js and store its src value in the database. This would be efficient in terms of both speed and storage.

对于图像,您可以将它们存储在类似于用于css / js的文件夹中,并将其src值存储在数据库中。这在速度和存储方面都是有效的。

#1


5  

You shouldn't try to store text and images in a single field, and in this circumstance you probably shouldn't store images in your database at all.

您不应该尝试将文本和图像存储在单个字段中,在这种情况下,您可能根本不应将图像存储在数据库中。

The best solution for this would be to use some kind of markup system in your articles - at it's simplest this could be a filtered subset of html - that is stored as plain text in your database and then parsed in the browser in some way. Obviously if you use filtered html you would not need to write any special code to parse it, but taking this approach does raise possible security issues.

对此最好的解决方案是在文章中使用某种标记系统 - 最简单的可能是html的过滤子集 - 在数据库中以纯文本形式存储,然后以某种方式在浏览器中进行解析。显然,如果您使用过滤后的html,则不需要编写任何特殊代码来解析它,但采用这种方法确实会引发可能的安全问题。

Other options to investigate include Markdown (the system used by this site) as well as BBCode (mainly used by online forums), as well as many others.

其他调查选项包括Markdown(本网站使用的系统)以及BBCode(主要由在线论坛使用),以及许多其他选项。

To summarise - don't store images and text in one field. Store text, and interpret that text to load images and other media in your articles as appropriate.

总结一下 - 不要将图像和文本存储在一个字段中。存储文本,并根据需要解释该文本以在文章中加载图像和其他媒体。

#2


1  

Save the images as VARCHAR, but only the image name (and/or location, depending on how big is your cms), save the text as - TEXT.

将图像保存为VARCHAR,但只保存图像名称(和/或位置,具体取决于您的cms有多大),将文本另存为 - TEXT。

#3


1  

In my oppinion use text for articles, containing HTML characters, thats what i use. But there are many other things to consider. It depends on what content of your articles. In your images its up to you, you will just store the path of the picture, unless you plan to store the picture itself.

在我的意见中使用包含HTML字符的文章的文本,这就是我使用的内容。但还有许多其他事情需要考虑。这取决于您的文章的内容。在您的图像中由您自己决定,您将只存储图片的路径,除非您打算存储图片本身。

#4


1  

Please see this article in which Microsoft insists the text datatype is going to disappear and therefore should not be used in new development. The alternatives are varchar and nvarchar for text and varbinary for images.

请参阅这篇文章,其中Microsoft坚持文本数据类型将消失,因此不应在新开发中使用。替代方案是text的varchar和nvarchar以及图像的varbinary。

#5


0  

Store images and text separately. Assuming you are trying to store the binary images in the database, I recommend storing images as a BLOB datatype and TEXT for the text.

分别存储图像和文本。假设您正在尝试将二进制映像存储在数据库中,我建议将映像存储为BLOB数据类型和文本的TEXT。

Don't use VARCHAR for article text because it does not dynamically expand past the size you make it.

不要将VARCHAR用于文章文本,因为它不会动态扩展超过您创建它的大小。

#6


0  

VARCHAR would be the best choice for storing text-articles as text and images data types would disappear in the future.

VARCHAR将是存储文本文章的最佳选择,因为文本和图像数据类型将来会消失。

For images you can store them in a folder similar to what you use for css/js and store its src value in the database. This would be efficient in terms of both speed and storage.

对于图像,您可以将它们存储在类似于用于css / js的文件夹中,并将其src值存储在数据库中。这在速度和存储方面都是有效的。