Check next available index has auto-increment

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Check next available index has auto-increment

Post by richie256 »

Hi everybody

In MySQL, in the index field, I just don't know how to check for the next available field to act has a auto-increment...

Thanks!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Presuming there's reasons you don't want to use an actual auto-increment field you could just SELECT MAX(id) AS themax FROM foo and the next 'available' should be $themax+1
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

Ha ok right.

I want to use this way because there is two table with the same index... A primary table and a secondary table... I want to do this way because I think it is easier to program!!

Thank you!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Two tables with identical primary keys sounds like it's going to give you headaches and in the long run keeping them in synch is probably not going to be very easy.

Mac
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

twigletmac wrote:Two tables with identical primary keys sounds like it's going to give you headaches and in the long run keeping them in synch is probably not going to be very easy.
I must do this way:

In the first table (primary) I have the primary key and different fields, and in the second table, I have two keys: same key as primary table with the language key to have descriptions in different languages.

So I must do this way to support different language. For exemple, if I wish to add a new language, it will be easy.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Table 1 is assumed to have the auto_incrementing field.

Table 1: INSERT INTO table (field) VALUES ('value');

Table 2: INSERT INTO other_table (primary_key, field) VALUES (LAST_INSERT_ID(), 'value');
So I must do this way to support different language. For exemple, if I wish to add a new language, it will be easy.
Actually, it would probably be easier to set a primary key of the Id and the language of the article. And make the language of the article (or whatever content it is) and ENUM. Then simply select on that.
Post Reply