|
25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more... |
Home Projects Sitemap Search Blog Forum+Chat About Us Privacy Terms of Use Feedback FAQ Images Services Payments Humor Music |
Migrating a database from Microsoft Access to MySQLThere are basically four ways to migrate a database from
Access
1) Pull the structure and data out of an .mdb fileWindowsI found the now-deprecated MySQL Migration Toolkit to be very helpful for migrating about 15 databases from Access to MySQL. The version I had was for Windows. After I changed a couple of global settings in Access and a couple of specific settings in the .mdb file to be migrated, the Migration Wizard read from the .mdb file, created corresponding table definitions using MySQL data types equivalent to the Access ones, and transferred all the table data. Although it was nearly 100% unsuccessful at migrating Access Queries to MySQL Views (I did that by manually copying the SQL code out of Access and hand-editing it), the Migration Toolkit's ability to migrate the tables saved time and was a lot better than no help at all. Unfortunately, the Migration Toolkit has been discontinued with no replacement available yet in MySQL Workbench, so right now "no help at all" is what's available, but that could change at any time. If you are still using an old version of MySQL and its GUI Tools package that has the Migration Toolkit, I'd suggest using it to do your migrations before upgrading to Workbench. LinuxThe MDB Tools package for Linux (one Ubuntu version) has programs to extract table structures and data directly from an Access .mdb file. I tested the programs but haven't used them for a migration. mdb-export - Export data in an MDB database table to CSV format. mdb-schema - Generate table creation SQL code. Output formats: access, sybase, oracle, postgres, mysql. mdb-sql - Interactive SQL interface to MDB Tools. mdb-tables - Get listing of tables in an MDB database. mdb-ver - Return the format of a given MDB database. The mdb-schema program has an output option called "mysql" that isn't mentioned in the documentation. That's the one to use. The command line version of MDB Tools worked for me without errors; I also have an Ubuntu Gnome GUI version that only crashes. 2) Use Access to push the structure and data into a MySQL databaseAccess-related documentation and help refer often to how an ODBC connection allows you to "access a data source" and query it. It took me a while to realize that it allows both read and write access to the data source, which means it gives you the ability to push the data into a second database. The advantages of this method are that you don't have to manually create the table definition with SQL (as with method 3, below), and you don't have to transfer the data manually. The drawbacks are: 1) It's complicated for the relatively small amount of automation it achieves. Nonetheless, it's satisfying after the last mouse click to see your database magically appear in MySQL. 2) Access doesn't know the MySQL-specific data types, so the generic SQL ones it chooses might or might not be the exact best choices. If you disagree with the ones chosen, you don't have a script that you can revise and rerun. You need to use an ALTER TABLE command to edit the definitions in-place in MySQL. These instructions were created in Windows XP SP3. Vista or 7 might have different names for things.
Once I got into the routine of it, my query migration method became easier to do than to describe. It seems cumbersome, but I find it easier to deal with queries as text files than it was in the forced layout and storage method in Access. I can put comments in the code, revise a query by editing its text file, create a new query from an old one by copying and revising the file, and look at how I did something in a query in a different database without opening the database and navigating to its queries. This is my query template: /* 07-14-2010 (last modified date) 3) Do the migration with manual hand-coding and data export/importThis option certainly gives you the most control, and it has the advantage that if the export/import doesn't go as expected, you can simply revise your script or the data format, and try again, as many times as needed.
4) Use a commercial migration toolkitIt looks like there are quite a few companies offering migration solutions for sale. I haven't tried any of them. Example .sql filesIn addition to their purpose of defining and populating a database with one table, these example .sql files also demonstrate the following:
1) The first file creates a database with one table: -- ---------------------------------------------------------------------- -- MySQL Migration Toolkit (manually edited) -- SQL Create Script -- ---------------------------------------------------------------------- -- Automatically show all warnings and errors from now on. WARNINGS; SET FOREIGN_KEY_CHECKS = 0; -- DROP DATABASE IF EXISTS `broadcast`; CREATE DATABASE IF NOT EXISTS `broadcast` CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE `broadcast`; -- ------------------------------------- -- Tables DROP TABLE IF EXISTS `broadcast`.`stations`; CREATE TABLE `broadcast`.`stations` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `notheard` CHAR(1) NULL, `approx` CHAR(1) NULL, `freq` DOUBLE NULL, `band` CHAR(2) NULL, `call` VARCHAR(6) NULL, `time` TINYINT UNSIGNED NULL, `city` VARCHAR(25) NULL, `network` VARCHAR(10) NULL, `notes` VARCHAR(80) NULL, PRIMARY KEY (id) ) ENGINE = INNODB; SET FOREIGN_KEY_CHECKS = 1; -- ---------------------------------------------------------------------- -- EOF 2) The second file (which could be made part of the first file) inserts 4 rows of data into the table: WARNINGS;
-- Save old setting, then disable foreign key checks
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
INSERT INTO `broadcast`.`stations`
(`notheard`, `approx`, `freq`, `band`, `call`, `time`, `city`, `network`, `notes`)
VALUES
(NULL, NULL, 640.0, 'am', 'KFI', 4, 'los angeles', NULL, 'talk'),
(NULL, NULL, 810.0, 'am', 'KGO', 4, 'san francisco', 'ABC', 'talk'),
(NULL, NULL, 1160.0, 'am', 'KSL', 4, 'salt lake city', 'CBS', 'talk,RadioMysteryTheater10pm'),
('N', NULL, 1070.0, 'am', 'KNX', NULL, 'los angeles', NULL, 'news, talk?');
-- Restore foreign key checks to whatever value it had before starting.
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
-- End of script
3) This shows an alternate method of populating the table. Microsoft Access exported the above data into a CSV (comma separated values) text file like this. I manually added the leftmost field (a primary key field called `id` with the values 1,2,3,4) to illustrate a point later: 1,,,640.00,"am","KFI",4,"los angeles",,"talk" As you can see, Access writes NULL values as "nothing" between the commas. MySQL can't interpret this format. It needs to find something there that indicates a null value. It can be either \N or NULL. So I had to edit the text file to turn it into this: 1,\N,\N,640.00,"am","KFI",4,"los angeles",\N,"talk" Given the above input file, the following script loads the data into the table. Notice that database, table, and field names are enclosed in the standard MySQL backtick delimiters, while text variables are enclosed in ordinary single quotes: WARNINGS; The first parameter in the field list, @ignoreid, is a variable rather than a field name. MySQL will read the first value from each line into that variable, but I don't instruct it to do anything with it; thus, the first value on each line is ignored. That's because it's an auto-increment primary key field in the table. Rather than create the possibility of a conflict with rows already in the table, I ignore the field, allowing MySQL to assign a new sequential value for that field for the incoming rows. That wouldn't really be a necessary precaution. I did it to demonstrate the feature. You can use the variable in other ways, too, such as perform a calculation with it and assign the result to a field of the table. Questions and comments are welcome in the discussion forum. |
|
|
|
|
|
Copyright ©2012 Steven Whitney. Last modified Sun 10/21/2012 05:52:13 -0700. |
||