Jump to content


Photo

auto increment field type


  • Please log in to reply
6 replies to this topic

#1 jpjsmith

jpjsmith
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationUSA

Posted 05 May 2003 - 08:01 AM

:oops:
I know this is a very simple question but I could not find (or just didn\'t look long enough) the answer. when creating a table, what is the column type for a field that is automaticaly incremented when not explcitly named in an insert statement? I plan to use this field as a primary key. Thanks for your help!! Yes, I am new to mysql!

#2 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 05 May 2003 - 08:15 AM

Normally integer....

sometimes BIGINT if you need a lot of records...

P.
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#3 jpjsmith

jpjsmith
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationUSA

Posted 05 May 2003 - 08:36 AM

Thanks for responding. The problem is that with both the int and bigint file types, the values do not automatically increment themselves when new entries are made into the table. The values are null or 0 if declared not null in the create table statement. Is there some code to put into the sql that will make the value if the field increment?

#4 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 06 May 2003 - 07:42 AM

OK, syntax of create:

Here is a table for files:

CREATE TABLE files (

  ID int(15) NOT NULL auto_increment,

  path varchar(255) NOT NULL default \'\',

  filename varchar(255) NOT NULL default \'\',

  contents blob NOT NULL,

  PRIMARY KEY  (ID)

) TYPE=MyISAM;



ID is auto incrementing...
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#5 jpjsmith

jpjsmith
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationUSA

Posted 06 May 2003 - 09:05 AM

Great! thanks for helping. this begs one question though, if i use this auto incrementing feild as a primary key, how would i retreive its value so i can use it as a foreign key in another table? thanks again!

#6 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 08 May 2003 - 08:36 AM

mysql_insert_id

refers to the last inserted ID in the auto_increment field.... It\'s thread specific, meaning that if 15 users are connected at the same time, mysql_insert_id will return the correct value to each user (or rather the script he is running (or she)).

Look in the php manual for help on this, it\'s quite easy.

P.
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#7 jpjsmith

jpjsmith
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationUSA

Posted 08 May 2003 - 11:33 AM

Thanks again for your help! thats just what i needed!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users