If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > AutoIncrement Function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-07, 22:28
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
AutoIncrement Function

Hi

I have a table where an ID is defined as PK with auto-increment.
In the same table, I have ID2 (as FK) and an ID3.
I need to increment ID3 by one for every ID2.
Something like this
Code:
INSERT INTO `tbl`
SET ID2 = '5' AUTO_INCREMENT `ID3`
Is this possible ?

Thanx
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 01-30-07, 03:02
Lexiflex Lexiflex is offline
Registered User
 
Join Date: Mar 2005
Location: Netherlands
Posts: 280
This should be possible in a trigger.

But the main question is why you would want to do this? The PK makes the records unique, adding ID3 makes an alternate key which adds no value to the record (it's just another technical key), it's redundant data.
Reply With Quote
  #3 (permalink)  
Old 02-05-07, 08:26
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
ID is PK and (ID2,ID3) is unique.

ID2 is used for reference to another table, and ID3 is required to be auto-incremented for every ID2 inserted.

If I can auto-increment for every ID2 inserted then, I can save on PHP code to do this.

Btw, for internal db programming, is Oracle the better choice ? I understand you write programs for your database within Oracle. I've to check with my host to provide the free version of Oracle.
__________________
MySQL 5.1
Reply With Quote
  #4 (permalink)  
Old 02-05-07, 10:22
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,259
As you are using MySQL V5 then you should be able to make use of MySQL's triggers and stored procedures to do this..
...got to agree with Lexiflex.. it does seem a very peculiar user requirement
if ID2 is incremented then ID3 should also be incremented and noth are a composite FK.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 02-05-07, 10:47
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
ID2 gets incremented in its own table for which its PK but in this table (where ID1 is PK), ID2 can be any number.

But for every ID2 inserted in this table, ID3 has to be incremented. I want this sequence.
__________________
MySQL 5.1
Reply With Quote
  #6 (permalink)  
Old 02-05-07, 14:25
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
Have you looked at the manual? There is an example which might be what you need to adapt.

http://dev.mysql.com/doc/refman/5.0/...increment.html


see the grp, animal, name example
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On