tutorial exporting and importing data between mysql and microsoft excel

Upload: afifchoerudin

Post on 07-Mar-2016

247 views

Category:

Documents


0 download

DESCRIPTION

Tutorial Exporting and Importing Data Between MySQL and Microsoft Excel

TRANSCRIPT

  • White Source - Manage Open Source Risk- Sign Up Now

    Exporting and importing data between MySQL and Microsoft excel has

    average rating 7 out of 10. Total 93 users rated.

    Installing and Configuring

    Import and export data between MySQL and Excel has become much easier inExcel 2007 than it's previous versions. To explore the feature, you need to installMySQL For Excel.

    Advertise Here

    Featured Tutorials: SQL JOINS, Introduction to DOM, Jquery, HTML5 Canvas Tutorial, Bootstrap 3.0.0, PHPExercises, JavaScript Exercises

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    1 of 20 19/04/2014 2:57

  • You can install the MySQL For Excel component while installing MySQLCommunity Server 6. Or you may install it as an add on upon your existingMySQL Server installation. Installation of this component has some prerequisitesthough.

    MySQL for Excel RequirementsYou must have following installed on your machine for installing MySQL For ExcelInstaller :

    .NET Framework 4.0 (Client or Full Profile).

    Microsoft Office Excel 2007 or greater, for Microsoft Windows.

    Visual Studio Tools for Office 4.0, and MySQL Installer may install this for you.

    An available MySQL Server connection.

    MySQL for Excel is loaded and executed by selecting the Data menu tab in Excel,and then choosing the "MySQL for Excel" Database icon. This opens a new Excelsidebar with the available MySQL for Excel options. The navigation bar with theMySQL for Excel icon is shown in the following screen shot:

    Edit MySQL Data in Excel

    MySQL for Excel enables you to load and edit MySQL data directly from MicrosoftExcel., or yon can done it manually by pressing Commit Changes.

    The example below uses the location table of the example employee database,but the screen will look the same for any table. Within MySQL for Excel, Open aMySQL Connection, click the employee schema, Next, select the location table,click Edit MySQL Data, then choose Import to import the data into a new MicrosoftExcel worksheet for editing.

    Here is the step by step guide to edit and commit the data:

    Step 1 :

    Load Microsoft Office Excel 7

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    2 of 20 19/04/2014 2:57

  • Step 2 :

    Click on Data Tab, see the above picture, the" MySQL for Excel" Database iconwill appear shown below.

    Step 3 :

    Click on "MySQL for Excel" Database icon. It opens a new Excel sidebar with theavailable MySQL for Excel options. The navigation bar with the MySQL for Excelicon is shown in the following picture.:

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    3 of 20 19/04/2014 2:57

  • Here our Database is employee and we are working with location table, but thescreen will look the same for any table.

    Step 4 :

    Within MySQL for Excel sidebar open a MySQL connection by double clicking.Here our connection is Local instance MySQL5.6 and with following screen willappear for accepting the password.

    Step 5 :

    Enter password for connection with MySQL server. The databases will show inMySQL for Excel sidebar. Our Database is employee. See the following picture.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    4 of 20 19/04/2014 2:57

  • Step 6 :

    Double click on your desire database, and the tables within the database willdisplay. Our table is location. See the following picture.

    Step 7 :

    Select the table which you want to edit, click "Edit MySQL Data" inside thenavigation bar as shown above, and see the following screen shot.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    5 of 20 19/04/2014 2:57

  • Step 8 :

    Click on Import button as mention in the above picture, and watch the followingscreen shot. The data of the selected table will appear and if you place the cursorwithin the data range the Revert Data and Commit Changes button ( specified bya red color rectangle ) will appear other wise not.

    Step 9 :

    Here in our table the first column ID is a primary key. If we change the value of IDin A19 cell 17 instead of 18 and press enter key the color of this cell will be green.Now we have changed the value of F16 cell is London and press enter key thecell color will change in to green, and the color yellow at the last, indicate that thecells that accept new data. Data entered here is inserted into the MySQL table.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    6 of 20 19/04/2014 2:57

  • Step 10 :

    Now if we click the Revert Data button, watch the following screen shot.

    Here in above picture if you click the Refresh Data from DB the data will be refresh anddisplay the original data from DB, and if you click on Revert Changed Data, you willloose your changes just made. In this stage if you click on Commit Changes button lookthe following screen shot.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    7 of 20 19/04/2014 2:57

  • Here in the above picture you are looking an error message, and the color of A19cell changed from green to red, that is because the ID column is primary key andthe uniqueness have been violated here. Now we returned the value of A19 cell inits original value, i.e. 18, and click on Commit Changes button, and now look thebelow screen shot.

    In the above picture shows the color of cell F16 have converted from green toblue, that means Commit Changes done successfully. Now you can close theExcel window saving or not but the database table have been updated. You cansee it to do the step again. If you checked the Auto Commit check box, effectimmediately be seen in your sheet and data will be updated in MySQL database.

    Import MySQL Data into Excel

    Data can be imported from MySQL into a Microsoft Excel spreadsheet by usingthe Import MySQL Data option after selecting either a table, view, or procedure toimport.

    First of all you do the first 6 steps describe above in "Edit MySQL Data in Excel"then select your table which you want to import. Here our table is location. Soselect the location table and then click "Import MySQL Data" and look theappeared screen shot here in the below.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    8 of 20 19/04/2014 2:57

  • Choosing columns to importBy default, all columns are selected and will be imported. Specific columns maybe selected (or unselected) using the standard Microsoft Windows method ofeither Control + Mouse click to select the individual columns, or Shift + Mouseclick to select a range of columns.

    The background color white indicates, the column or columns have been selectedand they are ready to be imported, on the other hand the gray color indicates thatthe columns are not selected and the column will not be imported.

    Right-clicking anywhere in the preview grid opens a context-menu with eithera Select None or Select All option, depending on the current status.

    Importing TableInclude Column Names as Headers: By default this option is enabled, and thistreats the column names at the top of the Microsoft Excel spreadsheet as a"headers" row and will be insert as a header.

    Limit to and Rows and Start with Row : By default this option is disabled , ifenabled, this limits the range of imported data. The Limit to option defaults to 1,and this limit can be changed by defines the number of rows to import. The Startwith Row option defaults to 1, i.e. starting from first row, and it can be changed bydefines the number from where the import begins. Each option has a maximumvalue of COUNT(rows) in the table.

    Now assumed that we want to import the columns LOATION_ID and CITY. Clickthe mouse on LOCATION_ID column and then press and hold CTRL key andclick on CITY column, and look the following screen shot.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    9 of 20 19/04/2014 2:57

  • Now if we click the Import button all the rows for this two columns will be import inMicrosoft Excel Worksheet.

    Assumes that, we want to import only 6 rows beginning from 3rd row. Now lookthe following screen shot.

    Here in the above picture shows all the columns have selected and the value ofLimit to is 6, that means number of 6 rows will be imported and the beginning ofimporting will be start from 3rd row, because we have set the value of Start withRow is 3. Now click the Import button and look the following screen shot.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    10 of 20 19/04/2014 2:57

  • Append Excel Data into MySQL

    The Microsoft Excel spreadsheet data can be appended to a MySQL databasetable by using the Append Excel Data to Table option.

    Column mappingsMapping the Excel columns to the MySQL columns can be executedautomatically (default), manually, or by using a stored mapping routine. To appendthe data from Excel to MySQL the following step to be followed:

    At first do the above 6 steps of "Edit MySQL Data in Excel" then input records inexcel spreadsheet compatible to the structure of MySQL table in which you wantto append the records. Here is the example in which we have taken only one row,you may take more than one rows and then select the records. See the followingscreen shot.

    Now click on the Append Excel Data to Table as mention above, and look thescreen like.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    11 of 20 19/04/2014 2:57

  • Here in the above picture you are looking two colors for the legend. One is Redand another is Green.

    The Green color indicate that a source column is mapped ( mapped means thesource columns in above grid created in the spreadsheet have been matched withstructure of the MySQL table as target columns shown in below grid ) to a targetcolumn and here in the above picture shows all the source columns have beenmapped with target columns.

    If you drag any target column heading backed by green color and leave it inoutside the grid, the color of the target column will be Red and the color of thesource column will be Gray. See in below picture.

    Here in the above picture the target column4 became Red and source column4became Gray.

    The Red color indicate that a target column is not mapped and the Gray colorindicate that, a source column is not mapped.

    It is to be noted that, a source column may be mapped to multiple target columns,although this action generates a warning dialog and right-clicking on a targetcolumn shows a menu with options to either Remove Column Mapping for asingle column, or to Clear All Mappings for all columns.

    Suppose, we are mapping manually by dragging source column4 with target

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    12 of 20 19/04/2014 2:57

  • column4 and column5. The target column4 will be mapping simply but when doingthe process for target column5 the warning dialog will appear, show below.

    Mapping methodsHere look the three mapping methods:

    The automatic mapping method attempts to match the Excel source columnnames with the MySQL target table column names.

    In the manual mapping method source column names are manually draggedwith the target column names. After the Automatic method is selected the Manualdragging method can also be performed.

    You may store your own mapping styles using the Store Mapping button andsaved a name like "name (dbname.tablename)" and it will be available in theMapping Method combo box.

    Stored mappings may be deleted or renamed within the AdvancedOptions dialog.

    Advanced OptionsIf we click the Advanced Options button the dialog looks similar to:

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    13 of 20 19/04/2014 2:57

  • Perform an automatic mapping when dialog opens: By this options, it happensautomatically to map the target and source when the Append Data dialog isopened.

    Automatically store the column mapping for the given table:To check thisoption, stores each mapping routine after executing the Append operation. Themapping routine is saved using the "tablenameMapping (dbname.tablename)"format. It can be performed manually by using the Store Mapping button.

    Reload stored column mapping for the selected table automatically: If astored mapping routine exists which matches all column names in the source gridwith the target grid, then it is automatically be loaded.

    The advanced Field Data Options:

    Use the first 100 (default) Excel data rows to preview and calculate data types.This determines the number of rows that the preview displays, and the values thataffect the automatic mapping feature.

    When you check the Use formatted values, the data from Excel is treatedas Text, Double, or Date. This is enabled by default. If we make it disabled, theexisting data is never treated as a Date type, so for example, this means that adate may be represented as a number..

    The Stored Column Mappings is a list of saved column mappings that weresaved with with the "Automatically store the column mapping for the given table"feature, or manually with the Store Mapping option.

    After completion all the steps if we click Append button the following screen willappear.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    14 of 20 19/04/2014 2:57

  • Export Excel Data into MySQL

    Microsoft Excel spreadsheet's data can be exported to a new MySQL databasetable by using the Export Excel Data to New Table option. At first you do the first6 steps above describe in Edit MySQL Data in Excel . Then enter some recordswith heading as you wish, into your Excel spreadsheet and select the records.Now look the sample records in the following picture -

    Now click on Export Excel Data to New Table mention by arrow in the abovepicture and look the following screen.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    15 of 20 19/04/2014 2:57

  • 1 - Specify a unique name for the MySQL table. Suppose the name of the MySQLtable is "invoice"

    2 - If Add a Primary Key column radio button is checked an additional primarykey column will be add, you can change the name of the column and by defaultthe data type will be integer which is displaying in 7.

    3 - When you check the Use Existing Columns radio button the column names willthe available in the combo box and you can choose the primary key column fromthe list, and the column will be selected. Suppose you selected INVOICE_NOcolumn, look the following picture.

    Here in the above picture the INVOICE_NO column have been selected and nowyou can change the Column Name (point 6), Data type (point 7) and theconstraint (point 9) which are active.

    4 - If you check the check box First Row Contains Column Names (point 4) thefirst column in your excel data will be heading other wise it will becolumn1,column2,... etc

    5 - Suppose you change the Data type and column name for other columns youcan click on the column heading. Assumed that we want to change the Data typeof INVOICE_DT, click on INVOICE_DT, and this column will be selected and thenyou can use point 6,7 and 9.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    16 of 20 19/04/2014 2:57

  • 6 - You can change the Column Name.

    7 - You can change the Data type

    8 - Advance Option show the following dialog.

    Advanced OptionsUse the first 100 (default) Excel data rows to preview and calculate datatypes: This option determines the number of rows that the preview displays, andthe specified values affect the feature of automatic mapping.

    Analyze and try to detect correct data type based on column field contents:This option try to analyze the data and determine the data type for the column.When the column contains multiple types of data then it defined as VARCHAR.

    Add additional buffer to VARCHAR length (round up to 12, 25, 45, 125, 255):When it is enabled, it detects the data type automatically and set to VARCHAR,then it find the maximum length for all rows within the column, and rounds it upthe maximum length to one of the defined lengths above and when disabled, thenthe VARCHAR length is set to the length of the longest entry in the Excelspreadsheet.

    Automatically check the Index checkbox for Integer columns: By default thisoption is enabled , and then columns with an Integer data type will have theCreate Index option enabled by default.

    Automatically check the Allow Empty checkbox for columns without anindex: By default this option is enabled, and when enabled the columns withoutthe Create Index checkbox checked will automatically enable the Allow Emptyconfiguration option.

    Use formatted values: By default this option is enabled, and the data from Excelis treated as Text, Double, or Date but when disabled the data's are independent,that is they have no specific data type.

    Remove columns that contain no data, otherwise flag them as "Excluded":When this option is enabled, columns without data in Excel are removed and notshown in the preview panel. By default this option is disabled and then thesecolumns will exist but have the Exclude Column option checked.

    After completion all setting click Export button the and see the following screenshot -

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    17 of 20 19/04/2014 2:57

  • We have compiled a number of MySQL Questions and Answers. Check andassess you MySQL skill by visiting those questions and answers.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    18 of 20 19/04/2014 2:57

  • 2 Comments

    Alan Lilly on my blog I have a FREE utility I wrote that is the fastest andeasiest way to convert excel data into mysql tables. go to my blogat panofish and look for excel2mysql. Let me know if you agree ordisagree and any feedback welcome!

    sunil very powerful tutorial

    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    19 of 20 19/04/2014 2:57

  • w3resource.com 2013-14 | Privacy | About | Contact | Feedback | Advertise

    Exporting and importing data between MySQL and Microsoft excel - w3... http://www.w3resource.com/mysql/exporting-and-importing-data-betwe...

    20 of 20 19/04/2014 2:57