Hi.
Can someone give me a small sample of the code for fast indexing and
relational work?
Sure. However, we can't see your database and don't know what you've
already done, but it sounds like you only imported the tables and didn't
bother taking any of the other steps required for a relational database,
such as set up primary keys and foreign keys, and possibly add indexes to
other columns used in joins, sorts and query criteria to make queries run
faster. Therefore, I'll list all of the steps you _should_ have taken, and
you can skip the ones that you've already done.
We have such big files, and it takes forever to get anything done.
That's where you'll see the huge difference between a beginner and someone
experienced with relational databases, and particularly with the database
tool he's using.
Honestly, it's a lot easier for a beginner to take these steps using the
GUI, but if you prefer code, then that's easy enough. In this example, the
two tables are named PERSON and ZIPCODE. When the step calls for you to
create a query, this is what you'll do:
1.) In the Database Window, ensure no item is selected, or at least no
table or SELECT query is selected.
2.) Select the Insert -> Query menu, then select the "OK" button to display
the "Show Table" dialog window. Select the "Close" button, because you
aren't going to use the GUI.
3.) Select the View -> SQL View menu to display the SQL View pane. This is
where you will paste the SQL code examples or type your own queries.
4.) Whatever is shown in the SQL View pane ("SELECT;") needs to be deleted
or overwritten by whatever you write or paste into the SQL View pane.
Essentially, you need to start with a blank slate for your query, but Access
tries to help you. If you're pasting in SQL code, that SQL code is all that
you need, not Access's "help."
5.) After you paste the SQL code or write your query, you will need to run
the query. Close the table that is going to be used in the query if it's
DDL (data definition language), or change the table's view from Design View
to Datasheet View if it DML (data manipulation language). Yes, I'll warn
you if it's going to be DDL in the following instructions.
6.) To run the query, ensure that the query you want to run is the active
window (the Title Bar of the window on top should give you a clue), and then
select the toolbar button marked with an exclamation point. (It's red in
Access 2003). It has a tool tip "Run" to help you identify it.
If you want to compare a "before" and "after" on a query with joined tables,
then create a new query and paste the following SQL code into the SQL View
pane:
SELECT PERSON.*, ZIPCODE.City, ZIPCODE.State
FROM PERSON INNER JOIN ZIPCODE ON PERSON.Zip = ZIPCODE.Zip
WHERE (ZIPCODE.State = 'Texas');
Replace Texas with any state you want that's listed in your table
(preferably with lots of records), then save this query. Get out your stop
watch, then run the query to see how much time it takes. Write down the
time, so you can compare it in a moment.
A. Primary Keys
Now, after importing the tables, a primary key should be assigned to each
table so that the database engine can uniquely identify each record in the
table. A side benefit is that the primary key will automatically be
indexed.
Did you add a primary key on the ID column in the PERSON table and the ZIP
column in the ZIPCODE table? If not, create a new query and paste the
following SQL code into the SQL View pane:
ALTER TABLE PERSON
ADD CONSTRAINT PrimaryKey PRIMARY KEY (ID);
This is DDL SQL, so ensure that the PERSON table is closed, then run the
query. Next, create a new query and paste the following SQL code into the
SQL View pane:
ALTER TABLE ZIPCODE
ADD CONSTRAINT PrimaryKey PRIMARY KEY (ZIP);
This is DDL SQL, so ensure that the ZIPCODE table is closed, then run the
query.
B. Relationships
The PERSON and ZIPCODE code tables are related, so if you don't want to set
the relationship in the Relationship window, the create a new query and
paste the following SQL code into the SQL View pane:
ALTER TABLE PERSON
ADD CONSTRAINT tblPersonZipCode_FK
FOREIGN KEY (ZIP) REFERENCES ZIPCODE ZIP;
This is DDL SQL, so ensure that the PERSON and ZIPCODE tables are closed,
then run the query. A side benefit to this constraint is that Jet will
automatically place a hidden index on the related column (because
referential integrity is enforced), so you don't need to add an index to the
column.
You can save these queries, but honestly, you'll probably never need them
again (unless you drop these constraints later and want to redo them), so
you don't need to save them. However, if you want an example for later
reference, you should save these queries.
C. Create a Query With a Join
Next, save your work and compact the database. (Select the Tools ->
Database Utilities -> Compact and Repair Database... menu.) If you didn't
create a query with a join on the two tables earlier, then create it now
with the SQL code I gave you above, save it, get out your stop watch, then
run the query to see how much time it takes.
It's a lot faster than without primary keys and indexes, huh?
HTH.
Gunny
See
http://www.QBuilt.com for all your database needs.
See
http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blogs:
www.DataDevilDog.BlogSpot.com,
www.DatabaseTips.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.