View Single Post
  #1 (permalink)  
Old 02-26-10, 12:54
kkrueck kkrueck is offline
Registered User
 
Join Date: Jul 2009
Posts: 16
How do I simulate 'auto_increment' on a non-primary key field

I have this table

Code:
CREATE TABLE wall_comments (commentID int unsigned NOT NULL auto_increment,
 wallmemberID int unsigned NOT NULL,
 topicID  int unsigned NOT NULL,
 posterID int unsigned  NOT NULL,
 posted DATETIME NOT NULL,
 comment varchar(350),
 primary key(commentID),
 unique key(wallmemberID, topicID, posterID, posted),
constraint foreign key (wallmemberID) references member_info(memberID),
constraint foreign key (posterID) references member_info(memberID)
) ENGINE = InnoDB;
I want wall_comments(topicID) to behave like an auto_increment field does. That is, I'd like to be able to do
Code:
INSERT INTO wall_comments(commentID, wallmemberID, topicID, posterID, posted, comment) VALUES('', 1, '', 10, now(), 'Some comment text');
Is there a way to does this that is safe when simultaneous INSERT occur, and there is a possible race conditon, short of using a transaction and doing:
Code:
select MAX(topciID)+1 as new_topicID
thanks,
Kurt
Reply With Quote