如何将文本文件中的列名、数据加载到 MySQL 表中?

发布于 2024-11-16 13:40:19 字数 202 浏览 8 评论 0 原文

我有一个包含很多列的数据集,我想将其导入 MySQL 数据库,因此我希望能够在不手动指定列标题的情况下创建表。相反,我想向(大概)MySQL CREATE TABLE 命令提供一个带有列标签的文件名。我在 Ubuntu 中使用标准 MySQL 查询浏览器工具,但我没有在创建表对话框中看到此选项,也无法弄清楚如何从 CREATE TABLE 文档页面编写查询来执行此操作。但一定有办法...

I have a dataset with a lot of columns I want to import into a MySQL database, so I want to be able to create tables without specifying the column headers by hand. Rather I want to supply a filename with the column labels in it to (presumably) the MySQL CREATE TABLE command. I'm using standard MySQL Query Browser tools in Ubuntu, but I didn't see in option for this in the create table dialog, nor could I figure out how to write a query to do this from the CREATE TABLE documentation page. But there must be a way...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2024-11-23 13:40:19

CREATE TABLE 语句不仅仅包括列名

  • 表名*
  • 列名*
  • 列数据类型*
  • 列约束,如 NOT NULL
  • 列选项,如 DEFAULT、字符集
  • 表约束,如 PRIMARY KEY* 和 FOREIGN KEY
  • 索引
  • 表选项,如存储引擎,默认字符集

* 强制

您无法仅从列名称列表中获得所有这些。您应该自己编写 CREATE TABLE 语句。


回复您的评论:许多软件开发框架支持在不使用 SQL DDL 的情况下声明表的方法。例如 Hibernate 使用 XML 文件。 YAML 受 Rails ActiveRecord、PHP Doctrine 和 Perl 的 SQLFairy 支持。可能还有其他工具使用其他格式(例如 JSON),但我一时不知道。

但最终,所有这些“简化”的接口学习起来并不比 SQL 简单,但无法准确表示 SQL 的作用。另请参阅泄漏抽象定律


请查看 SQLFairy,因为该工具可能已经以可以帮助您的方式从文件转换为 SQL。 FWIW MySQL Query Browser(或当前名称 MySQL Workbench)可以读取 SQL 文件。所以你可能不必复制 &手动粘贴。

A CREATE TABLE statement includes more than just column names

  • Table name*
  • Column names*
  • Column data types*
  • Column constraints, like NOT NULL
  • Column options, like DEFAULT, character set
  • Table constraints, like PRIMARY KEY* and FOREIGN KEY
  • Indexes
  • Table options, like storage engine, default character set

* mandatory

You can't get all this just from a list of column names. You should write the CREATE TABLE statement yourself.


Re your comment: Many software development frameworks support ways to declare tables without using SQL DDL. E.g. Hibernate uses XML files. YAML is supported by Rails ActiveRecord, PHP Doctrine and Perl's SQLFairy. There are probably other tools that use other format such as JSON, but I don't know one offhand.

But eventually, all these "simplified" interfaces are no less complex to learn as SQL, while failing to represent exactly what SQL does. See also The Law of Leaky Abstractions.


Check out SQLFairy, because that tool might already convert from files to SQL in a way that can help you. And FWIW MySQL Query Browser (or under its current name, MySQL Workbench) can read SQL files. So you probably don't have to copy & paste manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文