简体   繁体   中英

How to load column names, data from a text file into a MySQL 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...

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. Eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM