Archive for the ‘MySQL import’ Category

How To: Import Data to MySQL from Text File

Июль 30th, 2010

When working with databases it is always necessary to import data or schemas. In this article we describe the process of importing data from a text file into a MySQL database, and also we discuss questions concerning problems with MySQL import and the ways of solving these problems. We will give a detailed description of the Data Import tool of dbForge Studio for MySQL, describe the capabilities of this tool and illustrate its usage.

What problems can be experienced when importing data from a text file?

To specify all problems one can experience when importing data from a text file it’s necessary to remember the specificity of storing text data:

  • data in text files is always formatted, and formatting is free;
  • tabular data in text files can be presented together with its header, i.e. with column names and certain formatting;
  • the type of data in text files can not be distinguished, that’s why there are problems with types conversion and their correct formatting;
  • if data in a file is presented not in the form of a grid, but as a free text, then it’s rather difficult to separate out the needed data block to find the correspondence with the needed column in the table.

MySQL import solution offered by Devart

Data Import functionality is widely presented in dbForge Studio for MySQL. The instrument allows to import data from CSV, Txt, Excel, Access, DBF, XML, ODBC formats. The capability of importing from ODBC will allow a user to migrate data from other database providers. In this article we will give a detailed description of the process of importing data from a text file.

Data in a text file can be:

  • stored as columns of fixed width;
  • separated by a delimiter;
  • of free format.

There are settings for each of these variants in Data Import Wizard of dbForge Studio for MySQL (Figure 1). As you can see on the image below, you can setup data splitting in a file by three ways:

  • manual – data is splitted manually. This mode allows you to place the splitting markers as you consider it necessary;
  • fixed width – splitting by columns of fixed width;
  • delimiter – splitting text in accordance with a delimiter symbol. In this case you can choose Tab, space or your own symbol as a delimiter (for example, it is “;” for CSV).
Data Import Wizard. Options for import data from text file

Figure 1. Options for import data from text file

Also on the Options page of dbForge Studio for MySQL Data Import Wizard you can select file encoding, set the quotation symbol for strings, choose the header position (on Figure 1 the header is marked with a blue line), the number of strings that should be skipped in the beginning of the file (on Figure 1 it is shown as a rectangle with red lines), and the possibility to import even lines (in our example we want to skip horizontal division of records with the “-” symbol).

It is necessary to state that when importing data from a text file all extra spaces will be removed from all values automatically. If you want to save some spaces in data, the quotation symbol specified on the Options page of Data Import Wizard will help you to do this. All data marked with quotation symbols will be imported in original form without cutting.

The result of splitting can be viewed on the next page of the wizard – the Mapping page (see Figure 2).

Data Import Wizard. Mapping splitted data to table columns

Figure 2. Mapping splitted data to table columns

This page allows to set the columns of the selected table to which the data received after splitting a text file should be imported. On the image we can see that the mapping was set automatically owing to the header with column names from the text file. But it is not always the case.

In case when automatic mapping did not manage to map columns correctly, you can choose the needed column from the splitted text file manually using a drop-down list in the upper columns list (as you can see on Figure 2).

You have such opportunity if data in text file could be splitted by a delimiter. But if splitting is performed manually or by columns of fixed width, the situation in this case is a bit different (see Figure 3).

Data Import Wizard. Mapping custom data to table columns

Figure 3. Mapping custom data to table columns

Of course you can perform mapping automatically using the Fill Mapping button (to perform this action correctly there should be a header before data), but to setup the mapping manually you should choose a column in the table and a block of data in the file preview window and press the Map button. As a result the position and size of the block from the file, data from which will be imported to the selected column, will be shown in the upper columns list.

As you can see from Figure 3, you can specify if a column is a key column in the upper list. This criterion will be used when working with modes (see the “Additional capabilities of data import in the dbForge Studio for MySQL” chapter of this article).

After setting the mapping of columns and data from a file, the wizard offers to setup data formats (see Figure 4). This point is exteremly important when working with a text file, because data formats are not specified there (see the “What problems can be experienced when importing data from a text file?” chapter).

Data Import Wizard. Common and columns data formats

Figure 4. Common and columns data formats

On the Data Formats page you are offered to set common formats (setting the null value, thousand separator, decimal separator, boolean values, and date and time) as well as setting data formats for each column (it is necessary if any column contains data stored in a format different from the common format). When starting MySQL import, data will be loaded into a table in a column format with the settings listed on this page regardless the fact that all data was textual.

Additional capabilities of MySQL import in dbForge Studio

Data Import Wizard offers additional capabilities of data import. These are: possibility to import data into an existing or a new table, import modes, handling errors that may occur during the import process, logging of data import process.

