Friday, September 17, 2010

Use MS Access as the frontend for MySQL

1. Download and Install ODBC connector for MySQL

http://dev.mysql.com/downloads/connector/odbc/5.1.html

2. Go to Start-->Settings-->Control Panel--> Administrative Tools -->Data Sources(ODBC)

3. Double Click "Data Sources(ODBC)"

4. Click on "User DSN" tab --> Click on "Add.." button--> Scroll down to "MySQL ODBCx.x Driver, click it and Click on Finish

Populate the credentials. You can give any name for "Data Source Name". Let's say 'myData'. Specify you MySQL server IP address, username, password and the database. Click on 'Test' and see if the connector can connect to your MySQL database or not. If not, check if you can ping you MySQL box and check if the user is granted privilege to access locally or remotely.

5. Then go to Program Files and open MS Access. Create a new database and give any name you like.
Go to Tables and Right click. Go to "Link Tables.."

Then select ODBC database() from the "Files of type" and hit on "Link" button

Then it will pop up "Select Data Source" window. Go to "Machine Data Source" tab and click on the desired data source. Let's say you have created 'myData'. Then it pulls up all the table from your defined MySQL database defined in 'myData' data source. Select the table you want to view, edit from the access.

Some Links:
AND OR logic in MySQL query
http://www.plus2net.com/sql_tutorial/sql_or_and.php

Find duplicate data using access
http://articles.techrepublic.com.com/5100-10878_11-1043732.html

Append data from one table to another in access
http://support.microsoft.com/kb/306093

Advanced queries on MySQL

1. Update the data from one Table to another Table

Let's say we have Table1 and Table2 as the following:

Table1
col_num---- col_alpha-----col_cap
1-----------------a---------------NULL
2-----------------b---------------NULL
3-----------------c---------------NULL

Table2
col_alpha---- col_cap
a-----------------A
b-----------------B
c-----------------C

Let's say you want to populate Table1.col_cap (reads as col_cap column of Table1) with the data of Table2.col_cap if Table1.col_alpha=Table2.col_alpha, you can run the following query in MySQL

UPDATE Table1 JOIN Table2 ON Table2.col_alpha=Table1.col_alpha SET Table1.col_cap=Table2.col_cap

Final Result:
Table1
col_num---- col_alpha-----col_cap
1-----------------a---------------A
2-----------------b---------------B
3-----------------c---------------C


2. Append the data from one table to the another table


Let's consider that we have above two tables: Table1 and Table2

We can use the INSERT and SELECT query to append the data.

INSERT INTO Table1 (`col_alpha`,`col_cap`) SELECT `col_alpha`,`col_cap` FROM Table2;

Final Result:
Table1
col_num---- col_alpha-----col_cap
1-----------------a---------------A
2-----------------b---------------B
3-----------------c---------------C
4-----------------a---------------A
5-----------------b---------------B
6-----------------c---------------C

3. Select the data from multiple tables


Let's consider that we have above two tables: Table1 and Table2

Say you want to view Table1.col_num and Table2.col_cap if Table1.col_alpha=Table2.col_alpha, then

SELECT Table1.col_num,Table2.col_cap FROM Table1,Table2 WHERE Table1.col_alpha=Table2.col_alpha;

Final Result: View

col_num-------------col_cap
1-------------------------A
2-------------------------B
3-------------------------C