/    Sign up×
Community /Pin to ProfileBookmark

phpMyAdmin?

Does anybody know the code to insert in the sql command box in phpMyAdmin to make it add an auto incremented column? I want to make it so it will automaticly add a number for each row. I’m not that good with mySQL and I tried some stuff but no luck. Thanks for the help! ?

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — go to phpmyadmin

select your database (left)

select your table name (left)

on the page you are on, look for "add a new field" Then it will have a checkbox to says 1 fields, and click "go"

enter in the field name as id

make the field type SMALLINT

make the length/values 5

make the attributes UNSIGNED

make it not null

leave default blank

under extra choose auto increment

hit "save"

done.
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — After following your instuctions it gives back this as what I put in:

[b]ALTER TABLE news ADD id SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT FIRST[/b]

And it gives back this as an error:

[b]#1075 - Incorrect table definition; There can only be one auto column and it must be defined as a key[/b]

I'm not sure what's wrong here but it just shows that error and won't add it! Thanks for your help. ?
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — my bad. you're gonna have to create a new table from scratch.

do this:

go to phpmyadmin

select your database (left)

Create a new table, do this by filling in the Name input box with your desired table name and enter Fields as 1 and then press Go.

enter in the field name as id

make the field type MEDIUMINT

make the length/values 8

make the attributes UNSIGNED

make it not null

leave default blank

under extra choose auto increment

Choose the Radio button "Primary"

hit "save"

done.




sorry about that
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — I tried it without making a new table and just tried it on my exsiting one and it worked. Is there a reason I would have to create a new table because for one it seems to have worked without creating a new tables and two I don't really want to have to do that since I already have stuff in the table. Thanks for your help.

P.S. Ok, say I have three rows in the table, what would happen if I deleted row 2 and then added a new one? I ask cause each row has to have an id which is unique and cannot be used twice unless it dosen't exsist. Thanks for all your help.
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — Is there a reason I would have to create a new table because for one it seems to have worked without creating a new tables[/quote]
Whoa, you're right! ? my bad on that

so really the only thing wrong was not making it a primary key
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — P.S. Ok, say I have three rows in the table, what would happen if I deleted row 2 and then added a new one? I ask cause each row has to have an id which is unique and cannot be used twice unless it dosen't exsist. Thanks for all your help.[/quote]
the new one added would be 3, not 2.. (I'm pretty sure.)

try it
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — I did some more looking and I asked the last question toatally wrong. Sorry about that. Lost my head for a minute. :p

Here is the correct question. How can I make it so the id will always be in order. I did some more looking and it appears that when I delete say row 3 and then add one it ends up being row 4. How can I make it so if there is no row 3 it will add that instead of adding the next number.

Hope I got it right this time lol...
Copy linkTweet thisAlerts:
@solavarMar 20.2004 — With auto increment, if you delete record number 5, the next record will be record number 6. In other words, you can not replace deleted numbers.

So each time a new record is added, a new number is generated for it, even when the previous record has been deleted.
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — [i]Originally posted by mjdimick [/i]

[B]I did some more looking and I asked the last question toatally wrong. Sorry about that. Lost my head for a minute. :p



Here is the correct question. How can I make it so the id will always be in order. I did some more looking and it appears that when I delete say row 3 and then add one it ends up being row 4. How can I make it so if there is no row 3 it will add that instead of adding the next number.



Hope I got it right this time lol... [/B]
[/QUOTE]

why would you want it to do that? any purpose?
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — Well I have a page where these database rows are displayed. They are displayed by their row id which is a number and I found that it screws up the order of them when I delete one and add one. So by doing what I want I believe it will keep the order correct! ?
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — what does your query look like when you are selecting everything?

it should look something like this:
[code=php]$query = "SELECT * from TableName ORDER BY id desc";[/code]
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — It does!
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — I now found another problem. Because of it making it the next number it might run out of numbers to use because with my script there will be lots of deleting and adding going on so it will keep making it a higher number and to my knolage the auto increment option only allows a certain amount of numbers so it would stop working at some point. Plus I think it would just be better and more efficiant and just makes more sense the way I want it in my opinion. Thanks for your help. ?
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — MEDIUMINT(8) UNSIGNED will go up to 16,777,215 . Soo i dont think you have a problem there..
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — Well the numbers could easily get up to past the 1000's with how much it would be used and I would just feel better if I knew that all the numbers were being used before it added more. ?
Copy linkTweet thisAlerts:
@The_CheatMar 20.2004 — you could change it to BIGINT(20) UNSIGNED which would go up to more than you could probably use up in your lifetime. ? 18,446,744,073,709,551,615
Copy linkTweet thisAlerts:
@JickauthorMar 20.2004 — Ok, I changed it to that but I was wundering if you could still tell me how to make it do what I wanted because I would just like to know for future refrence. Thanks for all your help! ?
×

Success!

Help @Jick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...