943,841 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 22895
  • MySQL RSS
Dec 21st, 2007
1

MySql Autoincrement

Expand Post »
Hi,

I have 2 questions regarding the auto increment datatype in mysql.

Q1) Since we all know that if we create an auto increment column, it will start from the number '1' but is there a way we can force it to start with the number '300' and it should increment to 301 on the next new record inserted then 302 and so on?


Q2) Is there a way to make the auto increment field prefixed by 000? For example instead of making it run from the number '1' it should auto increment in this method '001' then 002 and so on?



Thanx
Similar Threads
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
Dec 22nd, 2007
0

Re: MySql Autoincrement

Why would you want to do that? It makes no sense.
Last edited by iamthwee; Dec 22nd, 2007 at 10:01 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 23rd, 2007
0

Re: MySql Autoincrement

Depending on your version of MySQL there are a couple of ways to do this. Assuming a version of at least 3.23, you can do this
MySQL Syntax (Toggle Plain Text)
  1. CREATE TABLE MyTable
  2. (seq INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY)
  3. AUTO-INCREMENT=300

If your table already exists, insert a fake record with a value of 300 in the AUTO_INCREMENT field. The next INSERT will then have 301 as the value.

Q2) Is there a way to make the auto increment field prefixed by 000? For example instead of making it run from the number '1' it should auto increment in this method '001' then 002 and so on?

Why would you want to do this anyway? That field is used by MySQL to identify records. If you really want leading zeros for whatever reason, look up CONCAT or do it with your programming language.
Last edited by trudge; Dec 23rd, 2007 at 5:52 pm.
Reputation Points: 18
Solved Threads: 20
Junior Poster
trudge is offline Offline
176 posts
since Sep 2007
Mar 6th, 2008
0

Re: MySql Autoincrement

Quote ...
Is there a way to make the auto increment field prefixed by 000?
Yes, you can with the UNSIGNED ZEROFILL keywords. If you want to prefix the existing table column, use the following query
MySQL Syntax (Toggle Plain Text)
  1. ALTER TABLE `tablename` CHANGE `id` `id` INT( 10 ) UNSIGNED ZEROFILL;
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007
Mar 7th, 2008
0

Re: MySql Autoincrement

Can I use the "UNSIGNED ZEROFILL" keyword while creating a new table?

If yes, how?


Thanx
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
Mar 7th, 2008
0

Re: MySql Autoincrement

Syntax will be
mysql Syntax (Toggle Plain Text)
  1. CREATE TABLE tablename (
  2. id INT(10) UNSIGNED ZEROFILL DEFAULT NULL
  3. ) ENGINE=MyISAM;
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007
Jun 25th, 2009
0

Re: MySql Autoincrement

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Why would you want to do that? It makes no sense.
How about instead of being a pompous know-it-all, you answer the man's question? It's obviously a newbie question, the man thinks he needs to do it, whether it needs to be done or not is ancillary to the question: "Can it be done?". If you really wanted to be helpful, AFTER answering the question at hand, you could address his specific issue and whether or not it needs to be done. You might find out he's smarter than you and is doing something really clever.

Knowledge of these things is not a priori, as any modestly intelligent person understands (not you). Furthermore learning this material can be intimidating and middle-aged-virgin-nerd-bullys (like you) aren't making life easier for people who just want to learn a new skill.

I realize that at the age we were all having intercourse for the first time you were sitting at a computer terminal, but you need to show some human understanding, I don't care what your blog-dweeb-ranking is.

The same goes for any other smarmy weasel asking "Why would you want to do that anyway?". Just answer people's questions.

On to the matter at hand:
Under, no doubt, similar circumstances, I'd like to set up an unsigned 4byte auto-incrementing primary key that starts from 1 (not zero). My questions are...
1) what is the benefit of using INT(M) vs MEDIUMINT(M), etc?
2) does it start from 1? I assumed it started from zero
3) does setting the AUTO_INCREMENT field also imply that the integer is UNSIGNED?
4) is there a MYSQL documentation out there that actually has useful information (ie not the official documentation which bites)
Last edited by enginerd; Jun 25th, 2009 at 6:56 pm. Reason: forgot a question I wanted to ask
Reputation Points: 10
Solved Threads: 0
Newbie Poster
enginerd is offline Offline
1 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Using data from two different tables
Next Thread in MySQL Forum Timeline: Trigger concepts & Stored procedures.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC