如何将 XML 数据存储到 mysql 数据库中?我不想要像疯了一样的外键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2204261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I store XML data into a mysql database? I don't want foreign keys like crazy
提问by TIMEX
If my XML data is very complex, is there a way I can store this in DB?
如果我的 XML 数据非常复杂,是否可以将其存储在 DB 中?
回答by Pascal Thivent
The "regular" way is to store XML in a CLOB (Character Large Object) and MySQL supports CLOB with 4 data types:
“常规”方式是将 XML 存储在 CLOB(字符大对象)中,MySQL 支持具有 4 种数据类型的 CLOB:
- TINYTEXT - A CLOB column with a maximum length of 255 (2**8 - 1) characters.
- TEXT - A CLOB column with a maximum length of 65,535 (2**16 - 1) characters.
- MEDIUMTEXT - A CLOB column with a maximum length of 16,777,215 (2**24 - 1) characters.
- LONGTEXT - A CLOB column with a maximum length of 4,294,967,295 or 4GB (2**32 - 1) characters.
- TINYTEXT - 最大长度为 255 (2**8 - 1) 个字符的 CLOB 列。
- TEXT - 最大长度为 65,535 (2**16 - 1) 个字符的 CLOB 列。
- MEDIUMTEXT - 最大长度为 16,777,215 (2**24 - 1) 个字符的 CLOB 列。
- LONGTEXT - 最大长度为 4,294,967,295 或 4GB (2**32 - 1) 个字符的 CLOB 列。
Using one or the other depends on your needs.
使用其中一种取决于您的需要。
回答by knipknap
It all depends on what you want your database to do with the XML.
这完全取决于您希望数据库对 XML 做什么。
- If you just want to store the XML document for later retrieval, just use a blob or text field. Also check the MySQL docs.
- If you are trying to dump/import a model, use mysqldump.
- If you are planning to query on XML, you should probably be using a native XML database such as eXist-dbinstead.
回答by Andrew Brennan
I would recommend using a database that has a native XML datatype. Postgres will do this, for example. This will make life much easier for you if you are planning to lots of work with XML in your database.
我建议使用具有本机 XML 数据类型的数据库。例如,Postgres 会这样做。如果您计划在数据库中使用 XML 进行大量工作,这将使您的生活更加轻松。