473,769 Members | 6,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a record where Primary Keys are not known



I am trying to add a record using SQL. My problem is that the primary keys
are foreign keys, and these foreign keys are autonumbers. I therefore do
not know the primary keys of the record I am trying to insert. I therefore
do not think that I can use the sql "Insert Into" command.

Here is a simplified illustration of my tables:

tblFather
NaturalKey1 [PK1]
NatuarlKey2 [PK2]
SundryFields
ID [Autonumber]

tblMother
NaturalKey1 [PK1]
NatuarlKey2 [PK2]
SundryFields
ID [Autonumber]
tblChild
FatherFK [PK1]
MotherFK [PK2]
ChildFields

Now say that mother and father give birth. I know the natural keys of both
parents. I should think that I have to do some sort of Join, but I am not
sure how to proceed.

Many thanks
Ilan

Nov 12 '05 #1
7 2956
Ilan Sebba previously wrote:

Now say that mother and father give birth. I should think that I
have to do some sort of Join, but I am not
sure how to proceed.


(I seem to recall the same sort of problem.. but that was many years ago!)

In principle you have to lookup the ID of each parent using the keys that
you know. Then you can create your new record.

It would look something like this but it will depend on how you are
getting your natural key values and whether they are number or text.

Dlookup("[ID]","tblFathe r","[Naturalkey1]= knownvalue1 AND [NaturalKey2] =
knownvalue2")

Regards

Peter Russell

Nov 12 '05 #2
On Tue, 11 Nov 2003 13:03:31 GMT, "Ilan Sebba" <ilan underscore sebba
at btinternet stop com de***@yahoo.com> wrote:


I am trying to add a record using SQL. My problem is that the primary keys
are foreign keys, and these foreign keys are autonumbers. I therefore do
not know the primary keys of the record I am trying to insert. I therefore
do not think that I can use the sql "Insert Into" command.

Here is a simplified illustration of my tables:

tblFather
NaturalKey1 [PK1]
NatuarlKey2 [PK2]
SundryFields
ID [Autonumber]

tblMother
NaturalKey1 [PK1]
NatuarlKey2 [PK2]
SundryFields
ID [Autonumber]
tblChild
FatherFK [PK1]
MotherFK [PK2]
ChildFields

Now say that mother and father give birth. I know the natural keys of both
parents. I should think that I have to do some sort of Join, but I am not
sure how to proceed.

Many thanks
Ilan


If I understand the situation, I don't think you are missing anything
to be able to proceed. Give the tblChild record it's own, unique
primary key (using an AutoID). Then, as you have done, ensure that
there is one field to store the key for the father and another for the
key for the mother.

Given a father, you can then determine all his offspring. Given a
mother, you can do the same. Given a child, you can identify his
parents.

Chuck
Nov 12 '05 #3
Thanks for your reply.

I understand your solution, you are saying: retrieve the IDs of the records
in the parent tables and, armed with these keys, proceed to insert a record
into the child table.

I accept that this will work. However, I feel that there should be a more
elegant way, one which takes into account the existence of relationship
which exists between the parent tables and the children tables. It could be
that I am expecting too much from the relational database.

Anyway, the solution you propose works, and I will use it for now.

Many thanks
Ilan
"Peter Russell" <ru***@127.0.0. 1> wrote in message
news:me******** **************@ russellscott.bt internet.com...
Ilan Sebba previously wrote:

Now say that mother and father give birth. I should think that I
have to do some sort of Join, but I am not
sure how to proceed.


(I seem to recall the same sort of problem.. but that was many years ago!)

In principle you have to lookup the ID of each parent using the keys that
you know. Then you can create your new record.

It would look something like this but it will depend on how you are
getting your natural key values and whether they are number or text.

Dlookup("[ID]","tblFathe r","[Naturalkey1]= knownvalue1 AND [NaturalKey2] =
knownvalue2")

Regards

Peter Russell

Nov 12 '05 #4
Hmmm. I don't think I clearly explained my problem. Yes, the child tables
can be as follows:

tblFather
NaturalPK1
NaturalPK2
OtherFields
ID

tblMother
NaturalPK1
NaturalPK2
OtherFields
ID

tblChild
FatherFK [PK1]
MotherFK [PK2]
DateOfBirth [PK3]
OtherFields
ID (optional?)

My problem is this. I have a mother (i know her primary keys, but not her
autonumber ID) and I have a father (I know his primary keys, but not his
autonumber ID). I now want to say 'these two parents, they are going to
have a baby today - I want to insert the record'. I want to insert the
record using SQL. I know that MS-Access forms can do this very easily -
without the end user having to worry about IDs.

The solution posted by Peter Russell(if I understood correctly) is to use
the parents primary keys to look up their IDs, and then to insert that
record for the child. That works, but I feel that this fails to use the
power of the relational database, because it does not make use of the
relationship which exists between the parent tables and the child tables.
But would there be a method where I can join the three tables together and
then add a new record to this expanded table, without having to retrieve the
parent IDs?

Many thanks

Ilan
"Chuck Van Den Corput" <NO************ *****@sympatico .ca> wrote in message
news:9o******** *************** *********@4ax.c om...
On Tue, 11 Nov 2003 13:03:31 GMT, "Ilan Sebba" <ilan underscore sebba
at btinternet stop com de***@yahoo.com> wrote:


I am trying to add a record using SQL. My problem is that the primary keysare foreign keys, and these foreign keys are autonumbers. I therefore do
not know the primary keys of the record I am trying to insert. I thereforedo not think that I can use the sql "Insert Into" command.

Here is a simplified illustration of my tables:

tblFather
NaturalKey1 [PK1]
NatuarlKey2 [PK2]
SundryFields
ID [Autonumber]

tblMother
NaturalKey1 [PK1]
NatuarlKey2 [PK2]
SundryFields
ID [Autonumber]
tblChild
FatherFK [PK1]
MotherFK [PK2]
ChildFields

Now say that mother and father give birth. I know the natural keys of bothparents. I should think that I have to do some sort of Join, but I am notsure how to proceed.

Many thanks
Ilan


If I understand the situation, I don't think you are missing anything
to be able to proceed. Give the tblChild record it's own, unique
primary key (using an AutoID). Then, as you have done, ensure that
there is one field to store the key for the father and another for the
key for the mother.

Given a father, you can then determine all his offspring. Given a
mother, you can do the same. Given a child, you can identify his
parents.

Chuck

Nov 12 '05 #5
Ilan Sebba previously wrote:
Thanks for your reply.

I understand your solution, you are saying: retrieve the IDs of the
records
in the parent tables and, armed with these keys, proceed to insert a
record
into the child table.

I accept that this will work. However, I feel that there should be a
more
elegant way, one which takes into account the existence of relationship
which exists between the parent tables and the children tables. It
could be
that I am expecting too much from the relational database.


Until the child record exists there is nothing for the mother or father
records to link to.

The situation is different with forms where Access automatically fills in
the value of a the link field in the child table.

Regards

Peter Russell
Nov 12 '05 #6
"there is nothing for the mother or father records to link to"

That makes sense - I never really thought about what the link actually
links.

Many thanks

Ilan Sebba

"Peter Russell" <ru***@127.0.0. 1> wrote in message
news:me******** **************@ russellscott.bt internet.com...
Ilan Sebba previously wrote:
Thanks for your reply.

I understand your solution, you are saying: retrieve the IDs of the
records
in the parent tables and, armed with these keys, proceed to insert a
record
into the child table.

I accept that this will work. However, I feel that there should be a
more
elegant way, one which takes into account the existence of relationship
which exists between the parent tables and the children tables. It
could be
that I am expecting too much from the relational database.


Until the child record exists there is nothing for the mother or father
records to link to.

The situation is different with forms where Access automatically fills in
the value of a the link field in the child table.

Regards

Peter Russell

Nov 12 '05 #7
I suppose the "elegant" solution would be not to use the autonumbers. In
this case (unless, of course, the parent tables already have records you
are referencing in the child table), you can create the parent tables'
PKs and insert them into parent tables, then into the junction table.
This is what I have to do when interfacing Access BE with Palm-based FE.
I used the autonumbers but the exact problem you describe forced me to
use buffer tables and SQL pre-processing to put PAlm DB data into the
right tables in the right order - parents first, children last, to avoid
ref. integrity violations.

Pavel

Ilan Sebba wrote:

Thanks for your reply.

I understand your solution, you are saying: retrieve the IDs of the records
in the parent tables and, armed with these keys, proceed to insert a record
into the child table.

I accept that this will work. However, I feel that there should be a more
elegant way, one which takes into account the existence of relationship
which exists between the parent tables and the children tables. It could be
that I am expecting too much from the relational database.

Anyway, the solution you propose works, and I will use it for now.

Many thanks

Ilan

"Peter Russell" <ru***@127.0.0. 1> wrote in message
news:me******** **************@ russellscott.bt internet.com...
Ilan Sebba previously wrote:

Now say that mother and father give birth. I should think that I
have to do some sort of Join, but I am not
sure how to proceed.


(I seem to recall the same sort of problem.. but that was many years ago!)

In principle you have to lookup the ID of each parent using the keys that
you know. Then you can create your new record.

It would look something like this but it will depend on how you are
getting your natural key values and whether they are number or text.

Dlookup("[ID]","tblFathe r","[Naturalkey1]= knownvalue1 AND [NaturalKey2] =
knownvalue2")

Regards

Peter Russell

Nov 12 '05 #8

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

Similar topics

17
4478
by: Rick | last post by:
Hi all, Is there a MySQL function to get the first record through a query? I would like to open a page of client records with the first one shown. The other records can be accessed from a hyperlinked list. Thanks for any advice, Rick
3
3217
by: James | last post by:
Hello group: I've done alot of reading on this subject somewhat and have found that many people have many different opinions on this subject. My question centers mainly around using a lookup table to enable users to select a pre-defined list of values. I have developed a practice myself of avoiding AutoNumber type data fields for primary keys where the primary key will be related to a child table. Nevertheless, what do most users do...
5
3752
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for Office 2000. I am creating a database to track student athletes. I have created the following tables. The table title is to the far left, with fields under each. The common field will be the StudentID field, which is their student number assigned...
1
1720
by: JohnR | last post by:
I'm using oledb to an Access datatable. I load the table into a dataset. I create databindings to link the fields on my form to the corresponding fields in the datatable. The tables DefaultView.Sort is set to the field that is bound to a dropdown (where I can pick what record to view, and the dropdown list is sorted). I click a rec in the dropdown, and all the values of that record then populate the fields on my form. Editing, and...
8
3273
by: shumaker | last post by:
I'm wondering if adding an autonumber primary key will improve the performance of a multiuser access database on a network share. I have a website that lists many tips for improving performance of access, but doesn't mention primary keys. However, it seems logical to think that having no primary key means that when a user updates a record, the database has to do comparisons on multiple fields to identify the specific record being...
4
2148
by: charliej2001 | last post by:
This is my first post on groups and id like to say that already its helped me out loads, but can't quite find what im looking for now I have 3 tables storing information about people; Main, Temp and Dupl. What I want to do, using VBA, is check each record in the Temp table (10-50 records) against each record in the Main table (1000+ records); if any records then match, I want to store them in the Dupl table. The 3 tables are identical in...
6
3858
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson", NamePerson", "AgePerson" and "IDParent". A record contains the information about a person (his name, his...
1
1707
by: HandersonVA | last post by:
Would anyone please instruct how to prevent the duplicate record by setting the unique keys on the ms sql server? i've been checking the duplicate record as front-end and i found out if there is an internet delay or some other reasons, it has a chance to store the duplicated data into the database. so i realized it has to be done on the back-end side. for example, if i have three columns (office code, office id, office section) as a...
2
9166
by: Danny | last post by:
Hello, We imported a bunch of tables from a database and realized that the primary keys weren't copied to the destination db. In order to re- create the keys, we need to know which tables have them. Is there a command that I can use (on the source db) to find out which tables contain primary keys? The db has hundreds of tables and I'd rather not go through each one to see which has a primary key. Also, for future reference, is there a...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.