Click to See Complete Forum and Search --> : PHP/MySQL questions


dark_man
12-06-2001, 12:14 AM
I have started working with PHP and so far things have gone very well although I am having some trouble with a MySQL database. I have a field in my database that acts as a record number and is set to auto increment. The auto increment part of things works but how do I adjust the value of the field if I need to jump around the database for a update or delete. Basically how can I manually change the value of the record number field?

------------------
[SGC Ultimate pass... Don't read anything I post unless you want a headache ] (http://www.sharkyforums.com/ubb/Forum17/HTML/005187.html)

[Member of the Sharky High-Resolution Club] (http://www.sharkyforums.com/ubb/Forum17/HTML/005121.html)

Grizzly
12-06-2001, 07:35 AM
Hmmm, I'm not sure if I entirely follow you here.

You have an Auto-Number field, and that should be your Primary Key, or in MySQL terms, defined as Primary-Indexed-Unique.

You should never change the value of your Primary key, and why would you want to in the first place? If you change that records unique identifier, than you run the risk of data corruption.

Did I just not follow your question right? If so, please try to re-word your question.

moog
12-06-2001, 01:06 PM
Values that you may need to change in the future shouldn't be autoincrement - you'll have to add another field to the table...

moog

------------------
No Norm, those are your safety glasses... I'll wear my own thanks.

dark_man
12-06-2001, 01:57 PM
Ok how's this. If delete all the entries in the db and then proceed to add a new entry the record number field will pick up where it last left off. So if the last record was number 9 and then I delete everything in the db and add a new record the record number will be set to 10 when I want it set to 0. Does that help any?

------------------
[SGC Ultimate pass... Don't read anything I post unless you want a headache ] (http://www.sharkyforums.com/ubb/Forum17/HTML/005187.html)

[Member of the Sharky High-Resolution Club] (http://www.sharkyforums.com/ubb/Forum17/HTML/005121.html)

Begby
12-07-2001, 01:12 PM
If its an autoincrement primary key, then you cannot get it to set back to zero without deleting the table and recreating it. One of the rules of primary keys is that they are to be unique and only be used once, even if deleted. (However, there may be a switch in MySQL somewhere I don't know about). You will have this problem in Access also.

To make this work you will need to do the incrementing yourself. Create a numerical field, then whenever you add a record, find the maximum value of that field and make the new record be that value + 1.

------------------
--------------------
"Log, its better than bad, its good!"

moog
12-07-2001, 01:13 PM
That's one of the 'features' of autonumber fields - you've always got a unique field in the database no matter whether you subsequently delete records.

So - if you want this value to be zeroed or whatever, you'll have to either turn off the auto-increment or create a new field in the db. In both cases you'll have to set the value yourself using PHP when you write the record.

Hope this helps.

moog

------------------
No Norm, those are your safety glasses... I'll wear my own thanks.

dark_man
12-07-2001, 07:17 PM
Originally posted by moog:
That's one of the 'features' of autonumber fields - you've always got a unique field in the database no matter whether you subsequently delete records.

So - if you want this value to be zeroed or whatever, you'll have to either turn off the auto-increment or create a new field in the db. In both cases you'll have to set the value yourself using PHP when you write the record.

Hope this helps.

moog


Thanks for the help!



------------------
[SGC Ultimate pass... Don't read anything I post unless you want a headache ] (http://www.sharkyforums.com/ubb/Forum17/HTML/005187.html)

[Member of the Sharky High-Resolution Club] (http://www.sharkyforums.com/ubb/Forum17/HTML/005121.html)