Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CREATING AN EDIT PAGE WITHOUT A PRIMARY KEY

Status
Not open for further replies.

kingjjx

Programmer
Joined
Sep 18, 2001
Messages
181
Location
US
Hi, I am trying to create an edit page for data being submmited by a user. The only was I know how to do this is by using the primary key ...

THIS IS WHAT I PUT AS THE LINK TO GO TO THE EDIT PAGE:
<a href=&quot;
AND THIS IS WHAT IDENTIFIES WHICH RECORD TO EDIT:

<CFQUERY NAME=&quot;EditDynorun&quot; DATASOURCE=&quot;DynoRunEx&quot;>
SELECT * FROM RunData WHERE Make= #URL.PriKey#
</cfquery>

My question is , Is there a way to edit records if I dont have a PRIMARY KEY, I want to do it by Last name and First name instead since I didnt create a Primary key in my table. How do you do this ???

thanks
 
You absolutely need a primary key or you will not be able to uniquely identify the record to edit. What happens if two people have the same first and last names? If you think that this is not likely to happen, you may be right for your particular situation. But programs have a way of lasting much longer that programmers expect (witness the Y2K problem), and why take the chance that someone will have the same name when it's so-o-o-o easy to prevent a problem. Just do it right!

So, you need primary key, however, a primary key need not be a (single) field in the table. It can be any combination of fields that is sufficient to uniquely identify a record. for example, it could be &quot;#lastname##firstname##address1#&quot; or it could be parts of several fields combined. If you are using Access and you have been used to using an autonumber field as the primary key, you may think that a primary key has to be some kind of serial number. It's not true. But, you have to pick one or more fields that (together) uniquely identify the record.

You asked how to do this. Well, you just specify the fields or parts of fields that are in your key. You see, a primary key is not necessarily part of your database structure or configuration (although it usually is). It's a logical concept. In your case, your primary key is lastname+firstname. Thus to find the record you want, you say:

SELECT *
FROM dynodata
WHERE lastname = '#form.lastname#'
AND firstname = '#form.firstname#'

If in fact this were a good choice for a primary key, you would get back 0 or 1 record, no more. The fact that it's a primary key means that there is no other record whose key matches this one. So, your URL would have to pass two parameters (some_template.cfm?lastname=xxxxx&firstname=yyyyy). It's not too late to create a field to be the primary key in your table, if you want to. But, it's up to you to determine that it's impossible for two records to have the same primary key, or at the very most, that it is highly unlikely that two records will have the same key.
 
Amen, a440guy! A primary key is crucial in working with a database. What if you have two people with the same name? Unusual, but it happens. We have two guys here with the same name, and the last place I worked, there were two women with the same name and even the same middle initial. Calista :-X
Jedi Knight,
Champion of the Force
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top