473,604 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access the primary key while inserting a record

Is there a way to reference the 'primary key' field of a record you
are actually inserting at the moment.

Im using an 'int auto increment' field as the primary key.

I could reference it with a select statement and adding a 1 to the
last record's primary key. However, there must be a better way to do
this - what if someone adds a record right after I reference the last
one. I hope my question makes sense. Thanks! -Nick
Jul 16 '05 #1
3 5071
Nick wrote:
Is there a way to reference the 'primary key' field of a record you
are actually inserting at the moment.

Im using an 'int auto increment' field as the primary key.

I could reference it with a select statement and adding a 1 to the
last record's primary key. However, there must be a better way to do
this - what if someone adds a record right after I reference the last
one. I hope my question makes sense. Thanks! -Nick

You will have to study transaction and locking concepts, which are not
all that easy to understand and probably cannot be explained concisely.
I suggest you look at the on-line MySQL manual at
http://www.mysql.com/doc/en/index.html and do searches on "locking" and
"transactio ns".

You will probably need to lock the entire table, get a primary key,
increment the value, insert a new row with the new value, and then
release the lock, all within a transaction.
Jul 16 '05 #2
nb********@hotm ail.com (Nick) wrote in message
news:<ce******* *************** ****@posting.go ogle.com>...

Is there a way to reference the 'primary key' field of a record you
are actually inserting at the moment.


No, but you can fetch it immediately AFTER the insert by calling
mysql_insert_id (). For mroe information, see The Manual:

http://www.php.net/manual/en/functio...-insert-id.php

Cheers,
NC
Jul 16 '05 #3
On Sat, 19 Jul 2003 21:56:12 +0200, Nick wrote:
Is there a way to reference the 'primary key' field of a record you are
actually inserting at the moment.

There is no portable way to do it, neither in SQL nor in PHP.
Some database engines alow developer to acquire id by calling
some specific API function (MySQL), other provide a service
that returns unique ID (Postgres). Some provide both.
Im using an 'int auto increment' field as the primary key.

I could reference it with a select statement and adding a 1 to the last
record's primary key. However, there must be a better way to do this -
what if someone adds a record right after I reference the last one. I
hope my question makes sense. Thanks! -Nick


It has to be done in transaction, or when table is locked, which is not
portable too....

Personaly, I call uniqid() method of PHP, that generates 13 character
string, or 13 digit hex number. Then I use fixed char(13) field as id.
This is somewhat slower them 8-byte long int, but it is portable across
SQL engines.

DG
Jul 16 '05 #4

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

Similar topics

1
4041
by: Fardude | last post by:
ACCESS 97, Pessimistic Record Locking!??? Does Access 97 allow record level Pessimistic locking? In other words, when user A is editing a record (has it locked) and User B tries to edit it (attempts to lock it), user B will get an error saying this record cannot be locked now. It seems like Access 97 is doing optimistic record level locking. In above scenario user B, is allowed to edit the record but when tries to
1
2025
by: Erica | last post by:
Hi, I have searched everywhere, and I can't seem to find the answer to my problem. I am trying to write a very simple piece of ASP code to insert a record into a field in a database. Ultimately it will be for an admin form that takes user input, but for now I just want to get the most intrinsic part working. This is the code: <% ' Declare variables Dim strURL ' The URL of this page so the form will work no matter
3
16501
by: James Alba | last post by:
Hey all, I am accessing an ms access database using .NET and C#. Like so, /* Create the database connection. */ connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Name); connection.Open(); And all is good.
2
1305
by: Shapper | last post by:
Hello, I am trying to insert a record in an Access database using Asp.Net/Vb.Net. I am getting the error: "Operation must use an updateable query." How can I solve this problem? The code I am using is:
1
3709
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO CREATE TABLE dbo.aTable ( INT NOT NULL
3
2833
by: CourtGuy | last post by:
Hi Folks, I've got a problem that's been confounding me for months. I work for a criminal court, and I'm trying to set up an Access database to track petitions filed by criminal defendants. I am using the court's case file number record as the primary key to make sure the database doesn't wind up with multiple entries for the same case number. However, a single case number can have other defendants charged in the same case number who...
0
2627
by: freeskier | last post by:
Hello, Sorry for the newb question; I've spent a good amount of time trying to figure this out. I am fairly new to the Postgre world. I am currently in the process of upsizing several MS Access apps to a single PostgreSQL system; I will continue to use Access as the frontend. I have duplicated the table structure in Postgre and have successfully connected Access as a frontend to view data. I am having trouble inserting a record into...
2
1800
by: Question123 | last post by:
Hi i have one database table Table1.which contains almost 20000000 recordes. record to this table are inserted through storedprocedure. storedprocedure takes parameter as "value", Beginningdate, Endate . which will insert one record for each day between Beginning date and EndDate. Before inserting record i check is record exsist for date,if exsist i will update value otherwise insert new record.
3
1562
tjc0ol
by: tjc0ol | last post by:
Hi guys, Im a newbie in .NET, I follow the book in SitePoint which is Building your own ASP.NET Website using C# but I'm having trouble in inserting new data to MS ACCESS. When I run it, I've got an error i.e. You cannot add or change a record because a related record is required in table 'HelpDeskCategories'. How to correct this error and How to insert new record to MS Access without experiencing error? Here is my code: <script...
5
2159
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. So here's my method. I created two tables with the same structure as the table I'm inserting from. One table, Split_Temp, is the one I'll be inserting to. The other table, Split_Insert, contains the "Blank" record, which actually just has the word...
0
7929
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8419
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
8409
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
8280
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...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3907
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
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1266
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.