473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating normalized tables with queries

I have a table in my database that is linked to an excel spreadsheet.
I need to be able to manipulate the data in this linked table so that I
can create smaller normalized tables that work with what I am trying to
do (all data used in my db will come from this one spreadsheet). I am
currently trying to utilize the make-table queries to create the tables
that I need from this data, however I am unsure of how to add foreign
keys to a table so that my data matches up correctly. Does anyone know
of a way to do this? Is there some sort of append query that can be
run to find entries in one table matching the entries of another table
as the criterion? Any help would be much appreciated!

Jun 9 '06 #1
2 2740
If this is a one-way deal, importing data into normalized tables and kissing
the old one goodbye, something along this line is what you need:

(Never tried pasting datasheets in this form, so if the tables are scrambled
I apologize in advance.)
Linked spreadsheet:

NameKey FirstName LastName HomePhone WorkPhone
1 Bob Wilson 111.222.333 444.555.6666
3 Harriet Stowe 777.888.9999 55.33.1111
Here are some queries to run

Make Normalized Tables 01 - Name Table (create name table, assuming each row
represents a different person)

SELECT Sheet1.NameKey, Sheet1.FirstName, Sheet1.LastName INTO MyNameTable
FROM Sheet1;

Resulting data in MyNameTable
NameKey FirstName LastName
1 Bob Wilson
3 Harriet Stowe
Make Normalized Tables 02 - Phone Table (create a phone table, using home
phone to begin with)

SELECT Sheet1.NameKey, Sheet1.HomePhone AS Phone, "H" AS PhoneType INTO
MyPhoneTable
FROM Sheet1
WHERE (((Sheet1.HomePhone) Is Not Null));

Resulting data in MyPhoneTable
NameKey Phone PhoneType
1 111.222.333 H
3 777.888.9999 H
Make Normalized Tables 03 - Append to Phone Table (append next type of
phone number to phone table, this time work phone)

INSERT INTO MyPhoneTable ( NameKey, Phone, PhoneType )
SELECT Sheet1.NameKey, Sheet1.WorkPhone AS Phone, "W" AS PhoneType
FROM Sheet1
WHERE (((Sheet1.WorkPhone) Is Not Null));

Resulting data in MyPhoneTable (including rows added above, of course)
NameKey Phone PhoneType
1 111.222.333 H
3 777.888.9999 H
1 444.555.6666 W
3 55.33.1111 W
Make Normalized Tables 04 - Names and Phones from new tables (stitch name
and phone back together in a simple select query)

SELECT MyNameTable.FirstName, MyNameTable.LastName, MyPhoneTable.PhoneType,
MyPhoneTable.Phone
FROM MyNameTable INNER JOIN MyPhoneTable ON MyNameTable.NameKey =
MyPhoneTable.NameKey;

Resulting data shown in this select query
FirstName LastName PhoneType Phone
Bob Wilson W 444.555.6666
Bob Wilson H 111.222.333
Harriet Stowe W 55.33.1111
Harriet Stowe H 777.888.9999

Make Normalized Tables 05 - Look like the old table using crosstab query
(present the normalized data to look like the original spreadsheet, except
column names, which I didn't bother about but could have if it mattered)

NameKey FirstName LastName H W
1 Bob Wilson 111.222.333 444.555.6666
3 Harriet Stowe 777.888.9999 55.33.1111
Fix all of the data on the way in. Don't drag it all in and then try to
match things up if you can avoid it.
Jun 9 '06 #2
Ja************@ge.com wrote:
of a way to do this? Is there some sort of append query that can be
run to find entries in one table matching the entries of another table
as the criterion? Any help would be much appreciated!


You can use a subquery that utilizes criteria to obtain the foreign
key. Here's an example:

Let's say I have tblEmployees that is mostly normalized and
tblVacations that uses a single field for the employee name from an
imported spreadsheet.

tblEmployees
EID Autonumber
EFirstName Text
ELastName Text
EID EFirstName ELastName
1 Joe Young
2 King Kong
3 The Hulk

tblVacations
VacationID Autonumber
VacationName Text
StartDate Date/Time
EndDate Date/Time
VacationID VacationName StartDate EndDate
1 King Kong 6/8/06 6/13/06
2 Joe Young 7/10/06 7/14/06

qryOnVacation:
SELECT EFirstName, ELastName, (SELECT VacationID FROM tblVacations
WHERE Date() BETWEEN StartDate AND EndDate AND VacationName =
tblEmployees.EFirstName & ' ' & tblEmployees.ELastName) AS VacationKey
FROM tblEmployees;

!qryOnVacation:
EFirstName ELastName VacationKey
Joe Young Null
King Kong 1
The Hulk Null

That was a warm up. Now I want to use the names in tblEmployee to
eliminate tblVacations.VacationName.

qryNewtblVacation:
SELECT VacationID, StartDate, EndDate, (SELECT EID FROM tblEmployees
WHERE EFirstName & ' ' & ELastName = tblVacations.VacationName) AS EID
FROM tblVacations;

!qryNewtblVacation:
VacationID StartDate EndDate EID
1 6/8/06 6/13/06 2
2 7/10/06 7/14/06 1

It's not a perfect example but should give you an idea of a way to
start matching up the ID's. The DLookup function acts much like a
subquery so it could be used instead the subquery.

James A. Fortune
CD********@FortuneJames.com

Jun 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alan | last post by:
Hi there, Are there Excel charting gurus here?? If so then please read on... Sorry for the cross-post but I'm not familiar with the Excel groups. I've posted to asp.general because if I have...
2
by: Tony Williams | last post by:
Sorry to repost but I am struggling with this. I have a suggestion which revolves around normalising data and producing union queries but am having a struggle understanding that concept. Is there...
44
by: Greg Strong | last post by:
Hello All, Is it better to create a query in DAO where a report has 4 sub-reports each of whose record source is a query created at runtime and everything is in 1 MDB file? From what I've...
6
by: Dennis Gearon | last post by:
If I want to set up a dbase with normalized tables for inserts,and a flattened table for selects, am i going in the right direction for speeding up a busy site? Also, if some of you are also...
1
by: phlype.johnson | last post by:
Suppose we have to design a database for a recruitment agency. There will be a table "candidates" with fields "candidateid","last name","first name" ; the languages mastered by a candidate as well...
0
by: phlype.johnson | last post by:
I'm struggling to find the best query from performance point of view and readability for a normalized DB design. To illustrate better my question on whether normalized designs lead to more complex...
6
by: Emin | last post by:
Dear Experts, When I use a single table I can easily use constraints to enforce my business logic, but what do I do when I normalize a single table into multiple tables. For example, imagine...
1
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our...
10
by: Richard | last post by:
Hi folks, thanks for taking the time to read this (and hopefully point our where I'm going wrong). The scenario: I have a local Access2007 database which links in several read only mySql...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.