When importing data to an existing table one should keep in mind that this table may already contain data, and conflicts that may appear in case data in key columns is identical should be somehow resolved. For this dbForge Studio offers special modes:

  • Append. All records will be added to the table;
  • Update. All records keys of which match the keys specified on the Mapping page (see Figure 3) will be updated;
  • Append/Update. This mode combines two previous modes. In case a record exists in the table it will be updated, otherwise it will be added;
  • Delete. All matching records will be deleted;
  • Repopulate. This mode allows to delete all records existing in the table before importing data from the source.

In case of a new table Data Import Wizard will create a table with the columns available in the source. A user can always correct the names and types of the columns and their other settings with the help of Column Editor on the Mapping page (the Edit button on the toolbar).

To complete the description of Data Import Wizard we would like to describe the last page of the wizard that allows to write the import report to the log file and handle errors that may occur during the import process (see Figure 5).

Data Import Wizard. Errors handling and log settings

Figure 5. Errors handling and log settings

Errors handling consists of specifying how the application should behave when an error is encountered. It can: show a message asking about further actions, ignore all errors, or abort the import process at the first error.

All information concerning the import process with all errors that may have been encountered will be written to a log file if the corresponding option was checked. This option will help you to identify the problems and take necessary steps to resolve them.

Conclusion

In this article we tried to give a detailed description of Data Import tool available in dbForge Studio for MySQL on the example of a text file. For truth’s sake we should say that text file is one of the most difficult formats for data import, and that’s why we’ve written such a detailed description of the process of importing it. The import process of other formats has similar functionality, but with some additional specific options.


PlanetMySQL Voting: Vote UP / Vote DOWN

How To: Export/Import Large MySQL Database

Июль 23rd, 2010

MySQL is frequently referred to as a database for Web applications. Partially it is really so, because MySQL became popular owing to its simplicity, high speed, and bounding with PHP. Developers of small Web projects often choose MySQL as a back end of their sites. Does this mean that MySQL can be used only for small databases? Not at all. There are lots of databases size of data in which is measured in gigabytes. Besides MySQL servers are frequently clustered to increase their performance. When a DBAs work with large amounts of data, they frequently have to make backup copies correctly and effectively, i. e. to export MySQL databases to SQL (or MySQL backup). It is exteremely important to import MySQL database from SQL correctly is when restoring a corrupted database and when migrating a database from one server to another.

What should be taken into account when exporting a large MySQL database?

Making a backup copy of a large database takes a lot of time. During this time some user can try to modify data in this database. But we want to get an all-of-a-piece database snapshot. For different tables this can be done in different ways:

  • for InnoDB tables a separate transaction should be started
  • MyISAM tables should be locked by FLUSH TABLES WITH READ LOCK

Except taking a lot of time export and import of a MySQL database implies transferring a large amount of data between client and server. The most effective way of reducing the amount of data transferred through the net and the amount of the used disk space is compression. All points mentioned above are taken into account in the utility for making database backups included in the set of tools available in dbForge Studio for MySQL. The rest of the article is written referring to this utility.

Step-by-step MySQL backup procedure

1. Set compression for the connection

Using compression for a connection allows to reduce net traffic owing to strings compression. The Use Compression option is available on the Advanced page of the Database Connection Properties dialog. But if the amount of string data per record is not large, you won’t gain much this way.

2. Choose a database for export and open Database Backup Wizard

In the Database Explorer tree choose the nod of the needed database or connection. Choose Backup Database from the pop-up menu. On the image below we show the main settings of MySQL backup. Pay your attention to the fact that we are using compression to reduce the disk space usage. Besides using compression allows to add comments to the backup.

Database Backup Wizard - General Page

3. Set options to create an all-of-a-piece database snapshot

Earlier in this article we wrote about the necessity of avoiding data corruption while creating a database backup. There are options to achieve this, and they are marked red on the image below:

Database Backup Wizard - Options Page

4. Make a backup of your MySQL database

After performing all necessary settings press the Backup button and wait until the application finishes export. During the operation you will be able to see the progress of backup creation and its stages.

Step-by-step MySQL import procedure

So, we’ve performed database export and compressed the results into a ZIP-archive. And now we have to import this database on the new server. Will we have to decompress a large script and try to execute it manually? Not at all. Database Import can be performed neatly and easily with the help of Database Restore Wizard

1. Choose a database to import and open Database Restore Wizard

In the Database Explorer tree choose the nod of the needed database or connection. Choose Restore Database from the pop-up menu. Choose the backup file in the window that opened. After you’ve chosen the file you should see approximately the same as shown on the image below:

Database Restore Wizard

The program finds the *.sql file in the archive automatically and shows the comment we’ve added when creating the backup. Now we are ready to import our MySQL database.

2. Import the database

After pressing the Restore button the program will automatically restore the database from the backup.

Conclusion

In this article we’ve reviewed some aspects of making backups of MySQL databases and illustrated capabilities of dbForge Studio for MySQL regarding export/import. You can download dbForge Studio for MySQL here.


PlanetMySQL Voting: Vote UP / Vote DOWN