473,388 Members | 1,286 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,388 software developers and data experts.

Finding next ID auto increment

I'm having trouble trying to find the next ID in a mysql table which is set to autoincrement. I would like to retrieve this before the record is created so I can store its ID as a foriegn key with into another table.

I've tried a query

SELECT Max(ID) nextID FROM screenings

But I found this works until I delete the most recently created records because the Max ID is no longer the one previous of the next ID.

Any help would be appreciated
Apr 25 '06 #1
2 3594
Banfa
9,065 Expert Mod 8TB
Do you have to know the ID of the record before it is inserted?

Can't you
  1. create the record in the first table,
  2. get the ID it was created with
  3. store this in the second table

rather than
  1. guess at the ID you are about to create
  2. store this in the second table
  3. create the record in the first table,

If you do it as a transaction then you tables will never be out of sync.
Apr 25 '06 #2
Hi.
You could use DBCC CHECKIDENT (<table Name>, NORESEED) which will give you the current seed, add 1 to that (as long as the seed is set to inc. by one). However, this really isn't a good idea.

As previous post suggests. You should grab the identity of the new record with @@Identity and post to second table as the foreign key - don't forget to wrap in transaction so you don't lose data integrity.
May 4 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bob Bedford | last post by:
Does exist a next prev function in PHP/Mysql. I've a website with returns lot of records (say about 200). The user may browse trough the list or returned records, but he has to click the record...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
8
by: John Smith | last post by:
Hello all: Is there a way to determine the auto increment id from a newly inserted record in the database? Thanks, John
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
5
by: Paulovič Michal | last post by:
hi all, I have problem with SERIAL field type (or sequence functionality). I have table with three columns - ID, IDS, NAME. I want auto-increment IDS grouped by ID. Example: 1, 1, Ferdo 1, 2,...
5
by: vul | last post by:
In VB6 there is Auto Increment check box in Project Properties, which allow you have a new version every time you compile the project. Is there any easy way to have this feature in VB 2005? Some...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
5
by: Germaris | last post by:
Hi there! How should I write a query to get the next auto-increment value of a table? Many thanks in advance for any help. Best regards, Gerry
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.